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

Annotation of src/usr.bin/ar/move.c, Revision 1.3

1.3     ! kstailey    1: /*     $OpenBSD: move.c,v 1.2 1996/06/26 05:31:21 deraadt Exp $        */
1.1       deraadt     2: /*     $NetBSD: move.c,v 1.5 1995/03/26 03:27:57 glass Exp $   */
                      3:
                      4: /*-
                      5:  * Copyright (c) 1990, 1993, 1994
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Hugh Smith at The University of Guelph.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
                     19:  * 3. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *     This product includes software developed by the University of
                     22:  *     California, Berkeley and its contributors.
                     23:  * 4. Neither the name of the University nor the names of its contributors
                     24:  *    may be used to endorse or promote products derived from this software
                     25:  *    without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     28:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     29:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     30:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     31:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     32:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     33:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     34:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     35:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     36:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     37:  * SUCH DAMAGE.
                     38:  */
                     39:
                     40: #ifndef lint
                     41: #if 0
                     42: static char sccsid[] = "@(#)move.c     8.3 (Berkeley) 4/2/94";
                     43: #else
1.3     ! kstailey   44: static char rcsid[] = "$OpenBSD: move.c,v 1.2 1996/06/26 05:31:21 deraadt Exp $";
1.1       deraadt    45: #endif
                     46: #endif /* not lint */
                     47:
                     48: #include <sys/param.h>
                     49: #include <sys/stat.h>
                     50:
                     51: #include <ar.h>
                     52: #include <dirent.h>
                     53: #include <err.h>
                     54: #include <fcntl.h>
                     55: #include <stdio.h>
                     56: #include <unistd.h>
                     57:
                     58: #include "archive.h"
                     59: #include "extern.h"
                     60: #include "pathnames.h"
                     61:
                     62: /*
                     63:  * move --
                     64:  *     Change location of named members in archive - if 'b' or 'i' option
                     65:  *     selected then named members are placed before 'posname'.  If 'a'
                     66:  *     option selected members go after 'posname'.  If no options, members
                     67:  *     are moved to end of archive.
                     68:  */
                     69: int
                     70: move(argv)
                     71:        char **argv;
                     72: {
                     73:        CF cf;
                     74:        off_t size, tsize;
                     75:        int afd, curfd, mods, tfd1, tfd2, tfd3;
                     76:        char *file;
                     77:
                     78:        afd = open_archive(O_RDWR);
                     79:        mods = options & (AR_A|AR_B);
                     80:
                     81:        tfd1 = tmp();                   /* Files before key file. */
                     82:        tfd2 = tmp();                   /* Files selected by user. */
                     83:        tfd3 = tmp();                   /* Files after key file. */
                     84:
                     85:        /*
                     86:         * Break archive into three parts -- selected entries and entries
                     87:         * before and after the key entry.  If positioning before the key,
                     88:         * place the key at the beginning of the after key entries and if
                     89:         * positioning after the key, place the key at the end of the before
                     90:         * key entries.  Put it all back together at the end.
                     91:         */
                     92:
                     93:        /* Read and write to an archive; pad on both. */
                     94:        SETCF(afd, archive, 0, tname, RPAD|WPAD);
                     95:        for (curfd = tfd1; get_arobj(afd);) {
                     96:                if (*argv && (file = files(argv))) {
                     97:                        if (options & AR_V)
                     98:                                (void)printf("m - %s\n", file);
                     99:                        cf.wfd = tfd2;
1.3     ! kstailey  100:                        put_arobj(&cf, NULL);
1.1       deraadt   101:                        continue;
                    102:                }
                    103:                if (mods && compare(posname)) {
                    104:                        mods = 0;
                    105:                        if (options & AR_B)
                    106:                                curfd = tfd3;
                    107:                        cf.wfd = curfd;
1.3     ! kstailey  108:                        put_arobj(&cf, NULL);
1.1       deraadt   109:                        if (options & AR_A)
                    110:                                curfd = tfd3;
                    111:                } else {
                    112:                        cf.wfd = curfd;
1.3     ! kstailey  113:                        put_arobj(&cf, NULL);
1.1       deraadt   114:                }
                    115:        }
                    116:
                    117:        if (mods) {
                    118:                warnx("%s: archive member not found", posarg);
                    119:                close_archive(afd);
                    120:                return (1);
                    121:        }
                    122:        (void)lseek(afd, (off_t)SARMAG, SEEK_SET);
                    123:
                    124:        SETCF(tfd1, tname, afd, archive, NOPAD);
                    125:        tsize = size = lseek(tfd1, (off_t)0, SEEK_CUR);
                    126:        (void)lseek(tfd1, (off_t)0, SEEK_SET);
                    127:        copy_ar(&cf, size);
                    128:
                    129:        tsize += size = lseek(tfd2, (off_t)0, SEEK_CUR);
                    130:        (void)lseek(tfd2, (off_t)0, SEEK_SET);
                    131:        cf.rfd = tfd2;
                    132:        copy_ar(&cf, size);
                    133:
                    134:        tsize += size = lseek(tfd3, (off_t)0, SEEK_CUR);
                    135:        (void)lseek(tfd3, (off_t)0, SEEK_SET);
                    136:        cf.rfd = tfd3;
                    137:        copy_ar(&cf, size);
                    138:
                    139:        (void)ftruncate(afd, tsize + SARMAG);
                    140:        close_archive(afd);
                    141:
                    142:        if (*argv) {
                    143:                orphans(argv);
                    144:                return (1);
                    145:        }
                    146:        return (0);
                    147: }