home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1998 < prev    next >
Internet Message Format  |  1990-12-28  |  3KB

  1. From: chris@mimsy.umd.edu (Chris Torek)
  2. Newsgroups: alt.sources
  3. Subject: [unix-programmer] Re: Checking return values (was: Trojan Horses, NFS, etc.)
  4. Message-ID: <1990Oct26.160016.20983@math.lsa.umich.edu>
  5. Date: 26 Oct 90 16:00:16 GMT
  6.  
  7. Archive-name: strerror/26-Oct-90
  8. Original-posting-by: chris@mimsy.umd.edu (Chris Torek)
  9. Original-subject: Re: Checking return values (was: Trojan Horses, NFS, etc.)
  10. Reposted-by: emv@math.lsa.umich.edu (Edward Vielmetti)
  11.  
  12. [Reposted from comp.unix.programmer.
  13. Comments on this service to emv@math.lsa.umich.edu (Edward Vielmetti).]
  14.  
  15. >In article <1990Oct25.075856.4923@robobar.co.uk> ronald@robobar.co.uk
  16. >(Ronald S H Khoo) writes:
  17. >>     perror(name);
  18. >>     fprintf(stderr, "%s: error opening %s (see error message above)\n",
  19. >>         progname, name);
  20.  
  21. In article <8215:Oct2521:30:3890@kramden.acf.nyu.edu>
  22. brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes:
  23. >Correct but ugly. As long as you have an array sys_errlist[] ...
  24.  
  25. But you do not---not on all systems, anyway.  (It was not documented
  26. originally and a number of vendors changed things.)
  27.  
  28. Instead of inventing Yet Another Way To Print Errors, why not use the
  29. one mandated by ANSI C, called `strerror'?
  30.  
  31.     char *strerror(int err);
  32.     /* returns the system error string associated with system error
  33.        number `err' */
  34.  
  35.     fprintf(stderr, "%s: error opening %s: %s\n",
  36.         progname, name, strerror(errno));
  37.  
  38. strerror is to be declared in <string.h> (`errno' is declared in <errno.h>)
  39. and found in the standard C library.  If it does not exist on your system,
  40. add it (edit <string.h> if necessary; be sure to save the original, and to
  41. note that the change may need to be reapplied on every new release until
  42. your vendor catches on to the fact that the ANSI C standard exists).
  43.  
  44. A suitable implementation (which may be compiled and merged into
  45. libc.a if necessary) appears below.  (I could delete the copyright,
  46. since I wrote the thing, but I am feeling perverse. :-) )
  47.  
  48. /*
  49.  * Copyright (c) 1989 The Regents of the University of California.
  50.  * All rights reserved.
  51.  *
  52.  * Redistribution and use in source and binary forms are permitted
  53.  * provided that the above copyright notice and this paragraph are
  54.  * duplicated in all such forms and that any documentation,
  55.  * advertising materials, and other materials related to such
  56.  * distribution and use acknowledge that the software was developed
  57.  * by the University of California, Berkeley.  The name of the
  58.  * University may not be used to endorse or promote products derived
  59.  * from this software without specific prior written permission.
  60.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  61.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  62.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  63.  */
  64.  
  65. #include <string.h>
  66.  
  67. /*
  68.  * Return the error message corresponding to some error number.
  69.  */
  70. char *
  71. strerror(e)
  72.     int e;
  73. {
  74.     extern int sys_nerr;
  75.     extern char *sys_errlist[];
  76.     static char unknown[30];
  77.  
  78.     if ((unsigned)e < sys_nerr)
  79.         return (sys_errlist[e]);
  80.     (void) sprintf(unknown, "Unknown error: %d", e);
  81.     return (unknown);
  82. }
  83. -- 
  84. In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 405 2750)
  85. Domain:    chris@cs.umd.edu    Path:    uunet!mimsy!chris
  86.