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