[BACK]Return to build.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ranlib

Annotation of src/usr.bin/ranlib/build.c, Revision 1.1

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