home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / emacs / emacs1857 / src_d2.zoo / source / utils / fixstk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-02  |  3.0 KB  |  163 lines

  1. /*
  2.  * utility to adjust _stksize in gcc-cc1.ttp
  3.  *
  4.  *    Usage: fixstk size [<filename>]
  5.  *      size:    specified as # of bytes         nnn
  6.  *        specified as # of Kilo Bytes        nnnK
  7.  *        specified as # of Mega Bytes        nnnM
  8.  *      filename:
  9.  *        optional, defaults to \.gcc-cc1.ttp
  10.  *
  11.  *    ++jrb
  12.  *
  13.  *      modified to allow fixing applications for which size of a stack
  14.  *      is defined via _initial_stack -- mj
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unixlib.h>
  20. #include <string.h>
  21. #include <st-out.h>
  22.  
  23. long lseek(int, long, int);
  24. static char *sym_names[] = { "__stksize", "__initial_stack" };
  25.  
  26. long find_offset (fd, fn, what)
  27. int fd;
  28. char *fn;
  29. int *what;
  30. {
  31.     struct aexec head;
  32.     struct asym  sym;
  33.     int found;
  34.     int    all = 0;
  35.     int index = 1;
  36.     
  37.     if(read(fd, &head, sizeof(head)) != sizeof(head))
  38.     {
  39.     perror(fn);
  40.     exit(2);
  41.     }
  42.     if(head.a_magic != CMAGIC)
  43.     {
  44.     fprintf(stderr,"Invalid magic number %x\n", head.a_magic);
  45.     exit(3);
  46.     }
  47.     if(head.a_syms == 0)
  48.     {
  49.     fprintf(stderr,"%s: no symbol table\n", fn);
  50.     exit(4);
  51.     }
  52.     if(lseek(fd, head.a_text+head.a_data, 1) != 
  53.        (head.a_text+head.a_data+sizeof(head)))
  54.     {
  55.     perror(fn);
  56.     exit(5);
  57.     }
  58.     for(;;)
  59.     {
  60.     if(index && (read(fd, &sym, sizeof(sym)) != sizeof(sym)))
  61.     {
  62.         fprintf(stderr, "symbol _stksize not found\n");
  63.         exit(6);
  64.     }
  65.     /* after symbol read check first for _stksize */
  66.     index ^= 1;
  67.     if (strncmp(sym_names[index], sym.a_name, 8) == 0)
  68.     {
  69.         if ((found = (sym.a_type & A_DATA)) || all++)
  70.         break;
  71.     }
  72.     }
  73.     
  74.     if(!found)
  75.     {
  76.     fprintf(stderr, "symbol _stksize is undefined\n");
  77.     exit(9);
  78.     }
  79.     *what = index;
  80.     return sym.a_value + sizeof(head);
  81. }
  82.  
  83. long calc_newsize(s)
  84. char *s;
  85. {
  86.     size_t len = strlen(s) - 1;
  87.     long mul = 1;
  88.     long atol(const char *);
  89.  
  90.     switch(s[len])
  91.     {
  92.       case 'k': case 'K':
  93.     mul = 1L << 10;
  94.     break;
  95.       case 'm': case 'M':
  96.     mul = 1L << 20;
  97.     break;
  98.       default:
  99.     len += 1;
  100.     }
  101.     
  102.     s[len] = '\0';
  103.     return mul * atol(s);
  104. }
  105.  
  106. int main(argc, argv)
  107. int argc;
  108. char **argv;
  109. {
  110.     int fd;
  111.     int what;
  112.     long newstksize, stksize, offset;
  113.     char sizestr[16], fn[FILENAME_MAX];
  114.     
  115.     if(argc < 2)
  116.     {
  117.     fprintf(stderr, "usage: fixstk size [<filename>]\n");
  118.     exit(1);
  119.     }
  120.  
  121.     strcpy(sizestr, *++argv);
  122.  
  123.     if(argc > 2)
  124.     (void) strcpy(fn, *++argv);
  125.     else
  126.     (void) strcpy(fn, "gcc-cc1.ttp");
  127.  
  128.     if((fd = open(fn, 2)) < 0)
  129.     {
  130.     perror(fn);
  131.     exit(1);
  132.     }
  133.     
  134.     newstksize = calc_newsize(sizestr);
  135.     offset = find_offset(fd, fn, &what);
  136.     if(lseek(fd, offset, 0) != offset)
  137.     {
  138.     perror(fn);
  139.     close(fd);
  140.     exit(7);
  141.     }
  142.     read(fd, &stksize, sizeof(long));
  143.     printf("%s: %s was %ld (%dK)\n",
  144.          fn, sym_names[what] + 1, stksize, (int)(stksize/1024));
  145.     
  146.     lseek(fd, -((long)sizeof(long)), 1);
  147.     
  148.     if(write(fd, &newstksize, sizeof(long)) != sizeof(long))
  149.     {
  150.     perror(fn);
  151.     close(fd);
  152.     exit(8);
  153.     }
  154.     
  155.     lseek(fd, -((long)sizeof(long)), 1);
  156.     
  157.     read(fd, &stksize, sizeof(long));
  158.     printf("%s: %s now is %ld (%dK)\n",
  159.          fn, sym_names[what] + 1, stksize, (int)(stksize/1024));
  160.     return close(fd);
  161. }
  162.  
  163.