perror.cc


      //  perror.cc
      
      /*  This is what perror() does.
       */
      
      //  For access to errno
      #include <errno.h>
      
      //  For access to fprintf and stderr
      #include <stdio.h>
      
      //  For access to strerror
      #include <string.h>
      
      //  perror()
      //  ------------------------------------------------------------------
      /*
       *    Prints an error message, constructed from a caller-supplied
       *    message, a colon, and a system-defined string determined by the
       *    current value of errno.
       */
        void
        perror( const char *msg )
        {
          fprintf(stderr, "%s: %s\n", msg, strerror( errno ) );
          return;
        }
      
      #include <stdlib.h>
      //  main()
      //  ------------------------------------------------------------------
      /*
       *    Invokes this perror with various values, supplied on the command
       *    line, assigned to errno.
       */
        int
        main( int argc, char *argv[], char *envp[] )
        {
          char  arg[80];
          for (int i = 1; i < argc; i++ )
          {
            memset( arg, '\0', sizeof( arg ) );
            strncpy( arg, argv[i], sizeof( arg ) - 1 );
            errno = strtoul( arg, 0, 0 );
            perror( arg );
          }
          exit( 0 );
        }