2 * Copyright (c) 2007 Kai Wang
3 * Copyright (c) 2007 Tim Kientzle
4 * Copyright (c) 2007 Joseph Koshy
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
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.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Copyright (c) 1990, 1993, 1994
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * Hugh Smith at The University of Guelph.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
64 #include <sys/queue.h>
65 #include <sys/types.h>
82 static struct option longopts[] =
84 {"help", no_argument, NULL, OPTION_HELP},
85 {"version", no_argument, NULL, 'V'},
89 static void bsdar_usage(void);
90 static void ranlib_usage(void);
91 static void set_mode(struct bsdar *bsdar, char opt);
92 static void only_mode(struct bsdar *bsdar, const char *opt,
93 const char *valid_modes);
94 static void bsdar_version(void);
95 static void ranlib_version(void);
98 main(int argc, char **argv)
100 struct bsdar *bsdar, bsdar_storage;
103 int i, opt, Dflag, Uflag;
105 bsdar = &bsdar_storage;
106 memset(bsdar, 0, sizeof(*bsdar));
110 if ((bsdar->progname = getprogname()) == NULL)
111 bsdar->progname = "ar";
113 /* Act like ranlib if our name ends in "ranlib"; this
114 * accommodates arm-freebsd7.1-ranlib, bsdranlib, etc. */
115 len = strlen(bsdar->progname);
116 if (len >= strlen("ranlib") &&
117 strcmp(bsdar->progname + len - strlen("ranlib"), "ranlib") == 0) {
118 while ((opt = getopt_long(argc, argv, "tDUV", longopts,
147 /* Enable determinstic mode unless -U is set. */
149 bsdar->options |= AR_D;
150 bsdar->options |= AR_S;
151 while ((bsdar->filename = *argv++) != NULL)
159 if (*argv[1] != '-') {
160 len = strlen(argv[1]) + 2;
161 if ((p = malloc(len)) == NULL)
162 bsdar_errc(bsdar, EX_SOFTWARE, errno,
165 (void)strlcpy(p + 1, argv[1], len - 1);
170 while ((opt = getopt_long(argc, argv, "abCcdDfijlMmopqrSsTtUuVvxz",
171 longopts, NULL)) != -1) {
174 bsdar->options |= AR_A;
178 bsdar->options |= AR_B;
181 bsdar->options |= AR_CC;
184 bsdar->options |= AR_C;
187 set_mode(bsdar, opt);
195 bsdar->options |= AR_TR;
201 /* ignored, for GNU ar comptibility */
204 set_mode(bsdar, opt);
207 set_mode(bsdar, opt);
210 bsdar->options |= AR_O;
213 set_mode(bsdar, opt);
216 set_mode(bsdar, opt);
219 set_mode(bsdar, opt);
222 bsdar->options |= AR_SS;
225 bsdar->options |= AR_S;
228 set_mode(bsdar, opt);
235 bsdar->options |= AR_U;
241 bsdar->options |= AR_V;
244 set_mode(bsdar, opt);
259 if (*argv == NULL && bsdar->mode != 'M')
262 if (bsdar->options & AR_A && bsdar->options & AR_B)
263 bsdar_errc(bsdar, EX_USAGE, 0,
264 "only one of -a and -[bi] options allowed");
266 if (bsdar->options & AR_J && bsdar->options & AR_Z)
267 bsdar_errc(bsdar, EX_USAGE, 0,
268 "only one of -j and -z options allowed");
270 if (bsdar->options & AR_S && bsdar->options & AR_SS)
271 bsdar_errc(bsdar, EX_USAGE, 0,
272 "only one of -s and -S options allowed");
274 if (bsdar->options & (AR_A | AR_B)) {
275 if ((bsdar->posarg = *argv) == NULL)
276 bsdar_errc(bsdar, EX_USAGE, 0,
277 "no position operand specified");
278 if ((bsdar->posarg = basename(bsdar->posarg)) == NULL)
279 bsdar_errc(bsdar, EX_SOFTWARE, errno,
285 /* Set determinstic mode for -D, and by default without -U. */
286 if (Dflag || (Uflag == 0 && (bsdar->mode == 'q' || bsdar->mode == 'r')))
287 bsdar->options |= AR_D;
289 if (bsdar->options & AR_A)
290 only_mode(bsdar, "-a", "mqr");
291 if (bsdar->options & AR_B)
292 only_mode(bsdar, "-b", "mqr");
293 if (bsdar->options & AR_C)
294 only_mode(bsdar, "-c", "qr");
295 if (bsdar->options & AR_CC)
296 only_mode(bsdar, "-C", "x");
298 only_mode(bsdar, "-D", "qr");
300 only_mode(bsdar, "-U", "qr");
301 if (bsdar->options & AR_O)
302 only_mode(bsdar, "-o", "x");
303 if (bsdar->options & AR_SS)
304 only_mode(bsdar, "-S", "mqr");
305 if (bsdar->options & AR_U)
306 only_mode(bsdar, "-u", "qrx");
308 if (bsdar->mode == 'M') {
309 ar_mode_script(bsdar);
313 if ((bsdar->filename = *argv) == NULL)
316 bsdar->argc = --argc;
317 bsdar->argv = ++argv;
319 if ((!bsdar->mode || strchr("ptx", bsdar->mode)) &&
320 bsdar->options & AR_S) {
326 switch(bsdar->mode) {
353 for (i = 0; i < bsdar->argc; i++)
354 if (bsdar->argv[i] != NULL)
355 bsdar_warnc(bsdar, 0, "%s: not found in archive",
362 set_mode(struct bsdar *bsdar, char opt)
365 if (bsdar->mode != '\0' && bsdar->mode != opt)
366 bsdar_errc(bsdar, EX_USAGE, 0,
367 "Can't specify both -%c and -%c", opt, bsdar->mode);
372 only_mode(struct bsdar *bsdar, const char *opt, const char *valid_modes)
375 if (strchr(valid_modes, bsdar->mode) == NULL)
376 bsdar_errc(bsdar, EX_USAGE, 0,
377 "Option %s is not permitted in mode -%c", opt, bsdar->mode);
384 (void)fprintf(stderr, "usage: ar -d [-Tjsvz] archive file ...\n");
385 (void)fprintf(stderr, "\tar -m [-Tjsvz] archive file ...\n");
386 (void)fprintf(stderr, "\tar -m [-Tabijsvz] position archive file ...\n");
387 (void)fprintf(stderr, "\tar -p [-Tv] archive [file ...]\n");
388 (void)fprintf(stderr, "\tar -q [-TcDjsUvz] archive file ...\n");
389 (void)fprintf(stderr, "\tar -r [-TcDjsUuvz] archive file ...\n");
390 (void)fprintf(stderr, "\tar -r [-TabcDijsUuvz] position archive file ...\n");
391 (void)fprintf(stderr, "\tar -s [-jz] archive\n");
392 (void)fprintf(stderr, "\tar -t [-Tv] archive [file ...]\n");
393 (void)fprintf(stderr, "\tar -x [-CTouv] archive [file ...]\n");
394 (void)fprintf(stderr, "\tar -V\n");
402 (void)fprintf(stderr, "usage: ranlib [-DtU] archive ...\n");
403 (void)fprintf(stderr, "\tranlib -V\n");
410 (void)printf("BSD ar %s - %s\n", BSDAR_VERSION, archive_version_string());
417 (void)printf("ranlib %s - %s\n", BSDAR_VERSION, archive_version_string());