1: static char *
   2: RcsId = "$Header: /usr/src/local/RCS/buildcore.c,v 1.2 83/04/26 21:24:33 crl Exp $";
   3: 
   4: /*
   5:  * buildcore - rebuild an executable file from a core image
   6:  */
   7: 
   8: #include <stdio.h>
   9: #include <a.out.h>
  10: #include <sys/param.h>
  11: #include <sys/stat.h>
  12: #undef DIRSIZ
  13: #include <dir.h>
  14: #include <sys/user.h>
  15: 
  16: char    *efilename;     /* executable filename */
  17: char    *cfilename = "core";    /* core filename */
  18: char    *afilename = "a.out";   /* new executable file */
  19: FILE    *efile;
  20: FILE    *cfile;
  21: FILE    *afile;         /* a.out */
  22: char    *progname;      /* our name */
  23: unsigned olddata;       /* size of data in executable */
  24: struct user u;          /* copy of user structure */
  25: struct exec e;          /* a.out header */
  26: struct stat sbuf;       /* for stat */
  27: struct ovlhdr overlay;      /* overlay header */
  28: 
  29: char    *rindex();
  30: FILE    *fopen();
  31: 
  32: main(argc, argv)
  33: int argc;
  34: char *argv[];
  35: {
  36:     register unsigned i;
  37:     register unsigned j;
  38:     register c;
  39: 
  40:     if ((progname = rindex(argv[0], '/')) == NULL)
  41:         progname = argv[0];
  42:     else
  43:         progname++;
  44: 
  45:     if (argc != 2) {
  46:         fprintf(stderr, "usage: %s file\n", progname);
  47:         exit(1);
  48:     }
  49:     efilename = argv[1];
  50:     if ((efile = fopen(efilename, "r")) == NULL) {
  51:         fprintf(stderr, "%s: can't read %s\n", progname, efilename);
  52:         exit(1);
  53:     }
  54:     if ((cfile = fopen(cfilename, "r")) == NULL) {
  55:         fprintf(stderr, "%s: can't read %s\n", progname, cfilename);
  56:         exit(1);
  57:     }
  58:     if ((afile = fopen(afilename, "w")) == NULL) {
  59:         fprintf(stderr, "%s: can't write %s\n", progname, afilename);
  60:         exit(1);
  61:     }
  62:     if (fread(&u, sizeof u, 1, cfile) != 1) {
  63:         fprintf(stderr, "%s: error reading user structure\n",
  64:                    progname);
  65:         exit(1);
  66:     }
  67:     fseek(cfile, (long)ctob(USIZE), 0);   /* seek past all of user area */
  68:     if (fread(&e, sizeof e, 1, efile) != 1) {
  69:         fprintf(stderr, "%s: error reading old exec header\n",
  70:                     progname);
  71:         exit(1);
  72:     }
  73: 
  74:     /*
  75: 	 * check for a support magic number
  76: 	 */
  77:     switch(e.a_magic) {
  78:       case A_MAGIC1:        /* normal */
  79:       case A_MAGIC4:        /* manual overlay */
  80:       case A_MAGIC5:        /* non-sep auto overlay */
  81:         fprintf(stderr, "%s: unsupported executable type %o\n",
  82:                     progname, e.a_magic);
  83:         exit(1);
  84:     }
  85:     /*
  86: 	 * build the a.out header
  87: 	 */
  88:     olddata = e.a_data;     /* save it for an offset */
  89:     e.a_data = ctob(u.u_dsize); /* get new data size from core */
  90:     e.a_bss = 0;            /* can't use bss anymore */
  91: 
  92:     if (fwrite(&e, sizeof e, 1, afile) != 1) {
  93:         fprintf(stderr, "%s: error in writing %s header\n",
  94:                     progname, afilename);
  95:         exit(1);
  96:     }
  97:     /*
  98: 	 * write out overlay header if overlayed
  99: 	 */
 100:     if (e.a_magic == A_MAGIC6) {
 101:         if (fread(&overlay, sizeof overlay, 1, efile) != 1) {
 102:             fprintf(stderr, "%s: error reading overlay header\n",
 103:                             progname);
 104:             exit(1);
 105:         }
 106:         if (fwrite(&overlay, sizeof overlay, 1, afile) != 1) {
 107:             fprintf(stderr, "%s: error writing overlay header\n",
 108:                             progname);
 109:             exit(1);
 110:         }
 111:     }
 112:     /*
 113: 	 * now write out the base text segment
 114: 	 */
 115:     i = e.a_text;
 116:     if (i) do {
 117:         if ((c = getc(efile)) == EOF) {
 118:             fprintf(stderr, "%s: unexpected EOF reading base text segment\n", progname);
 119:             exit(1);
 120:         }
 121:         putc(c, afile);
 122:     } while (--i);
 123:     /*
 124: 	 * write out overlays
 125: 	 */
 126:     if (e.a_magic == A_MAGIC6) {
 127:         for (i = 0; overlay.ov_siz[i] != 0 && i < NOVL; i++) {
 128:             j = overlay.ov_siz[i];
 129:             do {
 130:                 if ((c = getc(efile)) == EOF) {
 131:                 fprintf(stderr, "%s: unexpected EOF reading overlays \n", progname);
 132:                 exit(1);
 133:                 }
 134:                 putc(c, afile);
 135:             } while (--j);
 136:         }
 137:     }
 138:     /*
 139: 	 * and the new data segment
 140: 	 */
 141:     i = ctob(u.u_dsize);
 142:     if (i) do {
 143:         if ((c = getc(cfile)) == EOF) {
 144:             fprintf(stderr, "%s: unexpected EOF reading core data, i = %d\n", progname, i);
 145:             exit(1);
 146:         }
 147:         putc(c, afile);
 148:     } while (--i);
 149:     /*
 150: 	 * skip over the data segment in the executable
 151: 	 * and copy the rest of the file to the new a.out
 152: 	 */
 153:     fseek(efile, (long) olddata, 1);
 154:     while((c = getc(efile)) != EOF)
 155:         putc(c, afile);
 156:     /*
 157: 	 * make the new file executable
 158: 	 */
 159:     fstat(fileno(afile), &sbuf);
 160:     if ((sbuf.st_mode & 0111) == 0) {
 161:         sbuf.st_mode |= 0111;
 162:         chmod(afilename, sbuf.st_mode);
 163:     }
 164: }

Defined functions

main defined in line 32; never used

Defined variables

RcsId defined in line 2; never used
afilename defined in line 18; used 4 times
cfilename defined in line 17; used 2 times
e defined in line 25; used 12 times
efilename defined in line 16; used 3 times
olddata defined in line 23; used 2 times
overlay defined in line 27; used 6 times
progname defined in line 22; used 16 times
sbuf defined in line 26; used 4 times
u defined in line 24; used 4 times
Last modified: 1970-01-01
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3925
Valid CSS Valid XHTML 1.0 Strict