home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / lisp / utils / tackon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1983-08-22  |  2.5 KB  |  115 lines

  1. #include <stdio.h>
  2. #include "lconf.h"
  3. #include "config.h"
  4. #if ! os_unisoft
  5. #include <sys/types.h>
  6. #include <a.out.h>
  7. /*
  8.  * $Header: /na/franz/utils/RCS/tackon.c,v 1.4 83/08/22 19:01:17 sklower Exp $
  9.  *
  10.  * $Locker:  $
  11.  *
  12.  * This program tacks on extra symbols into the symbol table.
  13.  * someone should write one for system 5.
  14.  *
  15.  */
  16.  
  17. FILE *map;
  18. int aout;
  19. #define NEWSIZ 100000
  20. char newstrb[NEWSIZ];
  21.  
  22. #endif
  23. main(argc, argv)
  24. int argc;
  25. char *argv[];
  26. {
  27. #if ! os_unisoft
  28.     char sym[50], svalue[50];
  29.     char *strb,*newstr,*malloc();
  30.     char *curstr;
  31.     int value;
  32.     int cnt;
  33.     int strsiz;
  34.     int strcnt;
  35.     int size;
  36.     int header_location;
  37.     struct nlist a;
  38.     struct exec e;
  39.  
  40.     argc--, argv++;
  41.     if (argc == 0 || argc > 2) {
  42. usage:
  43.         fprintf(stderr, "usage: tackon map [ a.out ]\n");
  44.         exit(1);
  45.     }
  46.     map = fopen(argv[0], "r");
  47.     if (map == NULL) {
  48.         perror(argv[0]);
  49.         exit(1);
  50.     }
  51.     aout = open(argc == 2 ? argv[1] : "a.out", 2);
  52.     if ((aout < 0) && (argc == 2)) {
  53.         char Name[256];
  54.  
  55.         strcpy(Name,argv[1]);
  56.         strcat(Name,".exe");
  57.         aout = open(Name,2);
  58.     }
  59.     if (aout < 0) {
  60.         printf(" No object file to tackon or text busy\n");
  61.         exit(1);
  62.     }
  63.     header_location = 0;
  64.     read(aout,&e, sizeof(e));
  65.     if (N_BADMAG(e)) {
  66.         header_location = 512;
  67.         lseek(aout,512,0);
  68.         read(aout,&e,sizeof(e));
  69.         if (N_BADMAG(e)) {
  70.             printf("tackon: bad magic number\n");
  71.             exit(0);
  72.         }
  73.     }
  74.     /* read current string table into buffer */
  75.     lseek(aout, N_STROFF(e), 0);    /* seek to string table beginning */
  76.     read(aout,&strsiz,4);        /* read in string table size      */
  77.     strb = malloc(strsiz);
  78.     read(aout,strb,strsiz);        /* read in string table */
  79.     lseek(aout, N_STROFF(e), 0);    /* now write at end of symbols      */
  80.     cnt = 0;
  81.     strcnt = 4 + strsiz;
  82.     curstr = newstrb;        /* point to new string buffer */
  83.     for (;;) {
  84.         if (fgets(sym, 50, map) == NULL)
  85.             break;
  86.         sym[size=strlen(sym)-1] = 0;
  87.         if (fgets(svalue, 50, map) == NULL) {
  88.             fprintf(stderr, "missing value\n");
  89.             break;
  90.         }
  91.         strcpy(curstr,sym);
  92.         sscanf(svalue, "%x", &a.n_value);
  93.         a.n_un.n_strx = strcnt;
  94.         a.n_type = N_EXT|N_TEXT;
  95.         write(aout, &a, sizeof (a));
  96.         curstr += size+1;
  97.         strcnt += size+1;
  98.         cnt++;
  99.         if( curstr >= &newstrb[NEWSIZ])
  100.         {
  101.             printf(" Tackon; string buffer overflow \n");
  102.             exit(1);
  103.         }
  104.     }
  105.     write(aout, &strcnt, 4);    /* new character count */
  106.     write(aout, strb, strsiz);    /* write out old string table */
  107.     write(aout, newstrb, strcnt - ( 4 + strsiz));
  108.     lseek(aout, header_location, 0);
  109.     e.a_syms += cnt*sizeof(struct nlist);
  110.     lseek(aout, header_location, 0);
  111.     write(aout, &e, sizeof (e));
  112.     exit(0);
  113. #endif
  114. }
  115.