home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / language / sozobon2 / nm.c < prev    next >
C/C++ Source or Header  |  1993-10-23  |  3KB  |  192 lines

  1. /* Copyright (c) 1988 by Sozobon, Limited.  Author: Johann Ruegg
  2.  *
  3.  * Permission is granted to anyone to use this software for any purpose
  4.  * on any computer system, and to redistribute it freely, with the
  5.  * following restrictions:
  6.  * 1) No charge may be made other than reasonable charges for reproduction.
  7.  * 2) Modified versions must be clearly marked as such.
  8.  * 3) The authors are not responsible for any harmful consequences
  9.  *    of using this software, even if they result from defects in it.
  10.  *
  11.  *    Alternative ST symbol table lister.
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <ar.h>
  16.  
  17. struct hdr {
  18.     int magic;
  19.     long tsize, dsize, bsize;
  20.     long syms;
  21.     long f1,f2;
  22.     int f3;
  23. } h;
  24.  
  25. struct sym {
  26.     char name[8];
  27.     char flags;
  28.     long value;
  29. };
  30.  
  31. int gflag;
  32.  
  33. main(argc, argv)
  34. char **argv;
  35. {
  36.     int many;
  37.  
  38.     many = argc > 2;
  39.  
  40.     while (--argc) {
  41.         argv++;
  42.         if (*argv[0] == '-')
  43.             doopt(argv[0]);
  44.         else
  45.             doname(argv[0], many);
  46.     }
  47.     exit(0);
  48. }
  49.  
  50. doopt(s)
  51. char *s;
  52. {
  53.     while (*++s)
  54.         switch (*s) {
  55.         case 'g':
  56.             gflag++;
  57.         }
  58. }
  59.  
  60. doname(s, many)
  61. char *s;
  62. {
  63.     FILE *fd, *fopen();
  64.     int i;
  65.  
  66.     fd = fopen(s, "rb");
  67.     if (fd == NULL) {
  68.         fprintf(stderr, "Can't open %s\n", s);
  69.         return;
  70.     }
  71.  
  72.     if (many)
  73.         printf("\n%s:\n", s);
  74.  
  75.     if (i = dohdr(fd)) {
  76.         if (i == -1)
  77.             doarch(fd);
  78.         else {
  79.             while (i--)
  80.                 dosym(fd);
  81.         }
  82.     }
  83.         
  84.     fclose(fd);
  85. }
  86.  
  87. dohdr(fd)
  88. FILE *fd;
  89. {
  90.     int i;
  91.     long len;
  92.  
  93.     fread(&h, 2, 1, fd);
  94.     if (h.magic == ARMAG1)
  95.         return -1;
  96.     i = fread((char *)&h + 2, sizeof(h) - 2, 1, fd);
  97.     if (i != 1 || h.magic != 0x601a) {
  98.         printf("Bad header\n");
  99.         return 0;
  100.     }
  101.     len = h.tsize + h.dsize;
  102.     fseek(fd, len, 1);
  103.     return h.syms / sizeof(struct sym);
  104. }
  105.  
  106. dosym(fd)
  107. FILE *fd;
  108. {
  109.     struct sym s;
  110.     int i;
  111.  
  112.     i = fread(&s, sizeof(s), 1, fd);
  113.     if (i != 1)
  114.         return;
  115.     if (gflag && not_glob(s.flags))
  116.         return;
  117.     printf("%.8s", s.name);
  118.     fill8(s.name);
  119.     printf("\t%8lx", s.value);
  120.     putchar(' ');
  121.     sflags(s.flags);
  122.     putchar('\n');
  123. }
  124.  
  125. fill8(s)
  126. char *s;
  127. {
  128.     int i;
  129.  
  130.     for (i=0; i<8; i++)
  131.         if (s[i] == 0)
  132.             putchar(' ');
  133. }
  134.  
  135. char *fname[] = {
  136.     "?0?", "bss", "text", "?3?", "data",
  137.     "?5?", "?6?", "?7?"
  138. };
  139.  
  140. not_glob(x)
  141. {
  142.     x &= 0xff;
  143.     if (x & 0x20)
  144.         return 0;
  145.     x &= ~0x20;
  146.     if (x == 0x88)
  147.         return 0;
  148.     return 1;
  149. }
  150.  
  151. sflags(x)
  152. {
  153.     x &= 0xff;
  154.     if (x & 0x20)
  155.         printf("global ");
  156.     x &= ~0x20;
  157.     if (x == 0x88)
  158.         printf("external abs");
  159.     else if (x == 0xc0)
  160.         printf("equ abs");
  161.     else if (x == 0xd0)
  162.         printf("equ reg abs");
  163.     else {
  164.         x &= 7;
  165.         printf(fname[x]);
  166.     }
  167. }
  168.  
  169. doarch(fd)
  170. FILE *fd;
  171. {
  172.     struct ar_hdr a;
  173.     int i;
  174.     long len, ftell();
  175.  
  176. more:
  177.     i = fread(&a, sizeof(a), 1, fd);
  178.     if (i != 1)
  179.         return;
  180.     printf("\n(%.18s):\n", a.ar_name);
  181.     len = a.ar_size + ftell(fd);
  182.  
  183.     i = dohdr(fd);
  184.     if (i <= 0)
  185.         return;
  186.  
  187.     while (i--)
  188.         dosym(fd);
  189.     fseek(fd, len, 0);
  190.     goto more;
  191. }
  192.