home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / emx / test / hexdump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-02  |  3.8 KB  |  177 lines

  1. /* hexdump.c (emx+gcc) */
  2.  
  3. /* test setmode(), _isterm() */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <io.h>
  8. #include <fcntl.h>
  9. #include <getopt.h>
  10. #include <sys/ioctl.h>
  11.  
  12. #define FALSE 0
  13. #define TRUE  1
  14.  
  15. static int opt_b = FALSE;
  16. static int opt_t = FALSE;
  17. static int opt_v = FALSE;
  18. static unsigned char inp_buf[10240];
  19. static char out_buf[100];
  20. static char hex[] = "0123456789ABCDEF";
  21.  
  22. static void usage (void)
  23. {
  24.   puts ("Usage: hexdump [-btv] [-n<count>] [<input_file>]");
  25.   exit (1);
  26. }
  27.  
  28. static void flush (void)
  29. {
  30.   if (write (1, out_buf, strlen (out_buf)) != strlen (out_buf))
  31.     {
  32.       perror ("write() failed");
  33.       exit (2);
  34.     }
  35. }
  36.  
  37.  
  38. static void talk_about (int fd, const char *name)
  39. {
  40.   int i;
  41.  
  42.   fprintf (stderr, "%s: isatty:  %d\n", name, isatty (fd));
  43.   fprintf (stderr, "%s: _isterm: %d\n", name, _isterm (fd));
  44.   if (ioctl (fd, FGETHTYPE, &i) < 0)
  45.     perror ("ioctl FGETHTYPE");
  46.   else
  47.     switch (i)
  48.       {
  49.       case HT_FILE:
  50.         fprintf (stderr, "%s is a regular file\n", name);
  51.         break;
  52.       case HT_UPIPE:
  53.         fprintf (stderr, "%s is a unnamed pipe\n", name);
  54.         break;
  55.       case HT_NPIPE:
  56.         fprintf (stderr, "%s is a named pipe\n", name);
  57.         break;
  58.       case HT_DEV_NUL:
  59.         fprintf (stderr, "%s is the null device\n", name);
  60.         break;
  61.       case HT_DEV_CON:
  62.         fprintf (stderr, "%s is the console\n", name);
  63.         break;
  64.       case HT_DEV_CLK:
  65.         fprintf (stderr, "%s is the clock device\n", name);
  66.         break;
  67.       case HT_DEV_OTHER:
  68.         fprintf (stderr, "%s is an unknown device\n", name);
  69.         break;
  70.       default:
  71.         fprintf (stderr, "%s: unknown handle type: %d\n", name, i);
  72.         break;
  73.       }
  74. }
  75.  
  76.  
  77. int main (int argc, char *argv[])
  78. {
  79.   int i, fd, c, x, m, n, nread;
  80.   char *d, *inp_fname;
  81.  
  82.   opterr = FALSE; optswchar = "-"; optind = 0;
  83.   nread = sizeof (inp_buf);
  84.   while ((c = getopt (argc, argv, "bn:tv")) != EOF)
  85.     {
  86.       switch (c)
  87.         {
  88.         case 'b':
  89.           opt_b = TRUE;
  90.           break;
  91.         case 'n':
  92.           nread = atoi (optarg);
  93.           if (nread < 1 || nread > sizeof (inp_buf))
  94.             usage ();
  95.           break;
  96.         case 't':
  97.           opt_t = TRUE;
  98.           break;
  99.         case 'v':
  100.           opt_v = TRUE;
  101.           break;
  102.         default:
  103.           usage ();
  104.         }
  105.     }
  106.   m = 0;
  107.   if (opt_b) m |= O_BINARY;
  108.   if (opt_t) m |= O_TEXT;
  109.   if (optind == argc)
  110.     {
  111.       fd = 0;         /* stdin */
  112.       inp_fname = "stdin";
  113.       if (m != 0 && setmode (fd, m) < 0)
  114.         {
  115.           perror ("setmode() failed");
  116.           return (2);
  117.         }
  118.     }
  119.   else if (optind + 1 == argc)
  120.     {
  121.       inp_fname = argv[optind];
  122.       fd = open (argv[optind], O_RDONLY | m);
  123.       if (fd < 0)
  124.         {
  125.           perror ("open() failed");
  126.           return (1);
  127.         }
  128.     }
  129.   else
  130.     usage ();
  131.   if (opt_v)
  132.     {
  133.       talk_about (fd, inp_fname);
  134.       talk_about (1, "stdout");
  135.     }
  136.   x = 0; d = out_buf; n = 0; i = 0;
  137.   for (;;)
  138.     {
  139.       if (i >= n)
  140.         {
  141.           i = 0;
  142.           n = read (fd, inp_buf, nread);
  143.           if (n < 0)
  144.             {
  145.               perror ("read() failed");
  146.               return (2);
  147.             }
  148.           if (n == 0)
  149.             break;
  150.         }
  151.       if (x >= 16)
  152.         {
  153.           *d++ = '\n'; *d = 0;
  154.           flush ();
  155.           x = 0; d = out_buf;
  156.         }
  157.       c = inp_buf[i++];
  158.       if (x != 0)
  159.         *d++ = ' ';
  160.       ++x;
  161.       *d++ = hex[(c >> 4) & 0x0f];
  162.       *d++ = hex[c & 0x0f];
  163.     }
  164.   if (out_buf[0] != 0)
  165.     {
  166.       *d++ = '\n'; *d = 0;
  167.       flush ();
  168.     }
  169.   return (0);
  170. }
  171.  
  172. /*
  173.  * Local variables:
  174.  * compile-command: "make hexdump.exe"
  175.  * end:
  176.  */
  177.