error messages (was Re: friendly messages)

Dan Bernstein bernsten at phoenix.Princeton.EDU
Mon Apr 3 11:34:49 AEST 1989


I don't know about System V, but under BSD systems, including <errno.h>
gives you not only perror() but a sys_errlist[] of error message texts.
So how about this error message facility:

  static char unknownerr[32];

  char *err(errno)
  int errno;
  {
   if (errno < 0 || errno >= sys_nerr)
    {
     (void) sprintf(unknownerr,"Unknown error number %d",errno);
     return(unknownerr);
    }
   else
     return(sys_errlist[errno]);
  }

This is not a high-level routine, nor should it be. Instead of
attempting to build the generality of printf() into the error
message printer, I just reference err(errno) and use printf()
or sprintf() as usual. Why rebuild what's already there?

Some say that the error message facility should automatically print the
program name for you, and others say that the wave of ANSI/the future
is to include info/warning/verybad/fatal/endoftheworld; I'm happy with

    fprintf(stderr,"%s: cannot open %s: %s\n",progname,fn,err(errno))
or
    fprintf(stderr,"%s: endoftheworld: cannot open %s: %s\n",
	    progname,fn,err(errno))

(here progname is (argv[0] ? argv[0] : "Unknown program name")).

Along the same philosophy that motivates err(): if I ever found a need
to specify info/warning/fatal dynamically, I would much rather have a
function errlev() that returned a string based on a numeric input, and
continue using printf() at the top, than write a new errmsg() routine
for the occasion. I think this is the same philosophy behind a lot of
UNIX: combine little programs and functions to do big work.

---Dan Bernstein, bernsten at phoenix.princeton.edu



More information about the Comp.unix.wizards mailing list