home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume06 / hd < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  4.6 KB

  1. From decwrl!wyse!uunet!allbery Sun Jan 29 20:30:54 PST 1989
  2. Article 802 of comp.sources.misc:
  3. Path: granite!decwrl!wyse!uunet!allbery
  4. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5. Newsgroups: comp.sources.misc
  6. Subject: v06i029: hd- A hex dump utility
  7. Message-ID: <47762@uunet.UU.NET>
  8. Date: 29 Jan 89 21:24:51 GMT
  9. Sender: allbery@uunet.UU.NET
  10. Reply-To: hal@dad.UUCP (Harold A. Miller)
  11. Lines: 124
  12. Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  13.  
  14. Posting-number: Volume 6, Issue 29
  15. Submitted-by: hal@dad.UUCP (Harold A. Miller)
  16. Archive-name: hd
  17.  
  18. #! /bin/sh
  19. echo shar: extracting hd.c
  20. if test -f 'hd.c' ; then
  21.     echo shar: will not overwrite hd.c
  22. else
  23.     cat > hd.c << '@EOF hd.c'
  24. /* ********************************************************************** */
  25. /*                                   hd.c                                 */
  26. /*                         A CP/M dump-like utility                       */
  27. /*                   Written by: Mike Ewan and Hal Miller                 */
  28. /* tektronix!tekgen!nesa!raven!mike       uw-beaver!tikal!dad!hal         */
  29. /* Use hereby authorized by anyone for any reason, except sale.           */
  30. /* ********************************************************************** */
  31.  
  32. #include <stdio.h>
  33.  
  34. main(argc,argv)
  35. int        argc ;
  36. char    *argv[] ;
  37. {
  38.     FILE    *fopen(), *fp ;
  39.     int     c     ;                       /* work character             */
  40.     int     llgt  ;                       /* for efficiency in loops    */
  41.     int     x     ;                       /* ascii column index         */
  42.     int     z     ;                       /* loop counter               */
  43.     long    addr  ;                       /* starting address for line  */
  44.     long    start ;                       /* optional begin file offset */
  45.     char    *flname ;                     /* pointer for file open      */
  46.     char    str[8]  ;                     /* work buffer for conversion */
  47.     char    asc[17] ;                     /* contents of ascii column   */
  48.  
  49.     if (argc == 3)
  50.         {                     /* given filename and starting offset     */
  51.         flname = argv[1] ;
  52.         sscanf(argv[2], "%x", &start) ;
  53.         }
  54.     else
  55.         {
  56.         if (argc == 2)
  57.             {                 /* no offset given, use beginning of file */
  58.             flname = argv[1] ;
  59.             start = 0L ;
  60.             }
  61.         else
  62.             {
  63.             printf("usage: %s filename [offset]\n", argv[0]) ;
  64.             exit(1) ;
  65.             }
  66.         }
  67.     if ((fp = fopen(flname,"r")) == NULL)
  68.         {
  69.         printf("%s: unable to open %s\n", argv[0], flname) ;
  70.         exit(1) ;
  71.         }
  72.  
  73.     x = 0 ;
  74.     addr = start ;
  75.  
  76.     sprintf(str, "%08x", addr) ;          /* set up and print start address */
  77.     llgt = strlen(str) ;                  /* for the first row              */
  78.     for (z=0; z<llgt; z++)
  79.         if (str[z] >= 'a' && str[z] <= 'f')
  80.             str[z] &= 0137 ;
  81.     printf("%8s:  ", str) ;
  82.  
  83.     if (start != 0)                       /* get to offset if need be       */
  84.         fseek(fp, start, 0) ;
  85.  
  86.     while ((c = getc(fp)) != EOF)
  87.         {                                 /* read and process entire file   */
  88.         sprintf(str,"%02x",c) ;           /* set up and print the hex stuff */
  89.         if (str[0] >= 'a' && str[0] <= 'f')
  90.             str[0] &= 0137 ;
  91.         if (str[1] >= 'a' && str[1] <= 'f')
  92.             str[1] &= 0137 ;
  93.         printf("%s ",str) ;
  94.  
  95.         if ((c >= ' ') && (c <= '~'))     /* set up the ascii stuff         */
  96.             asc[x] = c ;
  97.         else
  98.             asc[x] = '.' ;
  99.         x++ ;
  100.  
  101.         if (x >= 16)
  102.             {                             /* got enough to print a line     */
  103.             putchar(32) ;
  104.             for (z=0; z<=15; z++)
  105.                 {
  106.                 putchar(asc[z]) ;
  107.                 asc[z] = '\0' ;
  108.                 }
  109.             putchar(10) ;
  110.             x = 0 ;
  111.  
  112.             addr += 16L ;                 /* set up for next line's address */
  113.             sprintf(str, "%08x", addr) ;
  114.             llgt = strlen(str) ;
  115.             for (z=0; z<=llgt; z++)
  116.                 if (str[z] >= 'a' && str[z] <= 'f')
  117.                     str[z] &= 0137 ;
  118.             printf("%8s:  ",str) ;
  119.             }
  120.         }                                 /* end of file                    */
  121.  
  122.     llgt = 15 - x ;                       /* space fill rest of last line   */
  123.     for (z=0; z<=llgt; z++)
  124.         printf("   ") ;
  125.     putchar(32) ;
  126.     for (z=0; z<=15; z++)
  127.         putchar(asc[z]) ;
  128.     putchar(10) ;
  129.     fclose(fp) ;
  130.     exit(0) ;
  131. }
  132. @EOF hd.c
  133. if test 3871 -ne "`wc -c < hd.c`"; then
  134.     echo shar: checksum error - number of characters should be 3871
  135. fi
  136. fi
  137. chmod 666 hd.c
  138.  
  139.  
  140.