Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!olivea!spool.mu.edu!mips!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!ucbvax!pasteur!dog.ee.lbl.gov!elf.ee.lbl.gov!torek From: torek@elf.ee.lbl.gov (Chris Torek) Newsgroups: comp.unix.questions Subject: Re: errno Keywords: errno Message-ID: <14431@dog.ee.lbl.gov> Date: 18 Jun 91 19:39:19 GMT References: <212@sleepy.UUCP> <16982@darkstar.ucsc.edu> Reply-To: torek@elf.ee.lbl.gov (Chris Torek) Organization: Lawrence Berkeley Laboratory, Berkeley Lines: 42 X-Local-Date: Tue, 18 Jun 91 12:39:19 PDT >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. In article <16982@darkstar.ucsc.edu> jik@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 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@ee.lbl.gov