date and time created 88/12/14 16:21:47 by marc
[unix-history] / usr / src / usr.bin / strip / strip.c
CommitLineData
bcf1365c 1/*
8bae1ad6
KB
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.
bcf1365c
DF
16 */
17
18#ifndef lint
19char copyright[] =
8bae1ad6 20"@(#) Copyright (c) 1988 Regents of the University of California.\n\
bcf1365c 21 All rights reserved.\n";
8bae1ad6 22#endif /* not lint */
bcf1365c 23
7ea0db89 24#ifndef lint
8bae1ad6
KB
25static char sccsid[] = "@(#)strip.c 5.3 (Berkeley) %G%";
26#endif /* not lint */
7ea0db89 27
8bae1ad6
KB
28#include <sys/types.h>
29#include <sys/file.h>
b261d7a4 30#include <a.out.h>
0801d743 31#include <stdio.h>
b261d7a4 32
8bae1ad6 33/* ARGSUSED */
b261d7a4 34main(argc, argv)
8bae1ad6
KB
35 int argc;
36 char **argv;
b261d7a4 37{
8bae1ad6
KB
38 typedef struct exec EXEC;
39 register off_t fsize;
40 register int fd, n, pagesize;
41 EXEC head;
42 off_t lseek();
b261d7a4 43
7ea0db89 44 pagesize = getpagesize();
8bae1ad6
KB
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);
b261d7a4 67 }
8bae1ad6 68 exit(0);
b261d7a4
BJ
69}
70
8bae1ad6
KB
71static
72error(fname)
73 char *fname;
b261d7a4 74{
8bae1ad6
KB
75 fprintf(stderr, "strip: %s: ", fname);
76 perror((char *)NULL);
77 exit(1);
b261d7a4 78}