home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_10 / 1010021a < prev    next >
Text File  |  1992-08-12  |  942b  |  45 lines

  1.  
  2. Listing 10 -- the file strerror.c
  3.  
  4. /* strerror function */
  5. #include <errno.h>
  6. #include <string.h>
  7.  
  8. char *_Strerror(int errcode, char *buf)
  9.     {    /* copy error message into buffer as needed */
  10.     static char sbuf[] = {"error #xxx"};
  11.  
  12.     if (buf == NULL)
  13.         buf = sbuf;
  14.     switch (errcode)
  15.         {    /* switch on known error codes */
  16.     case 0:
  17.         return ("no error");
  18.     case EDOM:
  19.         return ("domain error");
  20.     case ERANGE:
  21.         return ("range error");
  22.     case EFPOS:
  23.         return ("file positioning error");
  24.     default:
  25.         if (errcode < 0 || _NERR <= errcode)
  26.             return ("unknown error");
  27.         else
  28.             {    /* generate numeric error code */
  29.             strcpy(buf, "error #xxx");
  30.             buf[9] = errcode % 10 + '0';
  31.             buf[8] = (errcode /= 10) % 10 + '0';
  32.             buf[7] = (errcode / 10) % 10 + '0';
  33.             return (buf);
  34.             }
  35.         }
  36.     }
  37.  
  38. char *(strerror)(int errcode)
  39.     {    /* find error message corresponding to errcode */
  40.     return (_Strerror(errcode, NULL));
  41.     }
  42.  
  43.  
  44.  
  45.