BSD 3 development
[unix-history] / usr / src / cmd / strip.c
CommitLineData
ba85e85e
BJ
1#include <a.out.h>
2#include <signal.h>
3#include <pagsiz.h>
4
5#define BUFSIZ BSIZE
6
7char *tname;
8char *mktemp();
9struct exec head;
10int a_magic[] = {A_MAGIC1, A_MAGIC2, A_MAGIC3, A_MAGIC4, 0412, 0413, 0};
11int status;
12int tf;
13
14main(argc, argv)
15char *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
39strip(name)
40char *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 for(i=0;a_magic[i];i++)
54 if(a_magic[i] == head.a_magic) break;
55 if(a_magic[i] == 0) {
56 printf("%s not in a.out format\n", name);
57 status = 1;
58 goto out;
59 }
60 if ((head.a_syms == 0) && (head.a_trsize == 0) && (head.a_drsize ==0)) {
61 printf("%s already stripped\n", name);
62 goto out;
63 }
64 size = (long)head.a_text + head.a_data;
65 head.a_syms = head.a_trsize = head.a_drsize = 0 ;
66
67 lseek(tf, (long)0, 0);
68 write(tf, (char *)&head, sizeof(head));
69 if (head.a_magic == 0412 || head.a_magic == 0413) {
70 size += PAGSIZ - sizeof (head);
71 }
72 if(copy(name, f, tf, size)) {
73 status = 1;
74 goto out;
75 }
76 size += sizeof(head);
77 close(f);
78 f = creat(name, 0666);
79 if(f < 0) {
80 printf("%s cannot recreate\n", name);
81 status = 1;
82 goto out;
83 }
84 lseek(tf, (long)0, 0);
85 if(copy(name, tf, f, size))
86 status = 2;
87
88out:
89 close(f);
90}
91
92copy(name, fr, to, size)
93char *name;
94long size;
95{
96 register s, n;
97 char buf[BUFSIZ];
98
99 while(size != 0) {
100 s = BUFSIZ;
101 if(size < BUFSIZ)
102 s = size;
103 n = read(fr, buf, s);
104 if(n != s) {
105 printf("%s unexpected eof\n", name);
106 return(1);
107 }
108 n = write(to, buf, s);
109 if(n != s) {
110 printf("%s unexpected write eof\n", name);
111 return(1);
112 }
113 size -= s;
114 }
115 return(0);
116}