home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / editors / mntemacs.zoo / utils / printstk.c < prev   
Encoding:
C/C++ Source or Header  |  1992-04-29  |  2.2 KB  |  116 lines

  1. /* 
  2.  * utility to print the value of _stksize from gcc-cc1.ttp
  3.  *
  4.  *    Usage: printstk [<filename>]
  5.  *        if <filename> is not specified defaults to .\gcc-cc1.ttp
  6.  *    ++jrb
  7.  *
  8.  *      modified to print a value of _initial_stack in cases when this
  9.  *      is defined instead of _stksize -- mj
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #ifndef __MINT__
  15. #include <unixlib.h>
  16. #else
  17. #include <unistd.h>
  18. #endif                /* __MINT__ */
  19. #include <string.h>
  20. #include <st-out.h>
  21.  
  22. long lseek(int, long, int);
  23. static char *sym_names[] = { "__stksize", "__initial_stack" };
  24.  
  25. long find_offset (fd, fn, what)
  26. int fd;
  27. char *fn;
  28. int *what;
  29. {
  30.     struct aexec head;
  31.     struct asym  sym;
  32.     int found;
  33.     int    all = 0;
  34.     int index = 1;
  35.     
  36.     if(read(fd, &head, sizeof(head)) != sizeof(head))
  37.     {
  38.     perror(fn);
  39.     exit(2);
  40.     }
  41.     if(head.a_magic != CMAGIC)
  42.     {
  43.     fprintf(stderr,"Invalid magic number %x\n", head.a_magic);
  44.     exit(3);
  45.     }
  46.     if(head.a_syms == 0)
  47.     {
  48.     fprintf(stderr,"%s: no symbol table\n", fn);
  49.     exit(4);
  50.     }
  51.     if(lseek(fd, head.a_text+head.a_data, 1) != 
  52.        (head.a_text+head.a_data+sizeof(head)))
  53.     {
  54.     perror(fn);
  55.     exit(5);
  56.     }
  57.     for(;;)
  58.     {
  59.     if(index && (read(fd, &sym, sizeof(sym)) != sizeof(sym)))
  60.     {
  61.         fprintf(stderr, "symbol _stksize not found\n");
  62.         exit(6);
  63.     }
  64.     /* after symbol read check first for _stksize */
  65.     index ^= 1;
  66.     if (strncmp(sym_names[index], sym.a_name, 8) == 0)
  67.     {
  68.         if ((found = (sym.a_type & A_DATA)) || all++)
  69.         break;
  70.     }
  71.     }
  72.     
  73.     if(!found)
  74.     {
  75.     fprintf(stderr, "symbol _stksize is undefined\n");
  76.     exit(9);
  77.     }
  78.     *what = index;
  79.     return sym.a_value + sizeof(head);
  80. }
  81.  
  82. int main(argc, argv)
  83. int argc;
  84. char **argv;
  85. {
  86.     int fd;
  87.     int what;
  88.     long stksize, offset;
  89.     char fn[FILENAME_MAX];
  90.     
  91.     if(argc > 1)
  92.     (void) strcpy(fn, *++argv);
  93.     else
  94.     (void) strcpy(fn, "gcc-cc1.ttp");
  95.     
  96.     if((fd = open(fn, 0)) < 0)
  97.     {
  98.     perror(fn);
  99.     exit(1);
  100.     }
  101.     
  102.     offset = find_offset(fd, fn, &what);
  103.     
  104.     if(lseek(fd, offset, 0) != offset)
  105.     {
  106.     perror(fn);
  107.     exit(7);
  108.     }
  109.     read(fd, &stksize, sizeof(long));
  110.     printf("%s: %s is %ld (%dK)\n",
  111.          fn, sym_names[what] + 1, stksize, (int)(stksize/1024));
  112.     
  113.     return close(fd);
  114. }
  115.  
  116.