xref: /original-bsd/usr.bin/strip/strip.c (revision 31205e5f)
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.3 (Berkeley) 06/30/88";
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 /* ARGSUSED */
34 main(argc, argv)
35 	int argc;
36 	char **argv;
37 {
38 	typedef struct exec EXEC;
39 	register off_t fsize;
40 	register int fd, n, pagesize;
41 	EXEC head;
42 	off_t lseek();
43 
44 	pagesize = getpagesize();
45 	while (*++argv) {
46 		if ((fd = open(*argv, O_RDWR)) < 0 ||
47 		    (n = read(fd, (char *)&head, sizeof(EXEC))) == -1)
48 			error(*argv);
49 		if (n != sizeof(EXEC) || N_BADMAG(head)) {
50 			fprintf(stderr, "strip: %s not in a.out format.\n",
51 			    *argv);
52 			exit(1);
53 		}
54 		if (!head.a_syms && !head.a_trsize && !head.a_drsize) {
55 			fprintf(stderr, "strip: %s already stripped.\n", *argv);
56 			continue;
57 		}
58 		fsize = head.a_text + head.a_data;
59 		if (head.a_magic == ZMAGIC)
60 			fsize += pagesize - sizeof(EXEC);
61 		head.a_syms = head.a_trsize = head.a_drsize = 0;
62 		if (ftruncate(fd, fsize + sizeof(EXEC)) ||
63 		    lseek(fd, 0L, L_SET) == -1 ||
64 		    write(fd, (char *)&head, sizeof(EXEC)) != sizeof(EXEC))
65 			error(*argv);
66 		(void)close(fd);
67 	}
68 	exit(0);
69 }
70 
71 static
72 error(fname)
73 	char *fname;
74 {
75 	fprintf(stderr, "strip: %s: ", fname);
76 	perror((char *)NULL);
77 	exit(1);
78 }
79