Commit | Line | Data |
---|---|---|
bcf1365c DF |
1 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
7 | #ifndef lint | |
8 | char copyright[] = | |
9 | "@(#) Copyright (c) 1983 Regents of the University of California.\n\ | |
10 | All rights reserved.\n"; | |
11 | #endif not lint | |
12 | ||
7ea0db89 | 13 | #ifndef lint |
bcf1365c DF |
14 | static char sccsid[] = "@(#)strip.c 5.1 (Berkeley) %G%"; |
15 | #endif not lint | |
7ea0db89 | 16 | |
b261d7a4 BJ |
17 | #include <a.out.h> |
18 | #include <signal.h> | |
0801d743 SL |
19 | #include <stdio.h> |
20 | #include <sys/file.h> | |
b261d7a4 | 21 | |
0801d743 | 22 | struct exec head; |
b261d7a4 | 23 | int status; |
7ea0db89 | 24 | int pagesize; |
b261d7a4 BJ |
25 | |
26 | main(argc, argv) | |
0801d743 | 27 | char *argv[]; |
b261d7a4 BJ |
28 | { |
29 | register i; | |
30 | ||
7ea0db89 | 31 | pagesize = getpagesize(); |
b261d7a4 BJ |
32 | signal(SIGHUP, SIG_IGN); |
33 | signal(SIGINT, SIG_IGN); | |
34 | signal(SIGQUIT, SIG_IGN); | |
0801d743 | 35 | for (i = 1; i < argc; i++) { |
b261d7a4 | 36 | strip(argv[i]); |
0801d743 | 37 | if (status > 1) |
b261d7a4 BJ |
38 | break; |
39 | } | |
b261d7a4 BJ |
40 | exit(status); |
41 | } | |
42 | ||
43 | strip(name) | |
0801d743 | 44 | char *name; |
b261d7a4 BJ |
45 | { |
46 | register f; | |
47 | long size; | |
b261d7a4 | 48 | |
0801d743 SL |
49 | f = open(name, O_RDWR); |
50 | if (f < 0) { | |
51 | fprintf(stderr, "strip: "); perror(name); | |
b261d7a4 BJ |
52 | status = 1; |
53 | goto out; | |
54 | } | |
0801d743 SL |
55 | if (read(f, (char *)&head, sizeof (head)) < 0 || N_BADMAG(head)) { |
56 | printf("strip: %s not in a.out format\n", name); | |
b261d7a4 BJ |
57 | status = 1; |
58 | goto out; | |
59 | } | |
60 | if ((head.a_syms == 0) && (head.a_trsize == 0) && (head.a_drsize ==0)) { | |
0801d743 | 61 | printf("strip: %s already stripped\n", name); |
b261d7a4 BJ |
62 | goto out; |
63 | } | |
64 | size = (long)head.a_text + head.a_data; | |
0801d743 | 65 | head.a_syms = head.a_trsize = head.a_drsize = 0; |
b261d7a4 | 66 | if (head.a_magic == ZMAGIC) |
7ea0db89 | 67 | size += pagesize - sizeof (head); |
0801d743 SL |
68 | if (ftruncate(f, size + sizeof (head)) < 0) { |
69 | fprintf("strip: "); perror(name); | |
b261d7a4 BJ |
70 | status = 1; |
71 | goto out; | |
72 | } | |
0801d743 SL |
73 | (void) lseek(f, (long)0, L_SET); |
74 | (void) write(f, (char *)&head, sizeof (head)); | |
b261d7a4 BJ |
75 | out: |
76 | close(f); | |
77 | } |