1: #include <whoami.h> 2: #include <sys/param.h> 3: #include <a.out.h> 4: #include <signal.h> 5: #include <sys/file.h> 6: #include <sysexits.h> 7: 8: char *mktemp(); 9: int errs; 10: struct exec exec, nexec; 11: #ifdef MENLO_OVLY 12: struct ovlhdr ovlhdr, novlhdr; 13: #endif 14: #ifdef MENLO_OVLY 15: #define ISOVERLAID(x) (((x).a_magic == A_MAGIC5) || ((x).a_magic == A_MAGIC6)) 16: #endif 17: #define ISSTRIPPED(x) (((x).a_syms == 0) && (((x).a_flag & 1) != 0)) 18: 19: main(ac, av) 20: char **av; 21: { 22: int nflag = 0; 23: char *tname; 24: register i; 25: register in, out; 26: off_t filesize, ovlsizes, rellen, reloffset; 27: 28: if (--ac > 1 && !strcmp(av[1], "-n")) { 29: nflag++; 30: av++; 31: } 32: 33: signal(SIGHUP, SIG_IGN); 34: signal(SIGINT, SIG_IGN); 35: signal(SIGQUIT, SIG_IGN); 36: tname = mktemp("/tmp/sXXXXXX"); 37: while (*++av) { 38: rellen = (off_t) 0; 39: reloffset = (off_t) 0; 40: ovlsizes = (off_t) 0; 41: filesize = (off_t) 0; 42: if ((out = creat(tname, 0600)) < 0) { 43: printf("Cannot create temporary file for %s\n", *av); 44: exit(EX_TEMPFAIL); 45: } 46: if ((in = open(*av, FATT_RDONLY)) < 0) { 47: perror(*av); 48: errs++; 49: goto err; 50: } 51: if (read(in, (char *) &exec, sizeof (struct exec)) != sizeof (struct exec)) { 52: printf("%s: not in a.out format\n", *av); 53: errs++; 54: goto err; 55: } 56: nexec = exec; 57: if (N_BADMAG(exec)) { 58: printf("%s: not in a.out format\n", *av); 59: errs++; 60: goto err; 61: } 62: if(ISSTRIPPED(exec)) { 63: printf("%s: already stripped\n", *av); 64: errs++; 65: goto err; 66: } 67: if (nflag) { 68: nexec.a_text = 0; 69: nexec.a_data = 0; 70: nexec.a_bss = 0; 71: } 72: else { 73: nexec.a_syms = 0; 74: nexec.a_flag |= 1; 75: } 76: if (copyout(*av, out, (char *) &nexec, sizeof (struct exec)) < 0) 77: goto err; 78: else 79: filesize += (off_t) sizeof (struct exec); 80: #ifdef MENLO_OVLY 81: if (ISOVERLAID(nexec)) { 82: if (read(in, (char *) &ovlhdr, sizeof (ovlhdr)) != sizeof (ovlhdr)) { 83: perror(*av); 84: errs++; 85: goto err; 86: } 87: if (nflag) { 88: novlhdr.max_ovl = 0; 89: for (i = 0; i < NOVL; i++) 90: novlhdr.ov_siz[i] = 0; 91: } 92: else 93: novlhdr = ovlhdr; 94: if (copyout(*av, out, (char *) &novlhdr, sizeof (struct ovlhdr)) < 0) 95: goto err; 96: else 97: filesize += (off_t) sizeof (struct ovlhdr); 98: } 99: #endif 100: if (!nflag) { 101: if (copy(*av, in, out, (off_t) exec.a_text) < 0) 102: goto err; 103: else 104: filesize += (off_t) exec.a_text; 105: #ifdef MENLO_OVLY 106: if (ISOVERLAID(nexec)) 107: for (i = 0; i < NOVL; i++) 108: ovlsizes += ovlhdr.ov_siz[i]; 109: if (copy(*av, in, out, ovlsizes) < 0) 110: goto err; 111: else 112: filesize += ovlsizes; 113: #endif 114: if (copy(*av, in, out, (off_t) exec.a_data) < 0) 115: goto err; 116: else 117: filesize += (off_t) exec.a_data; 118: } 119: else 120: { 121: reloffset = N_TXTOFF(exec) + exec.a_text + exec.a_data; 122: #ifdef MENLO_JCL 123: if (ISOVERLAID(nexec)) 124: for (i = 0; i < NOVL; i++) 125: reloffset += (off_t) ovlhdr.ov_siz[i]; 126: #endif 127: if (exec.a_flag & 1) 128: rellen = (off_t) 0; 129: else 130: rellen = (off_t) exec.a_text + (off_t) exec.a_data; 131: lseek(in, reloffset + rellen, FSEEK_ABSOLUTE); 132: if (copy(*av, in, out, (off_t) exec.a_syms) < 0) 133: goto err; 134: else 135: filesize += (off_t) exec.a_syms; 136: } 137: (void) close(in); 138: (void) close(out); 139: if ((out = creat(*av, 0666)) < 0) { 140: printf("%s: cannot recreate\n", *av); 141: errs++; 142: goto err; 143: } 144: if ((in = open(tname, FATT_RDONLY)) < 0) { 145: perror(tname); 146: errs++; 147: goto err; 148: } 149: else 150: if (copy(*av, in, out, filesize) < 0) { 151: printf("I/o error in rewriting %s\n", *av); 152: errs++; 153: goto err; 154: } 155: err: 156: (void) close(in); 157: (void) close(out); 158: (void) unlink(tname); 159: } 160: exit(errs); 161: } 162: 163: copy(name, fromfd, tofd, size) 164: char *name; 165: off_t size; 166: { 167: register s, n; 168: char buf[BSIZE]; 169: 170: while (size) { 171: s = sizeof (buf); 172: if (size < (off_t) sizeof (buf)) 173: s = (int) size; 174: n = read(fromfd, buf, s); 175: if (n != s) { 176: printf("%s: unexpected eof\n", name); 177: errs++; 178: return (-1); 179: } 180: n = write(tofd, buf, (u_short) s); 181: if (n != s) { 182: perror(name); 183: errs++; 184: return (-1); 185: } 186: size -= (off_t) s; 187: } 188: return(0); 189: } 190: 191: copyout (name, tofd, buf, nbytes) 192: char *name, *buf; 193: u_short nbytes; 194: { 195: if (write(tofd, buf, nbytes) != nbytes) { 196: perror(name); 197: errs++; 198: return (-1); 199: } 200: else 201: return (0); 202: }