2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Hugh Smith at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * $FreeBSD: src/usr.bin/ar/ar.c,v 1.8.2.1 2001/08/02 00:51:00 obrien Exp $
40 static const char copyright[] =
41 "@(#) Copyright (c) 1990, 1993, 1994\n\
42 The Regents of the University of California. All rights reserved.\n";
46 static const char sccsid[] = "@(#)ar.c 8.3 (Berkeley) 4/2/94";
49 #include <sys/param.h>
67 char *archive, *envtmp, *posarg, *posname;
68 static void badoptions __P((char *));
69 static void usage __P((void));
73 * main basically uses getopt to parse options and calls the appropriate
74 * functions. Some hacks that let us be backward compatible with 4.3 ar
75 * option parsing and sanity checking.
84 int (*fcall) __P((char **)) = NULL;
86 (void) setlocale(LC_TIME, "");;
92 * Historic versions didn't require a '-' in front of the options.
93 * Fix it, if necessary.
95 if (*argv[1] != '-') {
96 if (!(p = malloc((u_int)(strlen(argv[1]) + 2))))
99 (void)strcpy(p + 1, argv[1]);
103 while ((c = getopt(argc, argv, "abcdilmopqrTtuvx")) != -1) {
119 case 'l': /* not documented, compatibility only */
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");
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");
176 /* -ab require a position argument. */
177 if (options & (AR_A|AR_B)) {
178 if (!(posarg = *argv++)) {
179 warnx("no position operand specified");
182 posname = basename(posarg);
184 /* -d only valid with -Tv. */
185 if (options & AR_D && options & ~(AR_D|AR_TR|AR_V))
187 /* -m only valid with -abiTv. */
188 if (options & AR_M && options & ~(AR_A|AR_B|AR_M|AR_TR|AR_V))
190 /* -p only valid with -Tv. */
191 if (options & AR_P && options & ~(AR_P|AR_TR|AR_V))
193 /* -q only valid with -cTv. */
194 if (options & AR_Q && options & ~(AR_C|AR_Q|AR_TR|AR_V))
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))
199 /* -t only valid with -Tv. */
200 if (options & AR_T && options & ~(AR_T|AR_TR|AR_V))
202 /* -x only valid with -ouTv. */
203 if (options & AR_X && options & ~(AR_O|AR_U|AR_TR|AR_V|AR_X))
206 if (!(archive = *argv++)) {
207 warnx("no archive specified");
211 /* -dmr require a list of archive elements. */
212 if (options & (AR_D|AR_M|AR_R) && !*argv) {
213 warnx("no archive members specified");
217 exit((*fcall)(argv));
225 warnx("illegal option combination for %s", arg);
233 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
234 "usage: ar -d [-Tv] archive file ...",
235 " ar -m [-Tv] archive file ...",
236 " ar -m [-abiTv] position archive file ...",
237 " ar -p [-Tv] archive [file ...]",
238 " ar -q [-cTv] archive file ...",
239 " ar -r [-cuTv] archive file ...",
240 " ar -r [-abciuTv] position archive file ...",
241 " ar -t [-Tv] archive [file ...]",
242 " ar -x [-ouTv] archive [file ...]");