home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / hacking / internet / desc02.sh / perror2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-04  |  1.3 KB  |  56 lines

  1. /* perror2 -    Like perror(3), but with two string prefixes
  2.  *
  3.  * SYNOPSIS
  4.  *    void perror2 (const char *str1, const char *str2) ;
  5.  *
  6.  * DESCRIPTION
  7.  *    Prints str1, then a colon and a space, then str2, then a colon and
  8.  *    a space, then the error message corresponding to the contents of
  9.  *    errno, then a newline on stderr.
  10.  */
  11.  
  12. static char rcsid[] = "$Id: perror2.c,v 1.2 1992/12/02 03:49:59 tim Exp $";
  13.  
  14. extern int strlen () ;
  15.  
  16.  
  17. void perror2 (str1, str2)
  18.    char *str1, *str2 ;
  19. {
  20.    extern int errno ;
  21.    extern char *sys_errlist[] ;
  22.    extern int sys_nerr ;
  23.    register int save_errno = errno ;
  24.    static char unknown_error[] = "Unknown error" ;
  25.    static char colon_space[2] = {':', ' '} ;
  26.    static char newline = '\n' ;
  27.    char *p ;
  28.  
  29.    if (save_errno < 0 || save_errno >= sys_nerr)
  30.       p = unknown_error ;
  31.    else
  32.       p = sys_errlist[save_errno] ;
  33.    write (2, str1, strlen (str1)) ;
  34.    write (2, colon_space, sizeof (colon_space)) ;
  35.    write (2, str2, strlen (str2)) ;
  36.    write (2, colon_space, sizeof (colon_space)) ;
  37.    write (2, p, strlen (p)) ;
  38.    write (2, &newline, 1) ;
  39.    }
  40.  
  41.  
  42. #ifdef TEST
  43.  
  44. int main (argc, argv)
  45.    int argc ;
  46.    char **argv ;
  47. {
  48.    extern int errno ;
  49.  
  50.    errno = atoi (argv[1]) ;
  51.    perror2 (argv[2], argv[3]) ;
  52.    exit (1) ;
  53.    }
  54.  
  55. #endif    /* TEST */
  56.