xref: /freebsd/usr.bin/ar/ar.c (revision 38911b3c2c7dbb9a097b44856472ebbbedde71fc)
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 
76 #include "ar.h"
77 
78 enum options
79 {
80 	OPTION_HELP
81 };
82 
83 static struct option longopts[] =
84 {
85 	{"help", no_argument, NULL, OPTION_HELP},
86 	{"version", no_argument, NULL, 'V'},
87 	{NULL, 0, NULL, 0}
88 };
89 
90 static void	bsdar_usage(void);
91 static void	ranlib_usage(void);
92 static void	set_mode(struct bsdar *bsdar, char opt);
93 static void	only_mode(struct bsdar *bsdar, const char *opt,
94 		    const char *valid_modes);
95 static void	bsdar_version(void);
96 static void	ranlib_version(void);
97 
98 int
99 main(int argc, char **argv)
100 {
101 	struct bsdar	*bsdar, bsdar_storage;
102 	char		*p;
103 	size_t		 len;
104 	int		 exitcode, i, opt, Dflag, Uflag;
105 
106 	bsdar = &bsdar_storage;
107 	memset(bsdar, 0, sizeof(*bsdar));
108 	exitcode = EXIT_SUCCESS;
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 			if (ar_mode_s(bsdar))
155 				exitcode = EXIT_FAILURE;
156 
157 		exit(exitcode);
158 	} else {
159 		if (argc < 2)
160 			bsdar_usage();
161 
162 		if (*argv[1] != '-') {
163 			len = strlen(argv[1]) + 2;
164 			if ((p = malloc(len)) == NULL)
165 				bsdar_errc(bsdar, EXIT_FAILURE, errno,
166 				    "malloc failed");
167 			*p = '-';
168 			(void)strlcpy(p + 1, argv[1], len - 1);
169 			argv[1] = p;
170 		}
171 	}
172 
173 	while ((opt = getopt_long(argc, argv, "abCcdDfijlMmopqrSsTtUuVvxz",
174 	    longopts, NULL)) != -1) {
175 		switch(opt) {
176 		case 'a':
177 			bsdar->options |= AR_A;
178 			break;
179 		case 'b':
180 		case 'i':
181 			bsdar->options |= AR_B;
182 			break;
183 		case 'C':
184 			bsdar->options |= AR_CC;
185 			break;
186 		case 'c':
187 			bsdar->options |= AR_C;
188 			break;
189 		case 'd':
190 			set_mode(bsdar, opt);
191 			break;
192 		case 'D':
193 			Dflag = 1;
194 			Uflag = 0;
195 			break;
196 		case 'f':
197 		case 'T':
198 			bsdar->options |= AR_TR;
199 			break;
200 		case 'j':
201 			/* ignored */
202 			break;
203 		case 'l':
204 			/* ignored, for GNU ar comptibility */
205 			break;
206 		case 'M':
207 			set_mode(bsdar, opt);
208 			break;
209 		case 'm':
210 			set_mode(bsdar, opt);
211 			break;
212 		case 'o':
213 			bsdar->options |= AR_O;
214 			break;
215 		case 'p':
216 			set_mode(bsdar, opt);
217 			break;
218 		case 'q':
219 			set_mode(bsdar, opt);
220 			break;
221 		case 'r':
222 			set_mode(bsdar, opt);
223 			break;
224 		case 'S':
225 			bsdar->options |= AR_SS;
226 			break;
227 		case 's':
228 			bsdar->options |= AR_S;
229 			break;
230 		case 't':
231 			set_mode(bsdar, opt);
232 			break;
233 		case 'U':
234 			Uflag = 1;
235 			Dflag = 0;
236 			break;
237 		case 'u':
238 			bsdar->options |= AR_U;
239 			break;
240 		case 'V':
241 			bsdar_version();
242 			break;
243 		case 'v':
244 			bsdar->options |= AR_V;
245 			break;
246 		case 'x':
247 			set_mode(bsdar, opt);
248 			break;
249 		case 'z':
250 			/* ignored */
251 			break;
252 		case OPTION_HELP:
253 			bsdar_usage();
254 		default:
255 			bsdar_usage();
256 		}
257 	}
258 
259 	argv += optind;
260 	argc -= optind;
261 
262 	if (*argv == NULL && bsdar->mode != 'M')
263 		bsdar_usage();
264 
265 	if (bsdar->options & AR_A && bsdar->options & AR_B)
266 		bsdar_errc(bsdar, EXIT_FAILURE, 0,
267 		    "only one of -a and -[bi] options allowed");
268 
269 	if (bsdar->options & AR_J && bsdar->options & AR_Z)
270 		bsdar_errc(bsdar, EXIT_FAILURE, 0,
271 		    "only one of -j and -z options allowed");
272 
273 	if (bsdar->options & AR_S && bsdar->options & AR_SS)
274 		bsdar_errc(bsdar, EXIT_FAILURE, 0,
275 		    "only one of -s and -S options allowed");
276 
277 	if (bsdar->options & (AR_A | AR_B)) {
278 		if (*argv == NULL)
279 			bsdar_errc(bsdar, EXIT_FAILURE, 0,
280 			    "no position operand specified");
281 		if ((bsdar->posarg = basename(*argv)) == NULL)
282 			bsdar_errc(bsdar, EXIT_FAILURE, errno,
283 			    "basename failed");
284 		argc--;
285 		argv++;
286 	}
287 
288 	/* Set determinstic mode for -D, and by default without -U. */
289 	if (Dflag || (Uflag == 0 && (bsdar->mode == 'q' || bsdar->mode == 'r' ||
290 	    (bsdar->mode == '\0' && bsdar->options & AR_S))))
291 		bsdar->options |= AR_D;
292 
293 	if (bsdar->options & AR_A)
294 		only_mode(bsdar, "-a", "mqr");
295 	if (bsdar->options & AR_B)
296 		only_mode(bsdar, "-b", "mqr");
297 	if (bsdar->options & AR_C)
298 		only_mode(bsdar, "-c", "qr");
299 	if (bsdar->options & AR_CC)
300 		only_mode(bsdar, "-C", "x");
301 	if (Dflag)
302 		only_mode(bsdar, "-D", "qr");
303 	if (Uflag)
304 		only_mode(bsdar, "-U", "qr");
305 	if (bsdar->options & AR_O)
306 		only_mode(bsdar, "-o", "x");
307 	if (bsdar->options & AR_SS)
308 		only_mode(bsdar, "-S", "mqr");
309 	if (bsdar->options & AR_U)
310 		only_mode(bsdar, "-u", "qrx");
311 
312 	if (bsdar->mode == 'M') {
313 		ar_mode_script(bsdar);
314 		exit(EXIT_SUCCESS);
315 	}
316 
317 	if ((bsdar->filename = *argv) == NULL)
318 		bsdar_usage();
319 
320 	bsdar->argc = --argc;
321 	bsdar->argv = ++argv;
322 
323 	if ((!bsdar->mode || strchr("ptx", bsdar->mode)) &&
324 	    bsdar->options & AR_S) {
325 		exitcode = ar_mode_s(bsdar);
326 		if (!bsdar->mode)
327 			exit(exitcode);
328 	}
329 
330 	switch(bsdar->mode) {
331 	case 'd':
332 		exitcode = ar_mode_d(bsdar);
333 		break;
334 	case 'm':
335 		exitcode = ar_mode_m(bsdar);
336 		break;
337 	case 'p':
338 		exitcode = ar_mode_p(bsdar);
339 		break;
340 	case 'q':
341 		exitcode = ar_mode_q(bsdar);
342 		break;
343 	case 'r':
344 		exitcode = ar_mode_r(bsdar);
345 		break;
346 	case 't':
347 		exitcode = ar_mode_t(bsdar);
348 		break;
349 	case 'x':
350 		exitcode = ar_mode_x(bsdar);
351 		break;
352 	default:
353 		bsdar_usage();
354 		/* NOTREACHED */
355 	}
356 
357 	for (i = 0; i < bsdar->argc; i++) {
358 		if (bsdar->argv[i] != NULL) {
359 			bsdar_warnc(bsdar, 0, "%s: not found in archive",
360 			    bsdar->argv[i]);
361 			exitcode = EXIT_FAILURE;
362 		}
363 	}
364 
365 	exit(exitcode);
366 }
367 
368 static void
369 set_mode(struct bsdar *bsdar, char opt)
370 {
371 
372 	if (bsdar->mode != '\0' && bsdar->mode != opt)
373 		bsdar_errc(bsdar, EXIT_FAILURE, 0,
374 		    "Can't specify both -%c and -%c", opt, bsdar->mode);
375 	bsdar->mode = opt;
376 }
377 
378 static void
379 only_mode(struct bsdar *bsdar, const char *opt, const char *valid_modes)
380 {
381 
382 	if (strchr(valid_modes, bsdar->mode) == NULL)
383 		bsdar_errc(bsdar, EXIT_FAILURE, 0,
384 		    "Option %s is not permitted in mode -%c", opt, bsdar->mode);
385 }
386 
387 static void
388 bsdar_usage(void)
389 {
390 
391 	(void)fprintf(stderr, "usage:  ar -d [-Tjsvz] archive file ...\n");
392 	(void)fprintf(stderr, "\tar -m [-Tjsvz] archive file ...\n");
393 	(void)fprintf(stderr, "\tar -m [-Tabijsvz] position archive file ...\n");
394 	(void)fprintf(stderr, "\tar -p [-Tv] archive [file ...]\n");
395 	(void)fprintf(stderr, "\tar -q [-TcDjsUvz] archive file ...\n");
396 	(void)fprintf(stderr, "\tar -r [-TcDjsUuvz] archive file ...\n");
397 	(void)fprintf(stderr, "\tar -r [-TabcDijsUuvz] position archive file ...\n");
398 	(void)fprintf(stderr, "\tar -s [-jz] archive\n");
399 	(void)fprintf(stderr, "\tar -t [-Tv] archive [file ...]\n");
400 	(void)fprintf(stderr, "\tar -x [-CTouv] archive [file ...]\n");
401 	(void)fprintf(stderr, "\tar -V\n");
402 	exit(EXIT_FAILURE);
403 }
404 
405 static void
406 ranlib_usage(void)
407 {
408 
409 	(void)fprintf(stderr, "usage:	ranlib [-DtU] archive ...\n");
410 	(void)fprintf(stderr, "\tranlib -V\n");
411 	exit(EXIT_FAILURE);
412 }
413 
414 static void
415 bsdar_version(void)
416 {
417 	(void)printf("BSD ar %s - %s\n", BSDAR_VERSION, archive_version_string());
418 	exit(EXIT_SUCCESS);
419 }
420 
421 static void
422 ranlib_version(void)
423 {
424 	(void)printf("ranlib %s - %s\n", BSDAR_VERSION, archive_version_string());
425 	exit(EXIT_SUCCESS);
426 }
427