xref: /original-bsd/usr.bin/strip/strip.c (revision c43e4352)
1 #ifndef lint
2 static char *sccsid = "@(#)strip.c	4.5 (Berkeley) 07/06/83";
3 #endif
4 
5 #include <a.out.h>
6 #include <signal.h>
7 #include <stdio.h>
8 #include <sys/file.h>
9 
10 struct	exec head;
11 int	status;
12 int	pagesize;
13 
14 main(argc, argv)
15 	char *argv[];
16 {
17 	register i;
18 
19 	pagesize = getpagesize();
20 	signal(SIGHUP, SIG_IGN);
21 	signal(SIGINT, SIG_IGN);
22 	signal(SIGQUIT, SIG_IGN);
23 	for (i = 1; i < argc; i++) {
24 		strip(argv[i]);
25 		if (status > 1)
26 			break;
27 	}
28 	exit(status);
29 }
30 
31 strip(name)
32 	char *name;
33 {
34 	register f;
35 	long size;
36 
37 	f = open(name, O_RDWR);
38 	if (f < 0) {
39 		fprintf(stderr, "strip: "); perror(name);
40 		status = 1;
41 		goto out;
42 	}
43 	if (read(f, (char *)&head, sizeof (head)) < 0 || N_BADMAG(head)) {
44 		printf("strip: %s not in a.out format\n", name);
45 		status = 1;
46 		goto out;
47 	}
48 	if ((head.a_syms == 0) && (head.a_trsize == 0) && (head.a_drsize ==0)) {
49 		printf("strip: %s already stripped\n", name);
50 		goto out;
51 	}
52 	size = (long)head.a_text + head.a_data;
53 	head.a_syms = head.a_trsize = head.a_drsize = 0;
54 	if (head.a_magic == ZMAGIC)
55 		size += pagesize - sizeof (head);
56 	if (ftruncate(f, size + sizeof (head)) < 0) {
57 		fprintf("strip: "); perror(name);
58 		status = 1;
59 		goto out;
60 	}
61 	(void) lseek(f, (long)0, L_SET);
62 	(void) write(f, (char *)&head, sizeof (head));
63 out:
64 	close(f);
65 }
66