Commit | Line | Data |
---|---|---|
cb7bf434 | 1 | static char *sccsid = "@(#)strip.c 4.2 (Berkeley) %G%"; |
b261d7a4 BJ |
2 | #include <a.out.h> |
3 | #include <signal.h> | |
4 | #include <pagsiz.h> | |
5 | ||
cb7bf434 | 6 | #define BUFSIZ 1024 |
b261d7a4 BJ |
7 | |
8 | char *tname; | |
9 | char *mktemp(); | |
10 | struct exec head; | |
11 | int status; | |
12 | int tf; | |
13 | ||
14 | main(argc, argv) | |
15 | char *argv[]; | |
16 | { | |
17 | register i; | |
18 | ||
19 | signal(SIGHUP, SIG_IGN); | |
20 | signal(SIGINT, SIG_IGN); | |
21 | signal(SIGQUIT, SIG_IGN); | |
22 | tname = mktemp("/tmp/sXXXXX"); | |
23 | close(creat(tname, 0600)); | |
24 | tf = open(tname, 2); | |
25 | if(tf < 0) { | |
26 | printf("cannot create temp file\n"); | |
27 | exit(2); | |
28 | } | |
29 | for(i=1; i<argc; i++) { | |
30 | strip(argv[i]); | |
31 | if(status > 1) | |
32 | break; | |
33 | } | |
34 | close(tf); | |
35 | unlink(tname); | |
36 | exit(status); | |
37 | } | |
38 | ||
39 | strip(name) | |
40 | char *name; | |
41 | { | |
42 | register f; | |
43 | long size; | |
44 | int i; | |
45 | ||
46 | f = open(name, 0); | |
47 | if(f < 0) { | |
48 | printf("cannot open %s\n", name); | |
49 | status = 1; | |
50 | goto out; | |
51 | } | |
52 | read(f, (char *)&head, sizeof(head)); | |
53 | if (N_BADMAG(head)) { | |
54 | printf("%s not in a.out format\n", name); | |
55 | status = 1; | |
56 | goto out; | |
57 | } | |
58 | if ((head.a_syms == 0) && (head.a_trsize == 0) && (head.a_drsize ==0)) { | |
59 | printf("%s already stripped\n", name); | |
60 | goto out; | |
61 | } | |
62 | size = (long)head.a_text + head.a_data; | |
63 | head.a_syms = head.a_trsize = head.a_drsize = 0 ; | |
64 | lseek(tf, (long)0, 0); | |
65 | write(tf, (char *)&head, sizeof(head)); | |
66 | if (head.a_magic == ZMAGIC) | |
67 | size += PAGSIZ - sizeof (head); | |
68 | if (copy(name, f, tf, size)) { | |
69 | status = 1; | |
70 | goto out; | |
71 | } | |
72 | size += sizeof(head); | |
73 | close(f); | |
74 | f = creat(name, 0666); | |
75 | if(f < 0) { | |
76 | printf("%s cannot recreate\n", name); | |
77 | status = 1; | |
78 | goto out; | |
79 | } | |
80 | lseek(tf, (long)0, 0); | |
81 | if(copy(name, tf, f, size)) | |
82 | status = 2; | |
83 | ||
84 | out: | |
85 | close(f); | |
86 | } | |
87 | ||
88 | copy(name, fr, to, size) | |
89 | char *name; | |
90 | long size; | |
91 | { | |
92 | register s, n; | |
93 | char buf[BUFSIZ]; | |
94 | ||
95 | while(size != 0) { | |
96 | s = BUFSIZ; | |
97 | if(size < BUFSIZ) | |
98 | s = size; | |
99 | n = read(fr, buf, s); | |
100 | if(n != s) { | |
101 | printf("%s unexpected eof\n", name); | |
102 | return(1); | |
103 | } | |
104 | n = write(to, buf, s); | |
105 | if(n != s) { | |
106 | printf("%s unexpected write eof\n", name); | |
107 | return(1); | |
108 | } | |
109 | size -= s; | |
110 | } | |
111 | return(0); | |
112 | } |