xref: /original-bsd/usr.bin/strip/strip.c (revision 2e5c0888)
1 /*
2  * Copyright (c) 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1988 Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)strip.c	5.4 (Berkeley) 12/09/89";
26 #endif /* not lint */
27 
28 #include <sys/types.h>
29 #include <sys/file.h>
30 #include <a.out.h>
31 #include <stdio.h>
32 
33 typedef struct exec EXEC;
34 
35 /* ARGSUSED */
36 main(argc, argv)
37 	int argc;
38 	char **argv;
39 {
40 	register off_t fsize;
41 	register int fd, n, pagesize;
42 	EXEC head;
43 	off_t lseek();
44 
45 	pagesize = getpagesize();
46 	while (*++argv) {
47 		if ((fd = open(*argv, O_RDWR)) < 0 ||
48 		    (n = read(fd, (char *)&head, sizeof(EXEC))) == -1)
49 			error(*argv);
50 		if (n != sizeof(EXEC) || N_BADMAG(head)) {
51 			(void)fprintf(stderr,
52 			    "strip: %s not in a.out format.\n", *argv);
53 			exit(1);
54 		}
55 		if (!head.a_syms && !head.a_trsize && !head.a_drsize)
56 			continue;
57 		fsize = head.a_text + head.a_data;
58 		if (head.a_magic == ZMAGIC)
59 			fsize += pagesize - sizeof(EXEC);
60 		head.a_syms = head.a_trsize = head.a_drsize = 0;
61 		if (ftruncate(fd, fsize + sizeof(EXEC)) ||
62 		    lseek(fd, 0L, L_SET) == -1 ||
63 		    write(fd, (char *)&head, sizeof(EXEC)) != sizeof(EXEC))
64 			error(*argv);
65 		(void)close(fd);
66 	}
67 	exit(0);
68 }
69 
70 error(fname)
71 	char *fname;
72 {
73 	extern int errno;
74 	char *strerror();
75 
76 	(void)fprintf(stderr, "strip: %s: %s.\n", fname, strerror(errno));
77 	exit(1);
78 }
79