[alt.sources] [unix-programmer] Re: Checking return values

chris@mimsy.umd.edu (Chris Torek) (10/26/90)

Archive-name: strerror/26-Oct-90
Original-posting-by: chris@mimsy.umd.edu (Chris Torek)
Original-subject: Re: Checking return values (was: Trojan Horses, NFS, etc.)
Reposted-by: emv@math.lsa.umich.edu (Edward Vielmetti)

[Reposted from comp.unix.programmer.
Comments on this service to emv@math.lsa.umich.edu (Edward Vielmetti).]

>In article <1990Oct25.075856.4923@robobar.co.uk> ronald@robobar.co.uk
>(Ronald S H Khoo) writes:
>> 	perror(name);
>> 	fprintf(stderr, "%s: error opening %s (see error message above)\n",
>> 		progname, name);

In article <8215:Oct2521:30:3890@kramden.acf.nyu.edu>
brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes:
>Correct but ugly. As long as you have an array sys_errlist[] ...

But you do not---not on all systems, anyway.  (It was not documented
originally and a number of vendors changed things.)

Instead of inventing Yet Another Way To Print Errors, why not use the
one mandated by ANSI C, called `strerror'?

	char *strerror(int err);
	/* returns the system error string associated with system error
	   number `err' */

	fprintf(stderr, "%s: error opening %s: %s\n",
	    progname, name, strerror(errno));

strerror is to be declared in <string.h> (`errno' is declared in <errno.h>)
and found in the standard C library.  If it does not exist on your system,
add it (edit <string.h> if necessary; be sure to save the original, and to
note that the change may need to be reapplied on every new release until
your vendor catches on to the fact that the ANSI C standard exists).

A suitable implementation (which may be compiled and merged into
libc.a if necessary) appears below.  (I could delete the copyright,
since I wrote the thing, but I am feeling perverse. :-) )

/*
 * Copyright (c) 1989 The Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by the University of California, Berkeley.  The name of the
 * University may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include <string.h>

/*
 * Return the error message corresponding to some error number.
 */
char *
strerror(e)
	int e;
{
	extern int sys_nerr;
	extern char *sys_errlist[];
	static char unknown[30];

	if ((unsigned)e < sys_nerr)
		return (sys_errlist[e]);
	(void) sprintf(unknown, "Unknown error: %d", e);
	return (unknown);
}
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 405 2750)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris