xref: /freebsd/usr.bin/ar/ar.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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 <err.h>
70 #include <errno.h>
71 #include <getopt.h>
72 #include <libgen.h>
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <string.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		 exitcode, i, opt, Dflag, Uflag;
106 
107 	bsdar = &bsdar_storage;
108 	memset(bsdar, 0, sizeof(*bsdar));
109 	exitcode = EXIT_SUCCESS;
110 	Dflag = 0;
111 	Uflag = 0;
112 
113 	if ((bsdar->progname = getprogname()) == NULL)
114 		bsdar->progname = "ar";
115 
116 	/* Act like ranlib if our name ends in "ranlib"; this
117 	 * accommodates arm-freebsd7.1-ranlib, bsdranlib, etc. */
118 	len = strlen(bsdar->progname);
119 	if (len >= strlen("ranlib") &&
120 	    strcmp(bsdar->progname + len - strlen("ranlib"), "ranlib") == 0) {
121 		while ((opt = getopt_long(argc, argv, "tDUV", longopts,
122 		    NULL)) != -1) {
123 			switch(opt) {
124 			case 't':
125 				/* Ignored. */
126 				break;
127 			case 'D':
128 				Dflag = 1;
129 				Uflag = 0;
130 				break;
131 			case 'U':
132 				Uflag = 1;
133 				Dflag = 0;
134 				break;
135 			case 'V':
136 				ranlib_version();
137 				break;
138 			case OPTION_HELP:
139 				ranlib_usage();
140 			default:
141 				ranlib_usage();
142 			}
143 		}
144 		argv += optind;
145 		argc -= optind;
146 
147 		if (*argv == NULL)
148 			ranlib_usage();
149 
150 		/* Enable determinstic mode unless -U is set. */
151 		if (Uflag == 0)
152 			bsdar->options |= AR_D;
153 		bsdar->options |= AR_S;
154 		while ((bsdar->filename = *argv++) != NULL)
155 			if (ar_write_archive(bsdar, 's'))
156 				exitcode = EXIT_FAILURE;
157 
158 		exit(exitcode);
159 	} else {
160 		if (argc < 2)
161 			bsdar_usage();
162 
163 		if (*argv[1] != '-') {
164 			len = strlen(argv[1]) + 2;
165 			if ((p = malloc(len)) == NULL)
166 				bsdar_errc(bsdar, errno, "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 			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 			/* ignored */
231 			break;
232 		case 't':
233 			set_mode(bsdar, opt);
234 			break;
235 		case 'U':
236 			Uflag = 1;
237 			Dflag = 0;
238 			break;
239 		case 'u':
240 			bsdar->options |= AR_U;
241 			break;
242 		case 'V':
243 			bsdar_version();
244 			break;
245 		case 'v':
246 			bsdar->options |= AR_V;
247 			break;
248 		case 'x':
249 			set_mode(bsdar, opt);
250 			break;
251 		case 'z':
252 			/* ignored */
253 			break;
254 		case OPTION_HELP:
255 			bsdar_usage();
256 		default:
257 			bsdar_usage();
258 		}
259 	}
260 
261 	argv += optind;
262 	argc -= optind;
263 
264 	if (*argv == NULL && bsdar->mode != 'M')
265 		bsdar_usage();
266 
267 	if (bsdar->options & AR_A && bsdar->options & AR_B)
268 		bsdar_errc(bsdar, 0,
269 		    "only one of -a and -[bi] options allowed");
270 
271 	if (bsdar->options & AR_J && bsdar->options & AR_Z)
272 		bsdar_errc(bsdar, 0, "only one of -j and -z options allowed");
273 
274 	if (bsdar->options & AR_S && bsdar->options & AR_SS)
275 		bsdar_errc(bsdar, 0, "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, 0, "no position operand specified");
280 		if ((bsdar->posarg = basename(*argv)) == NULL)
281 			bsdar_errc(bsdar, errno, "basename failed");
282 		argc--;
283 		argv++;
284 	}
285 
286 	/* Set determinstic mode for -D, and by default without -U. */
287 	if (Dflag || (Uflag == 0 && (bsdar->mode == 'q' || bsdar->mode == 'r' ||
288 	    (bsdar->mode == '\0' && bsdar->options & AR_S))))
289 		bsdar->options |= AR_D;
290 
291 	if (bsdar->options & AR_A)
292 		only_mode(bsdar, "-a", "mqr");
293 	if (bsdar->options & AR_B)
294 		only_mode(bsdar, "-b", "mqr");
295 	if (bsdar->options & AR_C)
296 		only_mode(bsdar, "-c", "qr");
297 	if (bsdar->options & AR_CC)
298 		only_mode(bsdar, "-C", "x");
299 	if (Dflag)
300 		only_mode(bsdar, "-D", "qr");
301 	if (Uflag)
302 		only_mode(bsdar, "-U", "qr");
303 	if (bsdar->options & AR_O)
304 		only_mode(bsdar, "-o", "x");
305 	if (bsdar->options & AR_SS)
306 		only_mode(bsdar, "-S", "mqr");
307 	if (bsdar->options & AR_U)
308 		only_mode(bsdar, "-u", "qrx");
309 
310 	if (bsdar->mode == 'M') {
311 		ar_mode_script(bsdar);
312 		exit(EXIT_SUCCESS);
313 	}
314 
315 	if ((bsdar->filename = *argv) == NULL)
316 		bsdar_usage();
317 
318 	bsdar->argc = --argc;
319 	bsdar->argv = ++argv;
320 
321 	if ((!bsdar->mode || strchr("ptx", bsdar->mode)) &&
322 	    bsdar->options & AR_S) {
323 		exitcode = ar_write_archive(bsdar, 's');
324 		if (!bsdar->mode)
325 			exit(exitcode);
326 	}
327 
328 	switch(bsdar->mode) {
329 	case 'd': case 'm': case 'q': case 'r':
330 		exitcode = ar_write_archive(bsdar, bsdar->mode);
331 		break;
332 	case 'p': case 't': case 'x':
333 		exitcode = ar_read_archive(bsdar, bsdar->mode, stdout);
334 		break;
335 	default:
336 		bsdar_usage();
337 		/* NOTREACHED */
338 	}
339 
340 	for (i = 0; i < bsdar->argc; i++) {
341 		if (bsdar->argv[i] != NULL) {
342 			bsdar_warnc(bsdar, 0, "%s: not found in archive",
343 			    bsdar->argv[i]);
344 			exitcode = EXIT_FAILURE;
345 		}
346 	}
347 
348 	exit(exitcode);
349 }
350 
351 static void
352 set_mode(struct bsdar *bsdar, char opt)
353 {
354 
355 	if (bsdar->mode != '\0' && bsdar->mode != opt)
356 		bsdar_errc(bsdar, 0, "Can't specify both -%c and -%c", opt,
357 		    bsdar->mode);
358 	bsdar->mode = opt;
359 }
360 
361 static void
362 only_mode(struct bsdar *bsdar, const char *opt, const char *valid_modes)
363 {
364 
365 	if (strchr(valid_modes, bsdar->mode) == NULL)
366 		bsdar_errc(bsdar, 0, "Option %s is not permitted in mode -%c",
367 		    opt, bsdar->mode);
368 }
369 
370 static void
371 bsdar_usage(void)
372 {
373 
374 	(void)fprintf(stderr, "usage:  ar -d [-Tjsvz] archive file ...\n");
375 	(void)fprintf(stderr, "\tar -m [-Tjsvz] archive file ...\n");
376 	(void)fprintf(stderr, "\tar -m [-Tabijsvz] position archive file ...\n");
377 	(void)fprintf(stderr, "\tar -p [-Tv] archive [file ...]\n");
378 	(void)fprintf(stderr, "\tar -q [-TcDjsUvz] archive file ...\n");
379 	(void)fprintf(stderr, "\tar -r [-TcDjsUuvz] archive file ...\n");
380 	(void)fprintf(stderr, "\tar -r [-TabcDijsUuvz] position archive file ...\n");
381 	(void)fprintf(stderr, "\tar -s [-jz] archive\n");
382 	(void)fprintf(stderr, "\tar -t [-Tv] archive [file ...]\n");
383 	(void)fprintf(stderr, "\tar -x [-CTouv] archive [file ...]\n");
384 	(void)fprintf(stderr, "\tar -V\n");
385 	exit(EXIT_FAILURE);
386 }
387 
388 static void
389 ranlib_usage(void)
390 {
391 
392 	(void)fprintf(stderr, "usage:	ranlib [-DtU] archive ...\n");
393 	(void)fprintf(stderr, "\tranlib -V\n");
394 	exit(EXIT_FAILURE);
395 }
396 
397 static void
398 bsdar_version(void)
399 {
400 	(void)printf("BSD ar %s - %s\n", BSDAR_VERSION, archive_version_string());
401 	exit(EXIT_SUCCESS);
402 }
403 
404 static void
405 ranlib_version(void)
406 {
407 	(void)printf("ranlib %s - %s\n", BSDAR_VERSION, archive_version_string());
408 	exit(EXIT_SUCCESS);
409 }
410