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