home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2870 / od.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-28  |  3.5 KB  |  191 lines

  1. /*
  2.  * Copyright 1991, John F. Haugh II
  3.  * An unpublished work.
  4.  * All rights reserved.
  5.  *
  6.  * Permission is granted to copy and create derivative works for any
  7.  * non-commercial purpose, provided this copyright notice is preserved
  8.  * in all copies of source code, or included in human readable form
  9.  * and conspicuously displayed on all copies of object code or
  10.  * distribution media.
  11.  */
  12.  
  13. #ifndef    lint
  14. static    char    sccsid[] = "@(#)od.c    2.1    07:15:25    2/27/91";
  15. #endif
  16.  
  17. #define    SKIP_SPACE(cp)    while (*cp && isspace (*cp)) *cp++
  18.  
  19. #include <sys/types.h>
  20. #include <ctype.h>
  21. #include "crash.h"
  22.  
  23. od (string)
  24. char    *string;
  25. {
  26.     long    base;
  27.     long    length = 0;
  28.     char    *cp;
  29.     char    *fmt = 0;
  30.     int    i, j;
  31.     unsigned char    c;
  32.     unsigned short    s;
  33.     long    l;
  34.  
  35.     for (cp = string;*cp && *cp != ',';cp++)
  36.         ;
  37.  
  38.     if (*cp == ',')
  39.         *cp++ = '\0';
  40.  
  41.     if (expr (&string, &base))
  42.         return -1;
  43.     else
  44.         string = cp;
  45.  
  46.     if (*string) {
  47.         SKIP_SPACE(string);
  48.  
  49.         for (cp = string;*cp && *cp != ',';cp++)
  50.             ;
  51.  
  52.         if (*cp == ',')
  53.             *cp++ = '\0';
  54.  
  55.         if (expr (&string, &length))
  56.             return -1;
  57.         else
  58.             string = cp;
  59.  
  60.         SKIP_SPACE(string);
  61.         if (*string)
  62.             fmt = string;
  63.         else
  64.             fmt = "x";
  65.     } else {
  66.         length = 16;
  67.         fmt = "x";
  68.     }
  69.     if (strlen (fmt) > 1) {
  70.         while (length > 0) {
  71.             printf ("%0.8x", base);
  72.  
  73.             for (cp = fmt;length > 0 && *cp;) {
  74.                 if (l_lseek (memfd, base, 0) == -1)
  75.                     return;
  76.  
  77.                 switch (*cp) {
  78.                     case 'a':
  79.                         printf ("\n%0.8x", base);
  80.                         break;
  81.                     case 'n':
  82.                         printf ("\n        ");
  83.                         break;
  84.                     case 'c':
  85.                     case 'b':
  86.                         if (r_read (memfd, &c, 1) == -1)
  87.                             return;
  88.                         base += 1;
  89.                         length -= 1;
  90.                         if (*fmt == 'b' ||
  91.                             (c <= ' ' || c > '~'))
  92.                             printf (" %0.2x", c);
  93.                         else
  94.                             printf (" '%c", c);
  95.                         break;
  96.                     case 'd':
  97.                     case 'o':
  98.                     case 'x':
  99.                         if (r_read (memfd, &s, 2) == -1)
  100.                             return;
  101.                         base += 2;
  102.                         length -= 2;
  103.                         printf (*cp == 'd' ? " %5u":
  104.                             (*cp == 'o' ? " %6o":
  105.                                 " %0.4x"), s);
  106.                         break;
  107.                     case 'D':
  108.                     case 'O':
  109.                     case 'X':
  110.                         if (r_read (memfd, &l, 4) == -1)
  111.                             return;
  112.                         base += 4;
  113.                         length -= 4;
  114.                         printf (*cp == 'D' ? " %9lu":
  115.                             (*cp == 'O' ? " %11lo":
  116.                                 " %0.8lx"), l);
  117.                         break;
  118.                     case 'Y':
  119.                         if (r_read (memfd, &l, 4) == -1)
  120.                             return;
  121.                         base += 4;
  122.                         printf (" %.24s", ctime (&l));
  123.                         break;
  124.                 }
  125.                 cp++;
  126.             }
  127.             putchar ('\n');
  128.         }
  129.     } else {
  130.         while (length > 0) {
  131.             printf ("%0.8x", base);
  132.  
  133.             for (i = 0;i < 16 && length > 0;) {
  134.                 if (l_lseek (memfd, base, 0) == -1)
  135.                     return;
  136.  
  137.                 switch (*fmt) {
  138.                     case 'c':
  139.                     case 'b':
  140.                         if (r_read (memfd, &c, 1) == -1)
  141.                             return;
  142.                         base += 1;
  143.                         i += 1;
  144.                         length -= 1;
  145.                         if (*fmt == 'b' ||
  146.                             (c <= ' ' || c > '~'))
  147.                             printf (" %0.2x", c);
  148.                         else
  149.                             printf (" '%c", c);
  150.                         break;
  151.                     case 'd':
  152.                     case 'o':
  153.                     case 'x':
  154.                         if (r_read (memfd, &s, 2) == -1)
  155.                             return;
  156.                         base += 2;
  157.                         i += 2;
  158.                         length -= 2;
  159.                         printf (*fmt == 'd' ? " %5u":
  160.                             (*fmt == 'o' ? " %6o":
  161.                                 " %0.4x"), s);
  162.                         break;
  163.                     case 'D':
  164.                     case 'O':
  165.                     case 'X':
  166.                         if (r_read (memfd, &l, 4) == -1)
  167.                             return;
  168.                         base += 4;
  169.                         i += 4;
  170.                         length -= 4;
  171.                         printf (*fmt == 'D' ? " %9lu":
  172.                             (*fmt == 'O' ? " %11lo":
  173.                                 " %0.8lx"), l);
  174.                         break;
  175.                     case 'Y':
  176.                         if (r_read (memfd, &l, 4) == -1)
  177.                             return;
  178.                         base += 4;
  179.                         i += 16;
  180.                         length -= 4;
  181.                         printf (" %.24s", ctime (&l));
  182.                         break;
  183.                 }
  184.             }
  185.             putchar ('\n');
  186.         }
  187.         if (i != 16)
  188.             printf ("%0.8x", base);
  189.     }
  190. }
  191.