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

Annotation of src/usr.bin/ar/ar.c, Revision 1.1.1.1

1.1       deraadt     1: /*     $NetBSD: ar.c,v 1.5 1995/03/26 03:27:44 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: static char copyright[] =
                     41: "@(#) Copyright (c) 1990, 1993, 1994\n\
                     42:        The Regents of the University of California.  All rights reserved.\n";
                     43: #endif /* not lint */
                     44:
                     45: #ifndef lint
                     46: #if 0
                     47: static char sccsid[] = "@(#)ar.c       8.3 (Berkeley) 4/2/94";
                     48: #else
                     49: static char rcsid[] = "$NetBSD: ar.c,v 1.5 1995/03/26 03:27:44 glass Exp $";
                     50: #endif
                     51: #endif /* not lint */
                     52:
                     53: #include <sys/param.h>
                     54:
                     55: #include <ar.h>
                     56: #include <dirent.h>
                     57: #include <err.h>
                     58: #include <paths.h>
                     59: #include <stdio.h>
                     60: #include <stdlib.h>
                     61: #include <string.h>
                     62: #include <unistd.h>
                     63:
                     64: #include "archive.h"
                     65: #include "extern.h"
                     66:
                     67: CHDR chdr;
                     68: u_int options;
                     69: char *archive, *envtmp, *posarg, *posname;
                     70: static void badoptions __P((char *));
                     71: static void usage __P((void));
                     72:
                     73: /*
                     74:  * main --
                     75:  *     main basically uses getopt to parse options and calls the appropriate
                     76:  *     functions.  Some hacks that let us be backward compatible with 4.3 ar
                     77:  *     option parsing and sanity checking.
                     78:  */
                     79: int
                     80: main(argc, argv)
                     81:        int argc;
                     82:        char **argv;
                     83: {
                     84:        int c;
                     85:        char *p;
                     86:        int (*fcall) __P((char **));
                     87:
                     88:        if (argc < 3)
                     89:                usage();
                     90:
                     91:        /*
                     92:         * Historic versions didn't require a '-' in front of the options.
                     93:         * Fix it, if necessary.
                     94:        */
                     95:        if (*argv[1] != '-') {
                     96:                if (!(p = malloc((u_int)(strlen(argv[1]) + 2))))
                     97:                        err(1, NULL);
                     98:                *p = '-';
                     99:                (void)strcpy(p + 1, argv[1]);
                    100:                argv[1] = p;
                    101:        }
                    102:
                    103:        while ((c = getopt(argc, argv, "abcdilmopqrTtuvx")) != EOF) {
                    104:                switch(c) {
                    105:                case 'a':
                    106:                        options |= AR_A;
                    107:                        break;
                    108:                case 'b':
                    109:                case 'i':
                    110:                        options |= AR_B;
                    111:                        break;
                    112:                case 'c':
                    113:                        options |= AR_C;
                    114:                        break;
                    115:                case 'd':
                    116:                        options |= AR_D;
                    117:                        fcall = delete;
                    118:                        break;
                    119:                case 'l':               /* not documented, compatibility only */
                    120:                        envtmp = ".";
                    121:                        break;
                    122:                case 'm':
                    123:                        options |= AR_M;
                    124:                        fcall = move;
                    125:                        break;
                    126:                case 'o':
                    127:                        options |= AR_O;
                    128:                        break;
                    129:                case 'p':
                    130:                        options |= AR_P;
                    131:                        fcall = print;
                    132:                        break;
                    133:                case 'q':
                    134:                        options |= AR_Q;
                    135:                        fcall = append;
                    136:                        break;
                    137:                case 'r':
                    138:                        options |= AR_R;
                    139:                        fcall = replace;
                    140:                        break;
                    141:                case 'T':
                    142:                        options |= AR_TR;
                    143:                        break;
                    144:                case 't':
                    145:                        options |= AR_T;
                    146:                        fcall = contents;
                    147:                        break;
                    148:                case 'u':
                    149:                        options |= AR_U;
                    150:                        break;
                    151:                case 'v':
                    152:                        options |= AR_V;
                    153:                        break;
                    154:                case 'x':
                    155:                        options |= AR_X;
                    156:                        fcall = extract;
                    157:                        break;
                    158:                default:
                    159:                        usage();
                    160:                }
                    161:        }
                    162:
                    163:        argv += optind;
                    164:        argc -= optind;
                    165:
                    166:        /* One of -dmpqrtx required. */
                    167:        if (!(options & (AR_D|AR_M|AR_P|AR_Q|AR_R|AR_T|AR_X))) {
                    168:                warnx("one of options -dmpqrtx is required");
                    169:                usage();
                    170:        }
                    171:        /* Only one of -a and -bi allowed. */
                    172:        if (options & AR_A && options & AR_B) {
                    173:                warnx("only one of -a and -[bi] options allowed");
                    174:                usage();
                    175:        }
                    176:        /* -ab require a position argument. */
                    177:        if (options & (AR_A|AR_B)) {
                    178:                if (!(posarg = *argv++)) {
                    179:                        warnx("no position operand specified");
                    180:                        usage();
                    181:                }
                    182:                posname = rname(posarg);
                    183:        }
                    184:        /* -d only valid with -Tv. */
                    185:        if (options & AR_D && options & ~(AR_D|AR_TR|AR_V))
                    186:                badoptions("-d");
                    187:        /* -m only valid with -abiTv. */
                    188:        if (options & AR_M && options & ~(AR_A|AR_B|AR_M|AR_TR|AR_V))
                    189:                badoptions("-m");
                    190:        /* -p only valid with -Tv. */
                    191:        if (options & AR_P && options & ~(AR_P|AR_TR|AR_V))
                    192:                badoptions("-p");
                    193:        /* -q only valid with -cTv. */
                    194:        if (options & AR_Q && options & ~(AR_C|AR_Q|AR_TR|AR_V))
                    195:                badoptions("-q");
                    196:        /* -r only valid with -abcuTv. */
                    197:        if (options & AR_R && options & ~(AR_A|AR_B|AR_C|AR_R|AR_U|AR_TR|AR_V))
                    198:                badoptions("-r");
                    199:        /* -t only valid with -Tv. */
                    200:        if (options & AR_T && options & ~(AR_T|AR_TR|AR_V))
                    201:                badoptions("-t");
                    202:        /* -x only valid with -ouTv. */
                    203:        if (options & AR_X && options & ~(AR_O|AR_U|AR_TR|AR_V|AR_X))
                    204:                badoptions("-x");
                    205:
                    206:        if (!(archive = *argv++)) {
                    207:                warnx("no archive specified");
                    208:                usage();
                    209:        }
                    210:
                    211:        /* -dmqr require a list of archive elements. */
                    212:        if (options & (AR_D|AR_M|AR_Q|AR_R) && !*argv) {
                    213:                warnx("no archive members specified");
                    214:                usage();
                    215:        }
                    216:
                    217:        exit((*fcall)(argv));
                    218: }
                    219:
                    220: static void
                    221: badoptions(arg)
                    222:        char *arg;
                    223: {
                    224:
                    225:        warnx("illegal option combination for %s", arg);
                    226:        usage();
                    227: }
                    228:
                    229: static void
                    230: usage()
                    231: {
                    232:
                    233:        (void)fprintf(stderr, "usage:  ar -d [-Tv] archive file ...\n");
                    234:        (void)fprintf(stderr, "\tar -m [-Tv] archive file ...\n");
                    235:        (void)fprintf(stderr, "\tar -m [-abiTv] position archive file ...\n");
                    236:        (void)fprintf(stderr, "\tar -p [-Tv] archive [file ...]\n");
                    237:        (void)fprintf(stderr, "\tar -q [-cTv] archive file ...\n");
                    238:        (void)fprintf(stderr, "\tar -r [-cuTv] archive file ...\n");
                    239:        (void)fprintf(stderr, "\tar -r [-abciuTv] position archive file ...\n");
                    240:        (void)fprintf(stderr, "\tar -t [-Tv] archive [file ...]\n");
                    241:        (void)fprintf(stderr, "\tar -x [-ouTv] archive [file ...]\n");
                    242:        exit(1);
                    243: }