Annotation of src/usr.bin/ranlib/build.c, Revision 1.3
1.3 ! deraadt 1: /* $OpenBSD: build.c,v 1.2 1996/06/26 05:38:03 deraadt Exp $ */
1.2 deraadt 2:
1.1 deraadt 3: /*-
4: * Copyright (c) 1990 The Regents of the University of California.
5: * All rights reserved.
6: *
7: * This code is derived from software contributed to Berkeley by
8: * Hugh Smith at The University of Guelph.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: * 3. All advertising materials mentioning features or use of this software
19: * must display the following acknowledgement:
20: * This product includes software developed by the University of
21: * California, Berkeley and its contributors.
22: * 4. Neither the name of the University nor the names of its contributors
23: * may be used to endorse or promote products derived from this software
24: * without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36: * SUCH DAMAGE.
37: */
38:
39: #ifndef lint
40: /*static char sccsid[] = "from: @(#)build.c 5.3 (Berkeley) 3/12/91";*/
1.3 ! deraadt 41: static char rcsid[] = "$OpenBSD: build.c,v 1.2 1996/06/26 05:38:03 deraadt Exp $";
1.1 deraadt 42: #endif /* not lint */
43:
44: #include <sys/types.h>
45: #include <sys/stat.h>
46: #include <errno.h>
47: #include <fcntl.h>
48: #include <a.out.h>
49: #include <dirent.h>
50: #include <unistd.h>
51: #include <ar.h>
52: #include <ranlib.h>
53: #include <stdio.h>
54: #include <string.h>
55: #include <archive.h>
56:
57: extern CHDR chdr; /* converted header */
58: extern char *archive; /* archive name */
59: extern char *tname; /* temporary file "name" */
60:
61: typedef struct _rlib {
62: struct _rlib *next; /* next structure */
63: off_t pos; /* offset of defining archive file */
64: char *sym; /* symbol */
65: int symlen; /* strlen(sym) */
66: } RLIB;
67: RLIB *rhead, **pnext;
68:
69: static FILE *fp;
70: static long symcnt; /* symbol count */
71: static long tsymlen; /* total string length */
72:
73: static void rexec(), symobj();
74: extern void *emalloc();
75:
76: build()
77: {
78: CF cf;
79: int afd, tfd;
80: off_t size;
81:
82: afd = open_archive(O_RDWR);
83: fp = fdopen(afd, "r+");
84: tfd = tmp();
85:
86: SETCF(afd, archive, tfd, tname, RPAD|WPAD);
87:
88: /* Read through the archive, creating list of symbols. */
89: symcnt = tsymlen = 0;
90: pnext = &rhead;
91: while(get_arobj(afd)) {
92: if (!strcmp(chdr.name, RANLIBMAG)) {
93: skip_arobj(afd);
94: continue;
95: }
96: rexec(afd, tfd);
97: put_arobj(&cf, (struct stat *)NULL);
98: }
99: *pnext = NULL;
100:
101: /* Create the symbol table. */
102: symobj();
103:
104: /* Copy the saved objects into the archive. */
105: size = lseek(tfd, (off_t)0, SEEK_CUR);
106: (void)lseek(tfd, (off_t)0, SEEK_SET);
107: SETCF(tfd, tname, afd, archive, NOPAD);
108: copy_ar(&cf, size);
109: (void)ftruncate(afd, lseek(afd, (off_t)0, SEEK_CUR));
110: (void)close(tfd);
111:
112: /* Set the time. */
113: settime(afd);
114: close_archive(afd);
115: return(0);
116: }
117:
118: /*
119: * rexec
120: * Read the exec structure; ignore any files that don't look
121: * exactly right.
122: */
123: static void
124: rexec(rfd, wfd)
125: register int rfd;
126: int wfd;
127: {
128: register RLIB *rp;
129: register long nsyms;
130: register int nr, symlen;
131: register char *strtab, *sym;
132: struct exec ebuf;
133: struct nlist nl;
134: off_t r_off, w_off;
135: long strsize;
136:
137: /* Get current offsets for original and tmp files. */
138: r_off = lseek(rfd, (off_t)0, SEEK_CUR);
139: w_off = lseek(wfd, (off_t)0, SEEK_CUR);
140:
141: /* Read in exec structure. */
142: nr = read(rfd, (char *)&ebuf, sizeof(struct exec));
143: if (nr != sizeof(struct exec))
144: goto badread;
145:
146: /* Check magic number and symbol count. */
147: if (N_BADMAG(ebuf) || ebuf.a_syms == 0)
148: goto bad1;
149:
150: /* Seek to string table. */
151: if (lseek(rfd, N_STROFF(ebuf) + r_off, SEEK_SET) == (off_t)-1)
152: error(archive);
153:
154: /* Read in size of the string table. */
155: nr = read(rfd, (char *)&strsize, sizeof(strsize));
156: if (nr != sizeof(strsize))
157: goto badread;
158:
159: /* Read in the string table. */
160: strsize -= sizeof(strsize);
161: strtab = (char *)emalloc(strsize);
162: nr = read(rfd, strtab, strsize);
163: if (nr != strsize) {
164: badread: if (nr < 0)
165: error(archive);
166: goto bad2;
167: }
168:
169: /* Seek to symbol table. */
170: if (fseek(fp, N_SYMOFF(ebuf) + r_off, SEEK_SET) == (off_t)-1)
171: goto bad2;
172:
173: /* For each symbol read the nlist entry and save it as necessary. */
174: nsyms = ebuf.a_syms / sizeof(struct nlist);
175: while (nsyms--) {
176: if (!fread((char *)&nl, sizeof(struct nlist), 1, fp)) {
177: if (feof(fp))
178: badfmt();
179: error(archive);
180: }
181:
182: /* Ignore if no name or local. */
183: if (!nl.n_un.n_strx || !(nl.n_type & N_EXT))
184: continue;
185:
186: /*
187: * If the symbol is an undefined external and the n_value
188: * field is non-zero, keep it.
189: */
190: if ((nl.n_type & N_TYPE) == N_UNDF && !nl.n_value)
191: continue;
192:
193: /* First four bytes are the table size. */
194: sym = strtab + nl.n_un.n_strx - sizeof(long);
195: symlen = strlen(sym) + 1;
196:
197: rp = (RLIB *)emalloc(sizeof(RLIB));
198: rp->sym = (char *)emalloc(symlen);
199: bcopy(sym, rp->sym, symlen);
200: rp->symlen = symlen;
201: rp->pos = w_off;
202:
203: /* Build in forward order for "ar -m" command. */
204: *pnext = rp;
205: pnext = &rp->next;
206:
207: ++symcnt;
208: tsymlen += symlen;
209: }
210:
211: bad2: free(strtab);
212: bad1: (void)lseek(rfd, (off_t)r_off, SEEK_SET);
213: }
214:
215: /*
216: * symobj --
217: * Write the symbol table into the archive, computing offsets as
218: * writing.
219: */
220: static void
221: symobj()
222: {
223: register RLIB *rp, *rnext;
224: struct ranlib rn;
225: char hb[sizeof(struct ar_hdr) + 1], pad;
226: long ransize, size, stroff;
1.3 ! deraadt 227: uid_t uid;
! 228: gid_t gid;
1.1 deraadt 229:
230: /* Rewind the archive, leaving the magic number. */
231: if (fseek(fp, (off_t)SARMAG, SEEK_SET) == (off_t)-1)
232: error(archive);
233:
234: /* Size of the ranlib archive file, pad if necessary. */
235: ransize = sizeof(long) +
236: symcnt * sizeof(struct ranlib) + sizeof(long) + tsymlen;
237: if (ransize & 01) {
238: ++ransize;
239: pad = '\n';
240: } else
241: pad = '\0';
242:
1.3 ! deraadt 243: uid = getuid();
! 244: if (uid > USHRT_MAX) {
! 245: warnx("warning: uid %d truncated to %d", uid, USHRT_MAX);
! 246: uid = USHRT_MAX;
! 247: }
! 248: gid = getgid();
! 249: if (gid > USHRT_MAX) {
! 250: warnx("warning: gid %d truncated to %d", gid, USHRT_MAX);
! 251: gid = USHRT_MAX;
! 252: }
! 253:
1.1 deraadt 254: /* Put out the ranlib archive file header. */
255: #define DEFMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
1.3 ! deraadt 256: (void)sprintf(hb, HDR2, RANLIBMAG, 0L, uid, gid,
1.1 deraadt 257: DEFMODE & ~umask(0), (off_t)ransize, ARFMAG);
258: if (!fwrite(hb, sizeof(struct ar_hdr), 1, fp))
259: error(tname);
260:
261: /* First long is the size of the ranlib structure section. */
262: size = symcnt * sizeof(struct ranlib);
263: if (!fwrite((char *)&size, sizeof(size), 1, fp))
264: error(tname);
265:
266: /* Offset of the first archive file. */
267: size = SARMAG + sizeof(struct ar_hdr) + ransize;
268:
269: /*
270: * Write out the ranlib structures. The offset into the string
271: * table is cumulative, the offset into the archive is the value
272: * set in rexec() plus the offset to the first archive file.
273: */
274: for (rp = rhead, stroff = 0; rp; rp = rp->next) {
275: rn.ran_un.ran_strx = stroff;
276: stroff += rp->symlen;
277: rn.ran_off = size + rp->pos;
278: if (!fwrite((char *)&rn, sizeof(struct ranlib), 1, fp))
279: error(archive);
280: }
281:
282: /* Second long is the size of the string table. */
283: if (!fwrite((char *)&tsymlen, sizeof(tsymlen), 1, fp))
284: error(tname);
285:
286: /* Write out the string table. */
287: for (rp = rhead; rp; rp = rnext) {
288: if (!fwrite(rp->sym, rp->symlen, 1, fp))
289: error(tname);
290: rnext = rp->next;
291: free(rp);
292: }
293: rhead = NULL;
294:
295: if (pad && !fwrite(&pad, sizeof(pad), 1, fp))
296: error(tname);
297:
298: (void)fflush(fp);
299: }