home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / util_src / strip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-29  |  3.5 KB  |  175 lines

  1. /*
  2.  *    strip TOS executable format files of symbol table info
  3.  *    usage: strip files ...
  4.  *
  5.  *        ++jrb    bammi@dsrgsun.ces.cwru.edu
  6.  */
  7. #ifdef atarist
  8. #  include <unixlib.h>
  9. #endif
  10.  
  11. #ifdef unix
  12. #  include <strings.h>
  13. #  define lwrite write
  14. #  define lread  read
  15. #else
  16. #  include <string.h>
  17. #endif
  18.  
  19. #include <fcntl.h>
  20.  
  21. #define NULL        ((void *)0)
  22. #define NEWBUFSIZ    16384L
  23.  
  24. char mybuf[NEWBUFSIZ];
  25. char tmpname[128];
  26.  
  27. int main(argc, argv)
  28. int argc;
  29. char **argv;
  30. {
  31.     extern int strip(char *);
  32.     extern void report(char *);
  33.     int status = 0;
  34. #ifdef atarist
  35.     char *tmpdir;
  36.     extern char *getenv(const char *);
  37. #endif
  38.  
  39.     if(argc < 2)
  40.     {
  41.     report("Usage: strip files ...\r\n");
  42.     return 1;
  43.     }
  44.  
  45. #ifdef atarist
  46.     tmpname[0] = '\0';
  47.     if((tmpdir = getenv("TEMP")) != NULL)
  48.     {
  49.     register int l;
  50.     strcpy(tmpname, tmpdir);
  51.     if(tmpname[(l = ((int)strlen(tmpname) - 1))] == '\\')
  52.         tmpname[l] = '\0';
  53.     }
  54.     strcat(tmpname, "\\STXXXXXX");
  55. #else
  56.     strcpy(tmpname,"/tmp/STXXXXXX");
  57. #endif
  58.  
  59.     mktemp(tmpname);
  60.    
  61.     while(--argc > 0)
  62.     status |= strip(*++argv);
  63.  
  64.     unlink(tmpname);
  65.     return status;
  66. }
  67.  
  68. #include <st-out.h>
  69. int strip (char *name)
  70. {
  71.     register int fd;
  72.     register int tfd;
  73.     register long count, sbytes, rbytes;
  74.     struct aexec ahead;
  75.     extern long copy(int, int, long);
  76.     extern void report(char *);
  77.     
  78.     if((fd = open(name, O_RDONLY)) < 0)
  79.     {
  80.     perror(name); return 2;
  81.     }
  82.     if((tfd = open(tmpname, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0)
  83.     {
  84.     perror(tmpname); close(fd); return 4;
  85.     }
  86.     if((count = lread(fd, &ahead, sizeof(ahead))) != sizeof(ahead))
  87.     {
  88.     perror(name);
  89.     close(tfd); close(fd); return 8;
  90.     }
  91.     if(A_BADMAG(ahead))
  92.     {
  93.     report(name); report(": Bad Magic number\r\n");
  94.     close(tfd); close(fd); return 16;
  95.     }
  96.     if((sbytes = ahead.a_syms) == 0)
  97.     {
  98.     report(name); report(": Already Stripped\r\n");
  99.     close(tfd); close(fd); return 4096;
  100.     }
  101.     ahead.a_syms = 0L;
  102.     if((count = lwrite(tfd, &ahead, sizeof(ahead))) != sizeof(ahead))
  103.     {
  104.     perror(tmpname);
  105.     close(tfd); close(fd); return 32;
  106.     }
  107.     count = ahead.a_text + ahead.a_data;
  108.     if(copy(fd, tfd, count) != count)
  109.     {
  110.     close(tfd); close(fd); return 64;
  111.     }
  112.     if(lseek(fd, sbytes , 1) < 0)
  113.     {
  114.     report(name); report(": seek error\r\n");
  115.     close(tfd); close(fd); return 128;
  116.     }
  117.     if((rbytes = copy(fd, tfd, 0x7fffffffL)) < 0)
  118.     {
  119.     close(tfd); close(fd); return 256;
  120.     }
  121.     close(tfd); close(fd);
  122.     if((fd = open(name, O_WRONLY | O_TRUNC)) < 0)
  123.     {
  124.     perror(name); return 512;
  125.     }
  126.     if((tfd = open(tmpname, O_RDONLY)) < 0)
  127.     {
  128.     perror(tmpname); close(fd); return 1024;
  129.     }
  130.     
  131.     count = sizeof(ahead) + ahead.a_text + ahead.a_data + rbytes;
  132.     if(copy(tfd, fd, count) != count)
  133.     {
  134.     close(tfd); close(fd); return 2048;
  135.     }
  136.     close(tfd); close(fd);
  137.     return 0;
  138. }
  139.  
  140. /*
  141.  * copy from, to in NEWBUFSIZ chunks upto bytes or EOF whichever occurs first
  142.  * returns # of bytes copied
  143.  */
  144. long copy(int from, int to, long bytes)
  145. {
  146.     register long todo, done = 0L, remaining = bytes, actual;
  147.     extern void report(char *);
  148.  
  149.     while(done != bytes)
  150.     {
  151.     todo = (remaining > NEWBUFSIZ)? NEWBUFSIZ : remaining;
  152.     if((actual = lread(from, mybuf, todo)) != todo)
  153.     {
  154.         if(actual < 0)
  155.         {
  156.         report("Error Reading\r\n");  return -done;
  157.         }
  158.     }
  159.     if(lwrite(to, mybuf, actual) != actual)
  160.     {
  161.         report("Error Writing\r\n"); return -done;
  162.     }
  163.         done += actual;
  164.     if(actual != todo) /* eof reached */
  165.         return done;
  166.     remaining -= actual;
  167.     }
  168.     return done;
  169. }
  170.  
  171. void report(char *s)
  172. {
  173.     lwrite(2, s, (long)strlen(s));
  174. }
  175.