errno

Jonathan I. Kamens jik at cats.ucsc.edu
Thu Jun 13 07:39:57 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.

  Check if your system has a function named strerror, that takes an int
(usually errno) and returns a string.  If it has it, there should be a man
page for it.  If there is, then use it.

  If not, you'll have to use sys_errlist, as you're trying to do.  But you're
not quite doing it right....

|> I know that the errno variable is an external variable which points
|> to some internal table in the kernel called sys_errlist. 

  sys_errlist is not "some internal table in the kernel," it's an array of
strings compiled into the C library.  The other variable you need to know
about is the int sys_nerr, which contains the number of strings in
sys_errlist.  Before trying to reference a string in sys_errlist, you need to
check if errno is less than sys_nerr; if it is not, then the error does not
have a corresponding string in the sys_errlist.

|> I tried the following logical solution after RTFM but it did not
|> work (it got a seg fault)

  I see a couple of problems.

|> extern char **sys_errlist;

  This should be "extern char *sys_errlist[];".  Also, you should declare
"extern int sys_nerr;".

|> printf("%d\n",(int)*(sys_errlist+errno));

  This should be

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

-- 
Jonathan Kamens					jik at CATS.UCSC.EDU



More information about the Comp.unix.questions mailing list