/* vi: set tabstop=4 shiftwidth=4: */ /* * $Id: error.c,v 1.2 1996/12/28 22:11:08 schweikh Exp $ */ /*@ignore@ */ #ifndef _POSIX_SOURCE #define _POSIX_SOURCE 1 #endif /*@end@ */ #include #include #include #include #include #include "shutup.h" #include "error.h" /*@-ptrarith@ */ /*@-redecl@ */ /* * progname is a short string prepended to error messages. */ /*@i1@ */ static char *progname = "(program name not assigned)"; void err_ret (const char *,...) /* @globals progname@ */ ; void err_sys (const char *,...) /* @globals progname@ */ ; void err_dump (const char *,...) /* @globals progname@ */ ; void err_msg (const char *,...) /* @globals progname@ */ ; void err_quit (const char *,...) /* @globals progname@ */ ; void err_progname (void) /* @globals progname,stderr,fileSystem@ */ /* @modifies *stderr,fileSystem@ */ ; char * program_name (void) /* @globals progname@ */ ; static void err_doit (int, const char *, va_list ap) /* @globals progname,stderr,fileSystem,errno@ */ /* @modifies *stderr,fileSystem,ap,errno@ */ ; void set_progname (char *, char *) /* @globals progname,internalState@ */ /* @modifies progname,internalState@ */ ; void cfprintf (FILE * fp, const char *,...) /* @globals progname@ */ /* @modifies *fp@ */ ; char * program_name (void) { return progname; } /* * Checked fprintf: do fprintf and exit if it failed * (probably due to an I/O or "no space on device" error). */ void cfprintf (FILE * fp, const char *fmt,...) { va_list ap; va_start (ap, fmt); if (vfprintf (fp, fmt, ap) < 0) { err_sys ("fprintf failed for format `%s'", fmt); } va_end (ap); } /* * Print the program name and nothig else on stderr. */ void err_progname (void) { Fprintf (stderr, "%s", progname); Fflush (stderr); } void set_progname (char *argv0, char *def) { if (argv0 == NULL || argv0[0] == '\0') { progname = def; } else { char *p = strrchr (argv0, '/'); if (p == NULL) { progname = argv0; } else { progname = p + 1; /* skip '/' */ } } } /* * Nonfatal error related to a system call. Print a message and return. */ void err_ret (const char *fmt,...) { va_list ap; va_start (ap, fmt); err_doit (1, fmt, ap); va_end (ap); } /* * Fatal error related to a system call. Print a message and exit(). */ void err_sys (const char *fmt,...) { va_list ap; va_start (ap, fmt); err_doit (1, fmt, ap); va_end (ap); exit (EXIT_FAILURE); } /* * Fatal error related to a system call. Print a message and dump core. */ void err_dump (const char *fmt,...) { va_list ap; va_start (ap, fmt); err_doit (1, fmt, ap); va_end (ap); abort (); /* @NOTREACHED@ */ exit (EXIT_FAILURE); /* should never get here */ } /* * Nonfatal error unrelated to a system call. Print a message and return. */ void err_msg (const char *fmt,...) { va_list ap; va_start (ap, fmt); err_doit (0, fmt, ap); va_end (ap); } /* * Fatal error unrelated to a system call. Print a message and exit(). */ void err_quit (const char *fmt,...) { va_list ap; va_start (ap, fmt); err_doit (0, fmt, ap); va_end (ap); exit (EXIT_FAILURE); } static void err_doit (int errnoflag, const char *fmt, va_list ap) { int errno_save = errno; Fflush (stdout); Fprintf (stderr, "%s: ", progname); Vfprintf (stderr, fmt, ap); if (errnoflag != 0) { Fputc (':', stderr); Fputc (' ', stderr); errno = errno_save; perror (NULL); } else { Fputc ('\n', stderr); } Fflush (NULL); }