home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / editors / mntemacs.zoo / utils / fixstk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-29  |  3.1 KB  |  167 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. #ifndef __MINT__
  20. #include <unixlib.h>
  21. #else
  22. #include <unistd.h>
  23. #endif                /* __MINT__ */
  24. #include <string.h>
  25. #include <st-out.h>
  26.  
  27. long lseek(int, long, int);
  28. static char *sym_names[] = { "__stksize", "__initial_stack" };
  29.  
  30. long find_offset (fd, fn, what)
  31. int fd;
  32. char *fn;
  33. int *what;
  34. {
  35.     struct aexec head;
  36.     struct asym  sym;
  37.     int found;
  38.     int    all = 0;
  39.     int index = 1;
  40.     
  41.     if(read(fd, &head, sizeof(head)) != sizeof(head))
  42.     {
  43.     perror(fn);
  44.     exit(2);
  45.     }
  46.     if(head.a_magic != CMAGIC)
  47.     {
  48.     fprintf(stderr,"Invalid magic number %x\n", head.a_magic);
  49.     exit(3);
  50.     }
  51.     if(head.a_syms == 0)
  52.     {
  53.     fprintf(stderr,"%s: no symbol table\n", fn);
  54.     exit(4);
  55.     }
  56.     if(lseek(fd, head.a_text+head.a_data, 1) != 
  57.        (head.a_text+head.a_data+sizeof(head)))
  58.     {
  59.     perror(fn);
  60.     exit(5);
  61.     }
  62.     for(;;)
  63.     {
  64.     if(index && (read(fd, &sym, sizeof(sym)) != sizeof(sym)))
  65.     {
  66.         fprintf(stderr, "symbol _stksize not found\n");
  67.         exit(6);
  68.     }
  69.     /* after symbol read check first for _stksize */
  70.     index ^= 1;
  71.     if (strncmp(sym_names[index], sym.a_name, 8) == 0)
  72.     {
  73.         if ((found = (sym.a_type & A_DATA)) || all++)
  74.         break;
  75.     }
  76.     }
  77.     
  78.     if(!found)
  79.     {
  80.     fprintf(stderr, "symbol _stksize is undefined\n");
  81.     exit(9);
  82.     }
  83.     *what = index;
  84.     return sym.a_value + sizeof(head);
  85. }
  86.  
  87. long calc_newsize(s)
  88. char *s;
  89. {
  90.     size_t len = strlen(s) - 1;
  91.     long mul = 1;
  92.     long atol(const char *);
  93.  
  94.     switch(s[len])
  95.     {
  96.       case 'k': case 'K':
  97.     mul = 1L << 10;
  98.     break;
  99.       case 'm': case 'M':
  100.     mul = 1L << 20;
  101.     break;
  102.       default:
  103.     len += 1;
  104.     }
  105.     
  106.     s[len] = '\0';
  107.     return mul * atol(s);
  108. }
  109.  
  110. int main(argc, argv)
  111. int argc;
  112. char **argv;
  113. {
  114.     int fd;
  115.     int what;
  116.     long newstksize, stksize, offset;
  117.     char sizestr[16], fn[FILENAME_MAX];
  118.     
  119.     if(argc < 2)
  120.     {
  121.     fprintf(stderr, "usage: fixstk size [<filename>]\n");
  122.     exit(1);
  123.     }
  124.  
  125.     strcpy(sizestr, *++argv);
  126.  
  127.     if(argc > 2)
  128.     (void) strcpy(fn, *++argv);
  129.     else
  130.     (void) strcpy(fn, "gcc-cc1.ttp");
  131.  
  132.     if((fd = open(fn, 2)) < 0)
  133.     {
  134.     perror(fn);
  135.     exit(1);
  136.     }
  137.     
  138.     newstksize = calc_newsize(sizestr);
  139.     offset = find_offset(fd, fn, &what);
  140.     if(lseek(fd, offset, 0) != offset)
  141.     {
  142.     perror(fn);
  143.     close(fd);
  144.     exit(7);
  145.     }
  146.     read(fd, &stksize, sizeof(long));
  147.     printf("%s: %s was %ld (%dK)\n",
  148.          fn, sym_names[what] + 1, stksize, (int)(stksize/1024));
  149.     
  150.     lseek(fd, -((long)sizeof(long)), 1);
  151.     
  152.     if(write(fd, &newstksize, sizeof(long)) != sizeof(long))
  153.     {
  154.     perror(fn);
  155.     close(fd);
  156.     exit(8);
  157.     }
  158.     
  159.     lseek(fd, -((long)sizeof(long)), 1);
  160.     
  161.     read(fd, &stksize, sizeof(long));
  162.     printf("%s: %s now is %ld (%dK)\n",
  163.          fn, sym_names[what] + 1, stksize, (int)(stksize/1024));
  164.     return close(fd);
  165. }
  166.  
  167.