xref: /freebsd/usr.bin/ar/ar.c (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2007 Kai Wang
5  * Copyright (c) 2007 Tim Kientzle
6  * Copyright (c) 2007 Joseph Koshy
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 /*-
32  * Copyright (c) 1990, 1993, 1994
33  *	The Regents of the University of California.  All rights reserved.
34  *
35  * This code is derived from software contributed to Berkeley by
36  * Hugh Smith at The University of Guelph.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. Neither the name of the University nor the names of its contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  */
62 
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
65 
66 #include <sys/queue.h>
67 #include <sys/types.h>
68 #include <archive.h>
69 #include <errno.h>
70 #include <getopt.h>
71 #include <libgen.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <sysexits.h>
76 
77 #include "ar.h"
78 
79 enum options
80 {
81 	OPTION_HELP
82 };
83 
84 static struct option longopts[] =
85 {
86 	{"help", no_argument, NULL, OPTION_HELP},
87 	{"version", no_argument, NULL, 'V'},
88 	{NULL, 0, NULL, 0}
89 };
90 
91 static void	bsdar_usage(void);
92 static void	ranlib_usage(void);
93 static void	set_mode(struct bsdar *bsdar, char opt);
94 static void	only_mode(struct bsdar *bsdar, const char *opt,
95 		    const char *valid_modes);
96 static void	bsdar_version(void);
97 static void	ranlib_version(void);
98 
99 int
100 main(int argc, char **argv)
101 {
102 	struct bsdar	*bsdar, bsdar_storage;
103 	char		*p;
104 	size_t		 len;
105 	int		 i, opt, Dflag, Uflag;
106 
107 	bsdar = &bsdar_storage;
108 	memset(bsdar, 0, sizeof(*bsdar));
109 	Dflag = 0;
110 	Uflag = 0;
111 
112 	if ((bsdar->progname = getprogname()) == NULL)
113 		bsdar->progname = "ar";
114 
115 	/* Act like ranlib if our name ends in "ranlib"; this
116 	 * accommodates arm-freebsd7.1-ranlib, bsdranlib, etc. */
117 	len = strlen(bsdar->progname);
118 	if (len >= strlen("ranlib") &&
119 	    strcmp(bsdar->progname + len - strlen("ranlib"), "ranlib") == 0) {
120 		while ((opt = getopt_long(argc, argv, "tDUV", longopts,
121 		    NULL)) != -1) {
122 			switch(opt) {
123 			case 't':
124 				/* Ignored. */
125 				break;
126 			case 'D':
127 				Dflag = 1;
128 				Uflag = 0;
129 				break;
130 			case 'U':
131 				Uflag = 1;
132 				Dflag = 0;
133 				break;
134 			case 'V':
135 				ranlib_version();
136 				break;
137 			case OPTION_HELP:
138 				ranlib_usage();
139 			default:
140 				ranlib_usage();
141 			}
142 		}
143 		argv += optind;
144 		argc -= optind;
145 
146 		if (*argv == NULL)
147 			ranlib_usage();
148 
149 		/* Enable determinstic mode unless -U is set. */
150 		if (Uflag == 0)
151 			bsdar->options |= AR_D;
152 		bsdar->options |= AR_S;
153 		while ((bsdar->filename = *argv++) != NULL)
154 			ar_mode_s(bsdar);
155 
156 		exit(EX_OK);
157 	} else {
158 		if (argc < 2)
159 			bsdar_usage();
160 
161 		if (*argv[1] != '-') {
162 			len = strlen(argv[1]) + 2;
163 			if ((p = malloc(len)) == NULL)
164 				bsdar_errc(bsdar, EX_SOFTWARE, errno,
165 				    "malloc failed");
166 			*p = '-';
167 			(void)strlcpy(p + 1, argv[1], len - 1);
168 			argv[1] = p;
169 		}
170 	}
171 
172 	while ((opt = getopt_long(argc, argv, "abCcdDfijlMmopqrSsTtUuVvxz",
173 	    longopts, NULL)) != -1) {
174 		switch(opt) {
175 		case 'a':
176 			bsdar->options |= AR_A;
177 			break;
178 		case 'b':
179 		case 'i':
180 			bsdar->options |= AR_B;
181 			break;
182 		case 'C':
183 			bsdar->options |= AR_CC;
184 			break;
185 		case 'c':
186 			bsdar->options |= AR_C;
187 			break;
188 		case 'd':
189 			set_mode(bsdar, opt);
190 			break;
191 		case 'D':
192 			Dflag = 1;
193 			Uflag = 0;
194 			break;
195 		case 'f':
196 		case 'T':
197 			bsdar->options |= AR_TR;
198 			break;
199 		case 'j':
200 			/* ignored */
201 			break;
202 		case 'l':
203 			/* ignored, for GNU ar comptibility */
204 			break;
205 		case 'M':
206 			set_mode(bsdar, opt);
207 			break;
208 		case 'm':
209 			set_mode(bsdar, opt);
210 			break;
211 		case 'o':
212 			bsdar->options |= AR_O;
213 			break;
214 		case 'p':
215 			set_mode(bsdar, opt);
216 			break;
217 		case 'q':
218 			set_mode(bsdar, opt);
219 			break;
220 		case 'r':
221 			set_mode(bsdar, opt);
222 			break;
223 		case 'S':
224 			bsdar->options |= AR_SS;
225 			break;
226 		case 's':
227 			bsdar->options |= AR_S;
228 			break;
229 		case 't':
230 			set_mode(bsdar, opt);
231 			break;
232 		case 'U':
233 			Uflag = 1;
234 			Dflag = 0;
235 			break;
236 		case 'u':
237 			bsdar->options |= AR_U;
238 			break;
239 		case 'V':
240 			bsdar_version();
241 			break;
242 		case 'v':
243 			bsdar->options |= AR_V;
244 			break;
245 		case 'x':
246 			set_mode(bsdar, opt);
247 			break;
248 		case 'z':
249 			/* ignored */
250 			break;
251 		case OPTION_HELP:
252 			bsdar_usage();
253 		default:
254 			bsdar_usage();
255 		}
256 	}
257 
258 	argv += optind;
259 	argc -= optind;
260 
261 	if (*argv == NULL && bsdar->mode != 'M')
262 		bsdar_usage();
263 
264 	if (bsdar->options & AR_A && bsdar->options & AR_B)
265 		bsdar_errc(bsdar, EX_USAGE, 0,
266 		    "only one of -a and -[bi] options allowed");
267 
268 	if (bsdar->options & AR_J && bsdar->options & AR_Z)
269 		bsdar_errc(bsdar, EX_USAGE, 0,
270 		    "only one of -j and -z options allowed");
271 
272 	if (bsdar->options & AR_S && bsdar->options & AR_SS)
273 		bsdar_errc(bsdar, EX_USAGE, 0,
274 		    "only one of -s and -S options allowed");
275 
276 	if (bsdar->options & (AR_A | AR_B)) {
277 		if (*argv == NULL)
278 			bsdar_errc(bsdar, EX_USAGE, 0,
279 			    "no position operand specified");
280 		if ((bsdar->posarg = basename(*argv)) == NULL)
281 			bsdar_errc(bsdar, EX_SOFTWARE, errno,
282 			    "basename failed");
283 		argc--;
284 		argv++;
285 	}
286 
287 	/* Set determinstic mode for -D, and by default without -U. */
288 	if (Dflag || (Uflag == 0 && (bsdar->mode == 'q' || bsdar->mode == 'r' ||
289 	    (bsdar->mode == '\0' && bsdar->options & AR_S))))
290 		bsdar->options |= AR_D;
291 
292 	if (bsdar->options & AR_A)
293 		only_mode(bsdar, "-a", "mqr");
294 	if (bsdar->options & AR_B)
295 		only_mode(bsdar, "-b", "mqr");
296 	if (bsdar->options & AR_C)
297 		only_mode(bsdar, "-c", "qr");
298 	if (bsdar->options & AR_CC)
299 		only_mode(bsdar, "-C", "x");
300 	if (Dflag)
301 		only_mode(bsdar, "-D", "qr");
302 	if (Uflag)
303 		only_mode(bsdar, "-U", "qr");
304 	if (bsdar->options & AR_O)
305 		only_mode(bsdar, "-o", "x");
306 	if (bsdar->options & AR_SS)
307 		only_mode(bsdar, "-S", "mqr");
308 	if (bsdar->options & AR_U)
309 		only_mode(bsdar, "-u", "qrx");
310 
311 	if (bsdar->mode == 'M') {
312 		ar_mode_script(bsdar);
313 		exit(EX_OK);
314 	}
315 
316 	if ((bsdar->filename = *argv) == NULL)
317 		bsdar_usage();
318 
319 	bsdar->argc = --argc;
320 	bsdar->argv = ++argv;
321 
322 	if ((!bsdar->mode || strchr("ptx", bsdar->mode)) &&
323 	    bsdar->options & AR_S) {
324 		ar_mode_s(bsdar);
325 		if (!bsdar->mode)
326 			exit(EX_OK);
327 	}
328 
329 	switch(bsdar->mode) {
330 	case 'd':
331 		ar_mode_d(bsdar);
332 		break;
333 	case 'm':
334 		ar_mode_m(bsdar);
335 		break;
336 	case 'p':
337 		ar_mode_p(bsdar);
338 		break;
339 	case 'q':
340 		ar_mode_q(bsdar);
341 		break;
342 	case 'r':
343 		ar_mode_r(bsdar);
344 		break;
345 	case 't':
346 		ar_mode_t(bsdar);
347 		break;
348 	case 'x':
349 		ar_mode_x(bsdar);
350 		break;
351 	default:
352 		bsdar_usage();
353 		/* NOTREACHED */
354 	}
355 
356 	for (i = 0; i < bsdar->argc; i++)
357 		if (bsdar->argv[i] != NULL)
358 			bsdar_warnc(bsdar, 0, "%s: not found in archive",
359 			    bsdar->argv[i]);
360 
361 	exit(EX_OK);
362 }
363 
364 static void
365 set_mode(struct bsdar *bsdar, char opt)
366 {
367 
368 	if (bsdar->mode != '\0' && bsdar->mode != opt)
369 		bsdar_errc(bsdar, EX_USAGE, 0,
370 		    "Can't specify both -%c and -%c", opt, bsdar->mode);
371 	bsdar->mode = opt;
372 }
373 
374 static void
375 only_mode(struct bsdar *bsdar, const char *opt, const char *valid_modes)
376 {
377 
378 	if (strchr(valid_modes, bsdar->mode) == NULL)
379 		bsdar_errc(bsdar, EX_USAGE, 0,
380 		    "Option %s is not permitted in mode -%c", opt, bsdar->mode);
381 }
382 
383 static void
384 bsdar_usage(void)
385 {
386 
387 	(void)fprintf(stderr, "usage:  ar -d [-Tjsvz] archive file ...\n");
388 	(void)fprintf(stderr, "\tar -m [-Tjsvz] archive file ...\n");
389 	(void)fprintf(stderr, "\tar -m [-Tabijsvz] position archive file ...\n");
390 	(void)fprintf(stderr, "\tar -p [-Tv] archive [file ...]\n");
391 	(void)fprintf(stderr, "\tar -q [-TcDjsUvz] archive file ...\n");
392 	(void)fprintf(stderr, "\tar -r [-TcDjsUuvz] archive file ...\n");
393 	(void)fprintf(stderr, "\tar -r [-TabcDijsUuvz] position archive file ...\n");
394 	(void)fprintf(stderr, "\tar -s [-jz] archive\n");
395 	(void)fprintf(stderr, "\tar -t [-Tv] archive [file ...]\n");
396 	(void)fprintf(stderr, "\tar -x [-CTouv] archive [file ...]\n");
397 	(void)fprintf(stderr, "\tar -V\n");
398 	exit(EX_USAGE);
399 }
400 
401 static void
402 ranlib_usage(void)
403 {
404 
405 	(void)fprintf(stderr, "usage:	ranlib [-DtU] archive ...\n");
406 	(void)fprintf(stderr, "\tranlib -V\n");
407 	exit(EX_USAGE);
408 }
409 
410 static void
411 bsdar_version(void)
412 {
413 	(void)printf("BSD ar %s - %s\n", BSDAR_VERSION, archive_version_string());
414 	exit(EX_OK);
415 }
416 
417 static void
418 ranlib_version(void)
419 {
420 	(void)printf("ranlib %s - %s\n", BSDAR_VERSION, archive_version_string());
421 	exit(EX_OK);
422 }
423