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