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