home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / utils / ixstack.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  2KB  |  98 lines

  1. #include <sys/fcntl.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <sys/mman.h>
  5. #include <sys/stat.h>
  6.  
  7. void setstack(long size, char *filename)
  8. {
  9.   int fd;
  10.   int len, i;
  11.   caddr_t addr;
  12.   struct stat s;
  13.   static int printed_header = 0;
  14.   
  15.   if (stat(filename, &s) == -1 || !S_ISREG(s.st_mode))
  16.   {
  17.     close(fd);
  18.     return;
  19.   }
  20.   fd = open(filename, O_RDWR);
  21.   if (fd == -1)
  22.   {
  23.     perror(filename);
  24.     return;
  25.   }
  26.   len = lseek(fd, 0, SEEK_END);
  27.   addr = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
  28.   if (fd == -1)
  29.   {
  30.     perror(filename);
  31.     close(fd);
  32.     return;
  33.   }
  34.   for (i = 0; i < len - 12; i += 2)
  35.     if (*(long *)(addr + i) == 'StCk' && *(long *)(addr + i + 8) == 'sTcK')
  36.     {
  37.       if (size >= 0)
  38.       {
  39.         lseek(fd, i + 4, SEEK_SET);
  40.         write(fd, &size, 4);
  41.       }
  42.       else
  43.       {
  44.         if (!printed_header)
  45.         {
  46.           printed_header = 1;
  47.           printf("stacksize  filename\n---------  --------\n");
  48.         }
  49.         printf("%9d  %s\n", *(long *)(addr + i + 4), filename);
  50.       }
  51.       munmap(addr, len);
  52.       close(fd);
  53.       return;
  54.     }
  55.   close(fd);
  56.   if (size)
  57.     printf("cannot set stack: %s\n", filename);
  58. }
  59.  
  60. main(int argc, char **argv)
  61. {
  62.   long size;
  63.  
  64.   if (argc < 3)
  65.   {
  66.     fprintf(stderr, "set stacksize:  ixstack <stacksize> <files ...>
  67. show stacksize: ixstack -l <files ...>\n");
  68.     exit(1);
  69.   }
  70.   if (!strcmp(argv[1], "-l"))
  71.     size = -1;
  72.   else
  73.   {
  74.     int i;
  75.     
  76.     for (i = 0; argv[1][i]; i++)
  77.       if (!isdigit(argv[1][i]))
  78.       {
  79.         fprintf(stderr, "stacksize %s is not a number\n", argv[1]);
  80.         exit(1);
  81.       }
  82.     size = atol(argv[1]);
  83.     if (size < 4000 && size)
  84.     {
  85.       fprintf(stderr, "stacksize must be at least 4000 bytes\n");
  86.       exit(1);
  87.     }
  88.     if (size && (size & 3))
  89.     {
  90.       fprintf(stderr, "stacksize must be a multiple of 4\n");
  91.       exit(1);
  92.     }
  93.   }
  94.   argv += 2;
  95.   while (*argv)
  96.     setstack(size, *argv++);
  97. }
  98.