ar.c revision 176434
1/*-
2 * Copyright (c) 2007 Kai Wang
3 * Copyright (c) 2007 Tim Kientzle
4 * Copyright (c) 2007 Joseph Koshy
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
16 *
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.
27 */
28
29/*-
30 * Copyright (c) 1990, 1993, 1994
31 *	The Regents of the University of California.  All rights reserved.
32 *
33 * This code is derived from software contributed to Berkeley by
34 * Hugh Smith at The University of Guelph.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
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.
47 *
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
58 * SUCH DAMAGE.
59 */
60
61#include <sys/cdefs.h>
62__FBSDID("$FreeBSD: head/usr.bin/ar/ar.c 176434 2008-02-21 10:52:31Z kaiw $");
63
64#include <sys/queue.h>
65#include <sys/types.h>
66#include <archive.h>
67#include <errno.h>
68#include <getopt.h>
69#include <libgen.h>
70#include <stdio.h>
71#include <stdlib.h>
72#include <string.h>
73#include <sysexits.h>
74
75#include "ar.h"
76
77enum options
78{
79	OPTION_HELP
80};
81
82static struct option longopts[] =
83{
84	{"help", no_argument, NULL, OPTION_HELP},
85	{"version", no_argument, NULL, 'V'},
86	{NULL, 0, NULL, 0}
87};
88
89static void	bsdar_usage(void);
90static void	ranlib_usage(void);
91static void	set_mode(struct bsdar *bsdar, char opt);
92static void	only_mode(struct bsdar *bsdar, const char *opt,
93		    const char *valid_modes);
94static void	bsdar_version(void);
95static void	ranlib_version(void);
96
97int
98main(int argc, char **argv)
99{
100	struct bsdar	*bsdar, bsdar_storage;
101	char		*p;
102	size_t		 len;
103	int		 i, opt;
104
105	bsdar = &bsdar_storage;
106	memset(bsdar, 0, sizeof(*bsdar));
107
108	if ((bsdar->progname = getprogname()) == NULL)
109		bsdar->progname = "ar";
110
111	if (strcmp(bsdar->progname, "ranlib") == 0) {
112		while ((opt = getopt_long(argc, argv, "tV", longopts,
113		    NULL)) != -1) {
114			switch(opt) {
115			case 't':
116				/* Ignored. */
117				break;
118			case 'V':
119				ranlib_version();
120				break;
121			case OPTION_HELP:
122				ranlib_usage();
123			default:
124				ranlib_usage();
125			}
126		}
127		argv += optind;
128		argc -= optind;
129
130		if (*argv == NULL)
131			ranlib_usage();
132
133		bsdar->options |= AR_S;
134		for (;(bsdar->filename = *argv++) != NULL;)
135			ar_mode_s(bsdar);
136
137		exit(EX_OK);
138	} else {
139		if (argc < 2)
140			bsdar_usage();
141
142		if (*argv[1] != '-') {
143			len = strlen(argv[1]) + 2;
144			if ((p = malloc(len)) == NULL)
145				bsdar_errc(bsdar, EX_SOFTWARE, errno,
146				    "malloc failed");
147			*p = '-';
148			(void)strlcpy(p + 1, argv[1], len - 1);
149			argv[1] = p;
150		}
151	}
152
153	while ((opt = getopt_long(argc, argv, "abCcdfijlmopqrSsTtuVvxz",
154	    longopts, NULL)) != -1) {
155		switch(opt) {
156		case 'a':
157			bsdar->options |= AR_A;
158			break;
159		case 'b':
160		case 'i':
161			bsdar->options |= AR_B;
162			break;
163		case 'C':
164			bsdar->options |= AR_CC;
165			break;
166		case 'c':
167			bsdar->options |= AR_C;
168			break;
169		case 'd':
170			set_mode(bsdar, opt);
171			break;
172		case 'f':
173		case 'T':
174			bsdar->options |= AR_TR;
175			break;
176		case 'j':
177			bsdar->options |= AR_J;
178			break;
179		case 'l':
180			/* ignored, for GNU ar comptibility */
181			break;
182		case 'm':
183			set_mode(bsdar, opt);
184			break;
185		case 'o':
186			bsdar->options |= AR_O;
187			break;
188		case 'p':
189			set_mode(bsdar, opt);
190			break;
191		case 'q':
192			set_mode(bsdar, opt);
193			break;
194		case 'r':
195			set_mode(bsdar, opt);
196			break;
197		case 'S':
198			bsdar->options |= AR_SS;
199			break;
200		case 's':
201			bsdar->options |= AR_S;
202			break;
203		case 't':
204			set_mode(bsdar, opt);
205			break;
206		case 'u':
207			bsdar->options |= AR_U;
208			break;
209		case 'V':
210			bsdar_version();
211			break;
212		case 'v':
213			bsdar->options |= AR_V;
214			break;
215		case 'x':
216			set_mode(bsdar, opt);
217			break;
218		case 'z':
219			bsdar->options |= AR_Z;
220			break;
221		case OPTION_HELP:
222			bsdar_usage();
223		default:
224			bsdar_usage();
225		}
226	}
227
228	argv += optind;
229	argc -= optind;
230
231	if (*argv == NULL)
232		bsdar_usage();
233
234	if (bsdar->options & AR_A && bsdar->options & AR_B)
235		bsdar_errc(bsdar, EX_USAGE, 0,
236		    "only one of -a and -[bi] options allowed");
237
238	if (bsdar->options & AR_J && bsdar->options & AR_Z)
239		bsdar_errc(bsdar, EX_USAGE, 0,
240		    "only one of -j and -z options allowed");
241
242	if (bsdar->options & AR_S && bsdar->options & AR_SS)
243		bsdar_errc(bsdar, EX_USAGE, 0,
244		    "only one of -s and -S options allowed");
245
246	if (bsdar->options & (AR_A | AR_B)) {
247		if ((bsdar->posarg = *argv) == NULL)
248			bsdar_errc(bsdar, EX_USAGE, 0,
249			    "no position operand specified");
250		if ((bsdar->posarg = basename(bsdar->posarg)) == NULL)
251			bsdar_errc(bsdar, EX_SOFTWARE, errno,
252			    "basename failed");
253		argc--;
254		argv++;
255	}
256
257	if (bsdar->options & AR_A)
258		only_mode(bsdar, "-a", "mqr");
259	if (bsdar->options & AR_B)
260		only_mode(bsdar, "-b", "mqr");
261	if (bsdar->options & AR_C)
262		only_mode(bsdar, "-c", "qr");
263	if (bsdar->options & AR_CC)
264		only_mode(bsdar, "-C", "x");
265	if (bsdar->options & AR_O)
266		only_mode(bsdar, "-o", "x");
267	if (bsdar->options & AR_SS)
268		only_mode(bsdar, "-S", "mqr");
269	if (bsdar->options & AR_U)
270		only_mode(bsdar, "-u", "qrx");
271
272	if ((bsdar->filename = *argv) == NULL)
273		bsdar_usage();
274
275	bsdar->argc = --argc;
276	bsdar->argv = ++argv;
277
278	if ((!bsdar->mode || strchr("ptx", bsdar->mode)) &&
279	    bsdar->options & AR_S) {
280		ar_mode_s(bsdar);
281		if (!bsdar->mode)
282			exit(EX_OK);
283	}
284
285	switch(bsdar->mode) {
286	case 'd':
287		ar_mode_d(bsdar);
288		break;
289	case 'm':
290		ar_mode_m(bsdar);
291		break;
292	case 'p':
293		ar_mode_p(bsdar);
294		break;
295	case 'q':
296		ar_mode_r(bsdar);
297		break;
298	case 'r':
299		ar_mode_r(bsdar);
300		break;
301	case 't':
302		ar_mode_t(bsdar);
303		break;
304	case 'x':
305		ar_mode_x(bsdar);
306		break;
307	default:
308		bsdar_usage();
309		/* NOTREACHED */
310	}
311
312	for (i = 0; i < bsdar->argc; i++)
313		if (bsdar->argv[i] != NULL)
314			bsdar_warnc(bsdar, 0, "%s: not found in archive",
315			    bsdar->argv[i]);
316
317	exit(EX_OK);
318}
319
320static void
321set_mode(struct bsdar *bsdar, char opt)
322{
323
324	if (bsdar->mode != '\0' && bsdar->mode != opt)
325		bsdar_errc(bsdar, EX_USAGE, 0,
326		    "Can't specify both -%c and -%c", opt, bsdar->mode);
327	bsdar->mode = opt;
328}
329
330static void
331only_mode(struct bsdar *bsdar, const char *opt, const char *valid_modes)
332{
333
334	if (strchr(valid_modes, bsdar->mode) == NULL)
335		bsdar_errc(bsdar, EX_USAGE, 0,
336		    "Option %s is not permitted in mode -%c", opt, bsdar->mode);
337}
338
339static void
340bsdar_usage()
341{
342
343	(void)fprintf(stderr, "usage:  ar -d [-Tjsvz] archive file ...\n");
344	(void)fprintf(stderr, "\tar -m [-Tjsvz] archive file ...\n");
345	(void)fprintf(stderr, "\tar -m [-Tabijsvz] position archive file ...\n");
346	(void)fprintf(stderr, "\tar -p [-Tv] archive [file ...]\n");
347	(void)fprintf(stderr, "\tar -r [-Tcjsuvz] archive file ...\n");
348	(void)fprintf(stderr, "\tar -r [-Tabcijsuvz] position archive file ...\n");
349	(void)fprintf(stderr, "\tar -s [-jz] archive\n");
350	(void)fprintf(stderr, "\tar -t [-Tv] archive [file ...]\n");
351	(void)fprintf(stderr, "\tar -x [-CTouv] archive [file ...]\n");
352	(void)fprintf(stderr, "\tar -V\n");
353	exit(EX_USAGE);
354}
355
356static void
357ranlib_usage()
358{
359
360	(void)fprintf(stderr, "usage:	ranlib [-t] archive ...\n");
361	(void)fprintf(stderr, "\tranlib -V\n");
362	exit(EX_USAGE);
363}
364
365static void
366bsdar_version()
367{
368	(void)printf("BSD ar %s - %s\n", BSDAR_VERSION, archive_version());
369	exit(EX_OK);
370}
371
372static void
373ranlib_version()
374{
375	(void)printf("ranlib %s - %s\n", BSDAR_VERSION, archive_version());
376	exit(EX_OK);
377}
378