home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / util_src / fixstk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-29  |  2.6 KB  |  144 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.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <unixlib.h>
  17. #include <string.h>
  18. #include <st-out.h>
  19.  
  20. long lseek(int, long, int);
  21.  
  22. long find_offset (fd, fn)
  23. int fd;
  24. char *fn;
  25. {
  26.     struct aexec head;
  27.     struct asym  sym;
  28.     
  29.     if(read(fd, &head, sizeof(head)) != sizeof(head))
  30.     {
  31.     perror(fn);
  32.     exit(2);
  33.     }
  34.     if(head.a_magic != CMAGIC)
  35.     {
  36.     fprintf(stderr,"Invalid magic number %x\n", head.a_magic);
  37.     exit(3);
  38.     }
  39.     if(head.a_syms == 0)
  40.     {
  41.     fprintf(stderr,"%s: no symbol table\n", fn);
  42.     exit(4);
  43.     }
  44.     if(lseek(fd, head.a_text+head.a_data, 1) != 
  45.        (head.a_text+head.a_data+sizeof(head)))
  46.     {
  47.     perror(fn);
  48.     exit(5);
  49.     }
  50.     do {
  51.     if(read(fd, &sym, sizeof(sym)) != sizeof(sym))
  52.     {
  53.         fprintf(stderr, "symbol _stksize not found\n");
  54.         exit(6);
  55.     }
  56.     } while(strncmp("__stksiz", sym.a_name, 8) != 0);
  57.     
  58.     if( !(sym.a_type & A_DATA) )
  59.     {
  60.     fprintf(stderr, "symbol _stksize is undefined\n");
  61.     exit(9);
  62.     }
  63.     return sym.a_value + sizeof(head);
  64. }
  65.  
  66. long calc_newsize(s)
  67. char *s;
  68. {
  69.     size_t len = strlen(s) - 1;
  70.     long mul = 1;
  71.     long atol(const char *);
  72.  
  73.     switch(s[len])
  74.     {
  75.       case 'k': case 'K':
  76.     mul = 1L << 10;
  77.     break;
  78.       case 'm': case 'M':
  79.     mul = 1L << 20;
  80.     break;
  81.       default:
  82.     len += 1;
  83.     }
  84.     
  85.     s[len] = '\0';
  86.     return mul * atol(s);
  87. }
  88.  
  89. int main(argc, argv)
  90. int argc;
  91. char **argv;
  92. {
  93.     int fd;
  94.     long newstksize, stksize, offset;
  95.     char sizestr[16], fn[FILENAME_MAX];
  96.     
  97.     if(argc < 2)
  98.     {
  99.     fprintf(stderr, "usage: fixstk size [<filename>]\n");
  100.     exit(1);
  101.     }
  102.  
  103.     strcpy(sizestr, *++argv);
  104.  
  105.     if(argc > 2)
  106.     (void) strcpy(fn, *++argv);
  107.     else
  108.     (void) strcpy(fn, "gcc-cc1.ttp");
  109.  
  110.     if((fd = open(fn, 2)) < 0)
  111.     {
  112.     perror(fn);
  113.     exit(1);
  114.     }
  115.     
  116.     newstksize = calc_newsize(sizestr);
  117.     offset = find_offset(fd, fn);
  118.     if(lseek(fd, offset, 0) != offset)
  119.     {
  120.     perror(fn);
  121.     close(fd);
  122.     exit(7);
  123.     }
  124.     read(fd, &stksize, sizeof(long));
  125.     printf("%s: stksize was %ld (%dK)\n", fn, stksize, (int)(stksize/1024));
  126.     
  127.     lseek(fd, -((long)sizeof(long)), 1);
  128.     
  129.     if(write(fd, &newstksize, sizeof(long)) != sizeof(long))
  130.     {
  131.     perror(fn);
  132.     close(fd);
  133.     exit(8);
  134.     }
  135.     
  136.     lseek(fd, -((long)sizeof(long)), 1);
  137.     
  138.     read(fd, &stksize, sizeof(long));
  139.     printf("%s: stksize now is %ld (%dK)\n", fn, stksize,
  140.        (int)(stksize/1024));
  141.     return close(fd);
  142. }
  143.  
  144.