errno

Chris Torek torek at elf.ee.lbl.gov
Wed Jun 19 05:39:19 AEST 1991


>In article <212 at sleepy.UUCP>, allyn at sleepy.UUCP (Mark Allyn) writes:
>>I need to be able to take what is printed when you call perror
>>and put it into a string variable to be used in a c program.

In article <16982 at darkstar.ucsc.edu> jik at cats.ucsc.edu
(Jonathan I. Kamens) writes:
>  Check if your system has a function named strerror, that takes an int
>(usually errno) and returns a string. ... If not, you'll have to use
>sys_errlist ...

In fact, if you do not have strerror(), you should write it in terms
of sys_errlist:

>	if (errno < sys_nerr)
>		printf("%s\n", sys_errlist[errno]);
>	else
>		printf("Unknown error\n");

Better:

#include <stdio.h>

char *
strerror(err)
	register int err;
{
	extern char *sys_errlist[];
	extern int sys_nerr;
	static char unknown[19+40+1];
			/* 19: strlen("Unknown error code ") +
			   40: max length of 128 bit int in decimal
			       (-170141183460469231731687303715884105728) +
			   1: final '\0' */

	if ((unsigned)err < sys_nerr)
		return (sys_errlist[err]);
	(void) sprintf(unknown, "Unknown error code %d", err);
	return (unknown);
}
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 415 486 5427)
Berkeley, CA		Domain:	torek at ee.lbl.gov



More information about the Comp.unix.questions mailing list