Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!rochester!udel!princeton!phoenix!bernsten From: bernsten@phoenix.Princeton.EDU (Dan Bernstein) Newsgroups: comp.unix.wizards Subject: error messages (was Re: friendly messages) Message-ID: <7555@phoenix.Princeton.EDU> Date: 3 Apr 89 01:34:49 GMT References: <604@marob.MASA.COM> Reply-To: bernsten@phoenix.Princeton.EDU (Dan Bernstein) Distribution: usa Organization: Hmph. Lines: 42 I don't know about System V, but under BSD systems, including 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@phoenix.princeton.edu