🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.programmer
Article #1095

Re: Checking return values (was: Trojan Horses, NFS, etc.)

#1095
From: Chris Torek
Date: Fri, 26 Oct 1990 14:46
71 lines
3234 bytes
>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
Technical Details
Message-ID: <27201@mimsy.umd.edu>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!wuarchive!zaphod.mps.ohio-state.edu!rpi!bu.edu!purdue!haven!mimsy!chris
References: <4315@pkmab.se> <1990Oct24.193712.8693@athena.mit.edu> <8215:Oct2521:30:3890@kramden.acf.nyu.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!wuarchive!zaphod.mps.ohio-state.edu!rpi!bu.edu!purdue!haven!mimsy!chris
From: chris@mimsy.umd.edu (Chris Torek)
Newsgroups: comp.unix.programmer
Subject: Re: Checking return values (was: Trojan Horses, NFS, etc.)
Message-ID: <27201@mimsy.umd.edu>
Date: 26 Oct 90 14:46:45 GMT
References: <4315@pkmab.se> <1990Oct24.193712.8693@athena.mit.edu> <8215:Oct2521:30:3890@kramden.acf.nyu.edu>
Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742
Lines: 71