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

Annotation of src/usr.bin/ar/replace.c, Revision 1.1

1.1     ! deraadt     1: /*     $NetBSD: replace.c,v 1.6 1995/03/26 03:28:01 glass Exp $        */
        !             2:
        !             3: /*-
        !             4:  * Copyright (c) 1990, 1993, 1994
        !             5:  *     The Regents of the University of California.  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: #if 0
        !            41: static char sccsid[] = "@(#)replace.c  8.3 (Berkeley) 4/2/94";
        !            42: #else
        !            43: static char rcsid[] = "$NetBSD: replace.c,v 1.6 1995/03/26 03:28:01 glass Exp $";
        !            44: #endif
        !            45: #endif /* not lint */
        !            46:
        !            47: #include <sys/param.h>
        !            48: #include <sys/stat.h>
        !            49:
        !            50: #include <ar.h>
        !            51: #include <dirent.h>
        !            52: #include <err.h>
        !            53: #include <fcntl.h>
        !            54: #include <stdio.h>
        !            55: #include <string.h>
        !            56: #include <unistd.h>
        !            57:
        !            58: #include "archive.h"
        !            59: #include "extern.h"
        !            60:
        !            61: /*
        !            62:  * replace --
        !            63:  *     Replace or add named members to archive.  Entries already in the
        !            64:  *     archive are swapped in place.  Others are added before or after
        !            65:  *     the key entry, based on the a, b and i options.  If the u option
        !            66:  *     is specified, modification dates select for replacement.
        !            67:  */
        !            68: int
        !            69: replace(argv)
        !            70:        char **argv;
        !            71: {
        !            72:        char *file;
        !            73:        int afd, curfd, errflg, exists, mods, sfd, tfd1, tfd2;
        !            74:        struct stat sb;
        !            75:        CF cf;
        !            76:        off_t size, tsize;
        !            77:
        !            78:        errflg = 0;
        !            79:        /*
        !            80:         * If doesn't exist, simply append to the archive.  There's
        !            81:         * a race here, but it's pretty short, and not worth fixing.
        !            82:         */
        !            83:        exists = !stat(archive, &sb);
        !            84:        afd = open_archive(O_CREAT|O_RDWR);
        !            85:
        !            86:        if (!exists) {
        !            87:                tfd1 = -1;
        !            88:                tfd2 = tmp();
        !            89:                goto append;
        !            90:        }
        !            91:
        !            92:        tfd1 = tmp();                   /* Files before key file. */
        !            93:        tfd2 = tmp();                   /* Files after key file. */
        !            94:
        !            95:        /*
        !            96:         * Break archive into two parts -- entries before and after the key
        !            97:         * entry.  If positioning before the key, place the key at the
        !            98:         * beginning of the after key entries and if positioning after the
        !            99:         * key, place the key at the end of the before key entries.  Put it
        !           100:         * all back together at the end.
        !           101:         */
        !           102:        mods = (options & (AR_A|AR_B));
        !           103:        for (curfd = tfd1; get_arobj(afd);) {
        !           104:                if (*argv && (file = files(argv))) {
        !           105:                        if ((sfd = open(file, O_RDONLY)) < 0) {
        !           106:                                errflg = 1;
        !           107:                                warn("%s", file);
        !           108:                                goto useold;
        !           109:                        }
        !           110:                        (void)fstat(sfd, &sb);
        !           111:                        if (options & AR_U && sb.st_mtime <= chdr.date) {
        !           112:                                close(sfd);
        !           113:                                goto useold;
        !           114:                        }
        !           115:
        !           116:                        if (options & AR_V)
        !           117:                             (void)printf("r - %s\n", file);
        !           118:
        !           119:                        /* Read from disk, write to an archive; pad on write */
        !           120:                        SETCF(sfd, file, curfd, tname, WPAD);
        !           121:                        put_arobj(&cf, &sb);
        !           122:                        (void)close(sfd);
        !           123:                        skip_arobj(afd);
        !           124:                        continue;
        !           125:                }
        !           126:
        !           127:                if (mods && compare(posname)) {
        !           128:                        mods = 0;
        !           129:                        if (options & AR_B)
        !           130:                                curfd = tfd2;
        !           131:                        /* Read and write to an archive; pad on both. */
        !           132:                        SETCF(afd, archive, curfd, tname, RPAD|WPAD);
        !           133:                        put_arobj(&cf, (struct stat *)NULL);
        !           134:                        if (options & AR_A)
        !           135:                                curfd = tfd2;
        !           136:                } else {
        !           137:                        /* Read and write to an archive; pad on both. */
        !           138: useold:                        SETCF(afd, archive, curfd, tname, RPAD|WPAD);
        !           139:                        put_arobj(&cf, (struct stat *)NULL);
        !           140:                }
        !           141:        }
        !           142:
        !           143:        if (mods) {
        !           144:                warnx("%s: archive member not found", posarg);
        !           145:                 close_archive(afd);
        !           146:                 return (1);
        !           147:         }
        !           148:
        !           149:        /* Append any left-over arguments to the end of the after file. */
        !           150: append:        while (file = *argv++) {
        !           151:                if (options & AR_V)
        !           152:                        (void)printf("a - %s\n", file);
        !           153:                if ((sfd = open(file, O_RDONLY)) < 0) {
        !           154:                        errflg = 1;
        !           155:                        warn("%s", file);
        !           156:                        continue;
        !           157:                }
        !           158:                (void)fstat(sfd, &sb);
        !           159:                /* Read from disk, write to an archive; pad on write. */
        !           160:                SETCF(sfd, file,
        !           161:                    options & (AR_A|AR_B) ? tfd1 : tfd2, tname, WPAD);
        !           162:                put_arobj(&cf, &sb);
        !           163:                (void)close(sfd);
        !           164:        }
        !           165:
        !           166:        (void)lseek(afd, (off_t)SARMAG, SEEK_SET);
        !           167:
        !           168:        SETCF(tfd1, tname, afd, archive, NOPAD);
        !           169:        if (tfd1 != -1) {
        !           170:                tsize = size = lseek(tfd1, (off_t)0, SEEK_CUR);
        !           171:                (void)lseek(tfd1, (off_t)0, SEEK_SET);
        !           172:                copy_ar(&cf, size);
        !           173:        } else
        !           174:                tsize = 0;
        !           175:
        !           176:        tsize += size = lseek(tfd2, (off_t)0, SEEK_CUR);
        !           177:        (void)lseek(tfd2, (off_t)0, SEEK_SET);
        !           178:        cf.rfd = tfd2;
        !           179:        copy_ar(&cf, size);
        !           180:
        !           181:        (void)ftruncate(afd, tsize + SARMAG);
        !           182:        close_archive(afd);
        !           183:        return (errflg);
        !           184: }