ar.c revision 288172
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: stable/10/usr.bin/ar/ar.c 288172 2015-09-24 12:47:31Z emaste $");
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	/* Act like ranlib if our name ends in "ranlib"; this
112	 * accommodates arm-freebsd7.1-ranlib, bsdranlib, etc. */
113	len = strlen(bsdar->progname);
114	if (len >= strlen("ranlib") &&
115	    strcmp(bsdar->progname + len - strlen("ranlib"), "ranlib") == 0) {
116		while ((opt = getopt_long(argc, argv, "tDUV", longopts,
117		    NULL)) != -1) {
118			switch(opt) {
119			case 't':
120				/* Ignored. */
121				break;
122			case 'D':
123				bsdar->options |= AR_D;
124				break;
125			case 'U':
126				bsdar->options &= ~AR_D;
127				break;
128			case 'V':
129				ranlib_version();
130				break;
131			case OPTION_HELP:
132				ranlib_usage();
133			default:
134				ranlib_usage();
135			}
136		}
137		argv += optind;
138		argc -= optind;
139
140		if (*argv == NULL)
141			ranlib_usage();
142
143		bsdar->options |= AR_S;
144		while ((bsdar->filename = *argv++) != NULL)
145			ar_mode_s(bsdar);
146
147		exit(EX_OK);
148	} else {
149		if (argc < 2)
150			bsdar_usage();
151
152		if (*argv[1] != '-') {
153			len = strlen(argv[1]) + 2;
154			if ((p = malloc(len)) == NULL)
155				bsdar_errc(bsdar, EX_SOFTWARE, errno,
156				    "malloc failed");
157			*p = '-';
158			(void)strlcpy(p + 1, argv[1], len - 1);
159			argv[1] = p;
160		}
161	}
162
163	while ((opt = getopt_long(argc, argv, "abCcdDfijlMmopqrSsTtUuVvxz",
164	    longopts, NULL)) != -1) {
165		switch(opt) {
166		case 'a':
167			bsdar->options |= AR_A;
168			break;
169		case 'b':
170		case 'i':
171			bsdar->options |= AR_B;
172			break;
173		case 'C':
174			bsdar->options |= AR_CC;
175			break;
176		case 'c':
177			bsdar->options |= AR_C;
178			break;
179		case 'd':
180			set_mode(bsdar, opt);
181			break;
182		case 'D':
183			bsdar->options |= AR_D;
184			break;
185		case 'f':
186		case 'T':
187			bsdar->options |= AR_TR;
188			break;
189		case 'j':
190			/* ignored */
191			break;
192		case 'l':
193			/* ignored, for GNU ar comptibility */
194			break;
195		case 'M':
196			set_mode(bsdar, opt);
197			break;
198		case 'm':
199			set_mode(bsdar, opt);
200			break;
201		case 'o':
202			bsdar->options |= AR_O;
203			break;
204		case 'p':
205			set_mode(bsdar, opt);
206			break;
207		case 'q':
208			set_mode(bsdar, opt);
209			break;
210		case 'r':
211			set_mode(bsdar, opt);
212			break;
213		case 'S':
214			bsdar->options |= AR_SS;
215			break;
216		case 's':
217			bsdar->options |= AR_S;
218			break;
219		case 't':
220			set_mode(bsdar, opt);
221			break;
222		case 'U':
223			bsdar->options &= ~AR_D;
224			break;
225		case 'u':
226			bsdar->options |= AR_U;
227			break;
228		case 'V':
229			bsdar_version();
230			break;
231		case 'v':
232			bsdar->options |= AR_V;
233			break;
234		case 'x':
235			set_mode(bsdar, opt);
236			break;
237		case 'z':
238			/* ignored */
239			break;
240		case OPTION_HELP:
241			bsdar_usage();
242		default:
243			bsdar_usage();
244		}
245	}
246
247	argv += optind;
248	argc -= optind;
249
250	if (*argv == NULL && bsdar->mode != 'M')
251		bsdar_usage();
252
253	if (bsdar->options & AR_A && bsdar->options & AR_B)
254		bsdar_errc(bsdar, EX_USAGE, 0,
255		    "only one of -a and -[bi] options allowed");
256
257	if (bsdar->options & AR_J && bsdar->options & AR_Z)
258		bsdar_errc(bsdar, EX_USAGE, 0,
259		    "only one of -j and -z options allowed");
260
261	if (bsdar->options & AR_S && bsdar->options & AR_SS)
262		bsdar_errc(bsdar, EX_USAGE, 0,
263		    "only one of -s and -S options allowed");
264
265	if (bsdar->options & (AR_A | AR_B)) {
266		if ((bsdar->posarg = *argv) == NULL)
267			bsdar_errc(bsdar, EX_USAGE, 0,
268			    "no position operand specified");
269		if ((bsdar->posarg = basename(bsdar->posarg)) == NULL)
270			bsdar_errc(bsdar, EX_SOFTWARE, errno,
271			    "basename failed");
272		argc--;
273		argv++;
274	}
275
276	if (bsdar->options & AR_A)
277		only_mode(bsdar, "-a", "mqr");
278	if (bsdar->options & AR_B)
279		only_mode(bsdar, "-b", "mqr");
280	if (bsdar->options & AR_C)
281		only_mode(bsdar, "-c", "qr");
282	if (bsdar->options & AR_CC)
283		only_mode(bsdar, "-C", "x");
284	if (bsdar->options & AR_D)
285		only_mode(bsdar, "-D", "qr");
286	if (bsdar->options & AR_O)
287		only_mode(bsdar, "-o", "x");
288	if (bsdar->options & AR_SS)
289		only_mode(bsdar, "-S", "mqr");
290	if (bsdar->options & AR_U)
291		only_mode(bsdar, "-u", "qrx");
292
293	if (bsdar->mode == 'M') {
294		ar_mode_script(bsdar);
295		exit(EX_OK);
296	}
297
298	if ((bsdar->filename = *argv) == NULL)
299		bsdar_usage();
300
301	bsdar->argc = --argc;
302	bsdar->argv = ++argv;
303
304	if ((!bsdar->mode || strchr("ptx", bsdar->mode)) &&
305	    bsdar->options & AR_S) {
306		ar_mode_s(bsdar);
307		if (!bsdar->mode)
308			exit(EX_OK);
309	}
310
311	switch(bsdar->mode) {
312	case 'd':
313		ar_mode_d(bsdar);
314		break;
315	case 'm':
316		ar_mode_m(bsdar);
317		break;
318	case 'p':
319		ar_mode_p(bsdar);
320		break;
321	case 'q':
322		ar_mode_q(bsdar);
323		break;
324	case 'r':
325		ar_mode_r(bsdar);
326		break;
327	case 't':
328		ar_mode_t(bsdar);
329		break;
330	case 'x':
331		ar_mode_x(bsdar);
332		break;
333	default:
334		bsdar_usage();
335		/* NOTREACHED */
336	}
337
338	for (i = 0; i < bsdar->argc; i++)
339		if (bsdar->argv[i] != NULL)
340			bsdar_warnc(bsdar, 0, "%s: not found in archive",
341			    bsdar->argv[i]);
342
343	exit(EX_OK);
344}
345
346static void
347set_mode(struct bsdar *bsdar, char opt)
348{
349
350	if (bsdar->mode != '\0' && bsdar->mode != opt)
351		bsdar_errc(bsdar, EX_USAGE, 0,
352		    "Can't specify both -%c and -%c", opt, bsdar->mode);
353	bsdar->mode = opt;
354}
355
356static void
357only_mode(struct bsdar *bsdar, const char *opt, const char *valid_modes)
358{
359
360	if (strchr(valid_modes, bsdar->mode) == NULL)
361		bsdar_errc(bsdar, EX_USAGE, 0,
362		    "Option %s is not permitted in mode -%c", opt, bsdar->mode);
363}
364
365static void
366bsdar_usage(void)
367{
368
369	(void)fprintf(stderr, "usage:  ar -d [-Tjsvz] archive file ...\n");
370	(void)fprintf(stderr, "\tar -m [-Tjsvz] archive file ...\n");
371	(void)fprintf(stderr, "\tar -m [-Tabijsvz] position archive file ...\n");
372	(void)fprintf(stderr, "\tar -p [-Tv] archive [file ...]\n");
373	(void)fprintf(stderr, "\tar -q [-TcDjsUvz] archive file ...\n");
374	(void)fprintf(stderr, "\tar -r [-TcDjsUuvz] archive file ...\n");
375	(void)fprintf(stderr, "\tar -r [-TabcDijsUuvz] position archive file ...\n");
376	(void)fprintf(stderr, "\tar -s [-jz] archive\n");
377	(void)fprintf(stderr, "\tar -t [-Tv] archive [file ...]\n");
378	(void)fprintf(stderr, "\tar -x [-CTouv] archive [file ...]\n");
379	(void)fprintf(stderr, "\tar -V\n");
380	exit(EX_USAGE);
381}
382
383static void
384ranlib_usage(void)
385{
386
387	(void)fprintf(stderr, "usage:	ranlib [-DtU] archive ...\n");
388	(void)fprintf(stderr, "\tranlib -V\n");
389	exit(EX_USAGE);
390}
391
392static void
393bsdar_version(void)
394{
395	(void)printf("BSD ar %s - %s\n", BSDAR_VERSION, archive_version_string());
396	exit(EX_OK);
397}
398
399static void
400ranlib_version(void)
401{
402	(void)printf("ranlib %s - %s\n", BSDAR_VERSION, archive_version_string());
403	exit(EX_OK);
404}
405