xref: /original-bsd/usr.bin/strip/strip.c (revision faf3fd95)
1 /*
2  * Copyright (c) 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1988 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)strip.c	5.5 (Berkeley) 06/01/90";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 #include <sys/file.h>
20 #include <a.out.h>
21 #include <stdio.h>
22 
23 typedef struct exec EXEC;
24 
25 /* ARGSUSED */
26 main(argc, argv)
27 	int argc;
28 	char **argv;
29 {
30 	register off_t fsize;
31 	register int fd, n, pagesize;
32 	EXEC head;
33 	off_t lseek();
34 
35 	pagesize = getpagesize();
36 	while (*++argv) {
37 		if ((fd = open(*argv, O_RDWR)) < 0 ||
38 		    (n = read(fd, (char *)&head, sizeof(EXEC))) == -1)
39 			error(*argv);
40 		if (n != sizeof(EXEC) || N_BADMAG(head)) {
41 			(void)fprintf(stderr,
42 			    "strip: %s not in a.out format.\n", *argv);
43 			exit(1);
44 		}
45 		if (!head.a_syms && !head.a_trsize && !head.a_drsize)
46 			continue;
47 		fsize = head.a_text + head.a_data;
48 		if (head.a_magic == ZMAGIC)
49 			fsize += pagesize - sizeof(EXEC);
50 		head.a_syms = head.a_trsize = head.a_drsize = 0;
51 		if (ftruncate(fd, fsize + sizeof(EXEC)) ||
52 		    lseek(fd, 0L, L_SET) == -1 ||
53 		    write(fd, (char *)&head, sizeof(EXEC)) != sizeof(EXEC))
54 			error(*argv);
55 		(void)close(fd);
56 	}
57 	exit(0);
58 }
59 
60 error(fname)
61 	char *fname;
62 {
63 	extern int errno;
64 	char *strerror();
65 
66 	(void)fprintf(stderr, "strip: %s: %s.\n", fname, strerror(errno));
67 	exit(1);
68 }
69