source: trunk/minix/commands/simple/strip.c@ 11

Last change on this file since 11 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 3.6 KB
Line 
1/* strip - remove symbols. Author: Dick van Veen */
2
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6#include <a.out.h>
7#include <unistd.h>
8#include <string.h>
9#include <stdlib.h>
10#include <stdio.h>
11
12/* Strip [file] ...
13 *
14 * - when no file is present, a.out is assumed.
15 *
16 */
17
18#define A_OUT "a.out"
19#define NAME_LENGTH 128 /* max file path name */
20
21char buffer[BUFSIZ]; /* used to copy executable */
22char new_file[NAME_LENGTH]; /* contains name of temporary */
23struct exec header;
24
25_PROTOTYPE(int main, (int argc, char **argv));
26_PROTOTYPE(void strip, (char *file));
27_PROTOTYPE(int read_header, (int fd));
28_PROTOTYPE(int write_header, (int fd));
29_PROTOTYPE(int make_tmp, (char *new_name, char *name));
30_PROTOTYPE(int copy_file, (int fd1, int fd2, long size));
31
32int main(argc, argv)
33int argc;
34char **argv;
35{
36 argv++;
37 if (*argv == NULL)
38 strip(A_OUT);
39 else
40 while (*argv != NULL) {
41 strip(*argv);
42 argv++;
43 }
44 return(0);
45}
46
47void strip(file)
48char *file;
49{
50 int fd, new_fd;
51 struct stat buf;
52 long symb_size, relo_size;
53
54 fd = open(file, O_RDONLY);
55 if (fd == -1) {
56 fprintf(stderr, "can't open %s\n", file);
57 close(fd);
58 return;
59 }
60 if (read_header(fd)) {
61 fprintf(stderr, "%s: not an executable file\n", file);
62 close(fd);
63 return;
64 }
65 if (header.a_syms == 0L) {
66 close(fd); /* no symbol table present */
67 return;
68 }
69 symb_size = header.a_syms;
70 header.a_syms = 0L; /* remove table size */
71 fstat(fd, &buf);
72 relo_size = buf.st_size - (A_MINHDR + header.a_text + header.a_data + symb_size);
73 new_fd = make_tmp(new_file, file);
74 if (new_fd == -1) {
75 fprintf(stderr, "can't create temporary file\n");
76 close(fd);
77 return;
78 }
79 if (write_header(new_fd)) {
80 fprintf(stderr, "can't write temporary file\n");
81 unlink(new_file);
82 close(fd);
83 close(new_fd);
84 return;
85 }
86 if (copy_file(fd, new_fd, header.a_text + header.a_data)) {
87 fprintf(stderr, "can't copy %s\n", file);
88 unlink(new_file);
89 close(fd);
90 close(new_fd);
91 return;
92 }
93 if (relo_size != 0) {
94 lseek(fd, symb_size, 1);
95 if (copy_file(fd, new_fd, relo_size)) {
96 fprintf(stderr, "can't copy %s\n", file);
97 unlink(new_file);
98 close(fd);
99 close(new_fd);
100 return;
101 }
102 }
103 close(fd);
104 close(new_fd);
105 if (unlink(file) == -1) {
106 fprintf(stderr, "can't unlink %s\n", file);
107 unlink(new_file);
108 return;
109 }
110 link(new_file, file);
111 unlink(new_file);
112 chmod(file, buf.st_mode);
113}
114
115int read_header(fd)
116int fd;
117{
118 if (read(fd, (char *) &header, A_MINHDR) != A_MINHDR) return(1);
119 if (BADMAG(header)) return (1);
120 if (header.a_hdrlen > sizeof(struct exec)) return (1);
121 lseek(fd, 0L, SEEK_SET); /* variable size header */
122 if (read(fd, (char *)&header, (int)header.a_hdrlen) != (int) header.a_hdrlen)
123 return(1);
124 return(0);
125}
126
127int write_header(fd)
128int fd;
129{
130 lseek(fd, 0L, SEEK_SET);
131 if (write(fd, (char *)&header, (int)header.a_hdrlen) != (int)header.a_hdrlen)
132 return(1);
133 return(0);
134}
135
136int make_tmp(new_name, name)
137char *new_name, *name;
138{
139 int len;
140 char *nameptr;
141
142 len = strlen(name);
143 if (len + 1 > NAME_LENGTH) return(-1);
144 strcpy(new_name, name);
145 nameptr = strrchr(new_name, '/');
146 if (nameptr == NULL) nameptr = new_name - 1;
147 if (nameptr - new_name + 6 + 1 > NAME_LENGTH) return (-1);
148 strcpy(nameptr + 1, "XXXXXX");
149 mktemp(new_name);
150 return(creat(new_name, 0777));
151}
152
153int copy_file(fd1, fd2, size)
154int fd1, fd2;
155long size;
156{
157 int length;
158
159 while (size > 0) {
160 if (size < sizeof(buffer))
161 length = size;
162 else
163 length = sizeof(buffer);
164 if (read(fd1, buffer, length) != length) return(1);
165 if (write(fd2, buffer, length) != length) return (1);
166 size -= length;
167 }
168 return(0);
169}
Note: See TracBrowser for help on using the repository browser.