/* *Copyright (c) 1996, 1997 by Oracle Corporation. All Rights Reserved. \ $Header: util.c 11-jul-97.21:24:20 smckinle Exp $ */ /* * Error handling and reporting routines */ #include #include #ifndef WIN32 #include #else #include #endif #ifdef VMS #include #include #include #endif #ifndef WIN32 static struct timeval time_start, time_stop; /* for real time */ #endif static double start, stop, seconds; char *sys_err_str(); /* * Fatal error. Print a message, dump core */ err_dump(va_alist) va_dcl { va_list args; char *fmt; va_start(args); fmt = va_arg(args, char *); vfprintf(stderr, fmt, args); va_end(args); fprintf(stderr, "%s \n", sys_err_str()); fflush(stdout); /* abort does not flush stdio buffers */ fflush(stderr); abort(); /* dump core and terminate */ exit(1); /* should not get here */ } /* * Fatal error related to a system call. Print a message and terminate. */ err_sys(va_alist) va_dcl { va_list args; char *fmt; va_start(args); fmt = va_arg(args, char *); vfprintf(stderr, fmt, args); va_end(args); fprintf(stderr, "%s \n", sys_err_str()); exit(1); } /* * Print a message and return to caller */ void err_ret(va_alist) va_dcl { va_list args; char *fmt; va_start(args); fmt = va_arg(args, char *); vfprintf(stderr, fmt, args); va_end(args); fprintf(stderr, "%s \n", sys_err_str()); fflush(stdout); fflush(stderr); return; } /* * return a string containing some additional operating-system dependent * information. */ #if !defined(WIN32) && !defined(VMS) extern int errno; /* UNIX errno number */ extern int sys_nerr; /* # of error message strings in sys table */ extern char *sys_errlist[]; /* the system error message table */ #endif char * sys_err_str() { static char msgstr[1024]; #ifndef WIN32 if (errno != 0) { if (errno > 0 && errno < sys_nerr) sprintf(msgstr, "(%s)", sys_errlist[errno]); else sprintf(msgstr, "(errno = %d)", errno); } else { msgstr[0] = '\0'; } #endif return(msgstr); } /* * start the time, * save some info for the stop timer to use */ void t_start() { #ifndef WIN32 # if defined(_SVID_GETTOD) if (gettimeofday(&time_start) < 0) # else if (gettimeofday(&time_start, (struct timezone *)0) < 0) # endif err_sys("t_start: gettimeofday() error"); #endif } /* * stop the timeer and save appropiate info */ void t_stop() { #ifndef WIN32 # if defined(_SVID_GETTOD) if (gettimeofday(&time_stop) < 0) # else if (gettimeofday(&time_stop, (struct timezone *)0) < 0) # endif err_sys("t_stop: gettimeofday() error"); #endif } /* * return the real elapsed time in seconds */ double t_getrtime() { #ifndef WIN32 start = ((double)time_start.tv_sec)* 1000000.0 + time_start.tv_usec; stop = ((double)time_stop.tv_sec) * 1000000.0 + time_stop.tv_usec; seconds = (stop - start) / 1000000.0; #endif return (seconds); }