[comp.unix.questions] A better perror

guy%gorodish@Sun.COM (Guy Harris) (08/21/87)

(This has ceased to be a discussion of C, and has become a discussion of
features of UNIX.  It is being redirected to comp.unix.questions.)

> 	extern int	errno;
> 	extern char	*sys_errlist[];
> 
> Do an 'nm' command on libc.a to confirm the existence of these names,
> if not using a standard UN*X (works on SYSV, and on my BSD-derived
> system.)

Or, instead of outsmarting yourself by using a technique too clever by half,
just look in the Effing Manual!  You'll find "sys_errlist" listed in the
"perror" manual page somewhere in some subsection of section 3, and "errno"
described in the "intro" manual page in section 2, unless your vendor has
totally screwed up their documentation.

If it doesn't have "errno", it's not only not "standard UN*X", it's not any
sort of UN*X; the number of programs that would be broken by omitting "errno"
is very large.  If it doesn't have "sys_errlist", fewer programs will be
broken, but some will be.

BTW, do NOT assume that the value in "errno" is a valid index into the
"sys_errlist" array!  The chances are good that it is, but it is NOT certain;
check the "errno" value against "sys_nerr" (described along with "sys_errlist"
in the manual) and make sure it's greater than zero.  If it's not a valid
index, generate the message some other way, preferably including the value of
"errno" in decimal.  (If it IS a valid index, NEVER print the value of "errno"
in decimal; programs like "cpio" have a tendency to print the error number and
NOT print the message, which is incredibly idiotic.)
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com