Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!spool.mu.edu!agate!darkstar!cats.ucsc.edu!jik From: jik@cats.ucsc.edu (Jonathan I. Kamens) Newsgroups: comp.unix.questions Subject: Re: errno Keywords: errno Message-ID: <16982@darkstar.ucsc.edu> Date: 12 Jun 91 21:39:57 GMT References: <212@sleepy.UUCP> Sender: usenet@darkstar.ucsc.edu Organization: University of California, Santa Cruz Lines: 42 In article <212@sleepy.UUCP>, allyn@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@CATS.UCSC.EDU