]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/ranlib/build.c
This commit was generated by cvs2svn to compensate for changes in r85756,
[FreeBSD/FreeBSD.git] / usr.bin / ranlib / build.c
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.
35  */
36
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)build.c     8.1 (Berkeley) 6/6/93";
40 #endif
41 static const char rcsid[] =
42   "$FreeBSD$";
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/stat.h>
47
48 #include <a.out.h>
49 #include <ar.h>
50 #include <dirent.h>
51 #include <fcntl.h>
52 #include <ranlib.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57
58 #include "archive.h"
59
60 extern int tmp __P(( void ));
61 extern void error __P(( char * ));
62 extern void badfmt __P(( void ));
63 extern void settime __P(( int ));
64
65 extern CHDR chdr;                       /* converted header */
66 extern char *archive;                   /* archive name */
67 extern char *tname;                     /* temporary file "name" */
68
69 typedef struct _rlib {
70         struct _rlib *next;             /* next structure */
71         off_t pos;                      /* offset of defining archive file */
72         char *sym;                      /* symbol */
73         int symlen;                     /* strlen(sym) */
74 } RLIB;
75 RLIB *rhead, **pnext;
76
77 FILE *fp;
78
79 long symcnt;                            /* symbol count */
80 long tsymlen;                           /* total string length */
81
82 static void rexec __P((int, int));
83 static void symobj __P((void));
84
85 int
86 build(void)
87 {
88         CF cf;
89         int afd, tfd;
90         off_t size;
91
92         afd = open_archive(O_RDWR);
93         fp = fdopen(afd, "r+");
94         tfd = tmp();
95
96         SETCF(afd, archive, tfd, tname, RPAD|WPAD);
97
98         /* Read through the archive, creating list of symbols. */
99         pnext = &rhead;
100         symcnt = tsymlen = 0;
101         while(get_arobj(afd)) {
102                 if (!strcmp(chdr.name, RANLIBMAG)) {
103                         skip_arobj(afd);
104                         continue;
105                 }
106                 rexec(afd, tfd);
107                 put_arobj(&cf, (struct stat *)NULL);
108         }
109         *pnext = NULL;
110
111         /* Create the symbol table. */
112         symobj();
113
114         /* Copy the saved objects into the archive. */
115         size = lseek(tfd, (off_t)0, SEEK_CUR);
116         (void)lseek(tfd, (off_t)0, SEEK_SET);
117         SETCF(tfd, tname, afd, archive, WPAD);
118         copy_ar(&cf, size);
119         (void)ftruncate(afd, lseek(afd, (off_t)0, SEEK_CUR));
120         (void)close(tfd);
121
122         /* Set the time. */
123         settime(afd);
124         close_archive(afd);
125         return(0);
126 }
127
128 /*
129  * rexec
130  *      Read the exec structure; ignore any files that don't look
131  *      exactly right.
132  */
133 static void
134 rexec(rfd, wfd)
135         register int rfd;
136         int wfd;
137 {
138         register RLIB *rp;
139         register long nsyms;
140         register int nr, symlen;
141         register char *strtab = 0, *sym;
142         struct exec ebuf;
143         struct nlist nl;
144         off_t r_off, w_off;
145         long strsize;
146
147         /* Get current offsets for original and tmp files. */
148         r_off = lseek(rfd, (off_t)0, SEEK_CUR);
149         w_off = lseek(wfd, (off_t)0, SEEK_CUR);
150
151         /* Read in exec structure. */
152         nr = read(rfd, &ebuf, sizeof(struct exec));
153         if (nr != sizeof(struct exec))
154                 goto badread;
155
156         /* Check magic number and symbol count. */
157         if (N_BADMAG(ebuf) || ebuf.a_syms == 0)
158                 goto bad1;
159
160         /* Seek to string table. */
161         if (lseek(rfd, r_off + N_STROFF(ebuf), SEEK_SET) == (off_t)-1)
162                 error(archive);
163
164         /* Read in size of the string table. */
165         nr = read(rfd, &strsize, sizeof(strsize));
166         if (nr != sizeof(strsize))
167                 goto badread;
168
169         /* Read in the string table. */
170         strsize -= sizeof(strsize);
171         if ((strtab = malloc(strsize)) == NULL)
172                 error(archive);
173         nr = read(rfd, strtab, strsize);
174         if (nr != strsize) {
175 badread:        if (nr < 0)
176                         error(archive);
177                 goto bad2;
178         }
179
180         /* Seek to symbol table. */
181         if (fseek(fp, (long)r_off + N_SYMOFF(ebuf), SEEK_SET))
182                 goto bad2;
183
184         /* For each symbol read the nlist entry and save it as necessary. */
185         nsyms = ebuf.a_syms / sizeof(struct nlist);
186         while (nsyms--) {
187                 if (!fread(&nl, sizeof(struct nlist), 1, fp)) {
188                         if (feof(fp))
189                                 badfmt();
190                         error(archive);
191                 }
192
193                 /* Ignore if no name or local. */
194                 if (!nl.n_un.n_strx || !(nl.n_type & N_EXT))
195                         continue;
196
197                 /*
198                  * If the symbol is an undefined external and the n_value
199                  * field is non-zero, keep it.
200                  */
201                 if ((nl.n_type & N_TYPE) == N_UNDF && !nl.n_value)
202                         continue;
203
204                 /* First four bytes are the table size. */
205                 sym = strtab + nl.n_un.n_strx - sizeof(long);
206                 symlen = strlen(sym) + 1;
207
208                 if ((rp = malloc(sizeof(RLIB))) == NULL)
209                         error(archive);
210                 if ((rp->sym = malloc(symlen)) == NULL)
211                         error(archive);
212                 bcopy(sym, rp->sym, symlen);
213                 rp->symlen = symlen;
214                 rp->pos = w_off;
215
216                 /* Build in forward order for "ar -m" command. */
217                 *pnext = rp;
218                 pnext = &rp->next;
219
220                 ++symcnt;
221                 tsymlen += symlen;
222         }
223
224 bad2:   free(strtab);
225 bad1:   (void)lseek(rfd, r_off, SEEK_SET);
226 }
227
228 /*
229  * symobj --
230  *      Write the symbol table into the archive, computing offsets as
231  *      writing.
232  */
233 static void
234 symobj()
235 {
236         register RLIB *rp, *rpnext;
237         struct ranlib rn;
238         off_t ransize;
239         long size, stroff;
240         char hb[sizeof(struct ar_hdr) + 1], pad;
241
242         /* Rewind the archive, leaving the magic number. */
243         if (fseek(fp, (long)SARMAG, SEEK_SET))
244                 error(archive);
245
246         /* Size of the ranlib archive file, pad if necessary. */
247         ransize = sizeof(long) +
248             symcnt * sizeof(struct ranlib) + sizeof(long) + tsymlen;
249         if (ransize & 01) {
250                 ++ransize;
251                 pad = '\n';
252         } else
253                 pad = '\0';
254
255         /* Put out the ranlib archive file header. */
256 #define DEFMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
257         (void)sprintf(hb, HDR2, RANLIBMAG, 0L, getuid(), getgid(),
258             DEFMODE & ~umask(0), ransize, ARFMAG);
259         if (!fwrite(hb, sizeof(struct ar_hdr), 1, fp))
260                 error(tname);
261
262         /* First long is the size of the ranlib structure section. */
263         size = symcnt * sizeof(struct ranlib);
264         if (!fwrite(&size, sizeof(size), 1, fp))
265                 error(tname);
266
267         /* Offset of the first archive file. */
268         size = SARMAG + sizeof(struct ar_hdr) + ransize;
269
270         /*
271          * Write out the ranlib structures.  The offset into the string
272          * table is cumulative, the offset into the archive is the value
273          * set in rexec() plus the offset to the first archive file.
274          */
275         for (rp = rhead, stroff = 0; rp; rp = rp->next) {
276                 rn.ran_un.ran_strx = stroff;
277                 stroff += rp->symlen;
278                 rn.ran_off = size + rp->pos;
279                 if (!fwrite(&rn, sizeof(struct ranlib), 1, fp))
280                         error(archive);
281         }
282
283         /* Second long is the size of the string table. */
284         if (!fwrite(&tsymlen, sizeof(tsymlen), 1, fp))
285                 error(tname);
286
287         /* Write out the string table. */
288         for (rp = rhead; rp; rp = rpnext) {
289                 if (!fwrite(rp->sym, rp->symlen, 1, fp))
290                         error(tname);
291                 rpnext = rp->next;
292                 free(rp->sym);
293                 free(rp);
294         }
295         rhead = NULL;
296
297         if (pad && !fwrite(&pad, sizeof(pad), 1, fp))
298                 error(tname);
299
300         (void)fflush(fp);
301 }