add Berkeley copyright
[unix-history] / usr / src / usr.bin / strip / strip.c
CommitLineData
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
8char 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
7d0fd0d4 14static char sccsid[] = "@(#)strip.c 5.2 (Berkeley) %G%";
bcf1365c 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 22struct exec head;
b261d7a4 23int status;
7ea0db89 24int pagesize;
b261d7a4
BJ
25
26main(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
43strip(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 68 if (ftruncate(f, size + sizeof (head)) < 0) {
7d0fd0d4 69 fputs("strip: ", stderr); 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
75out:
76 close(f);
77}