xref: /freebsd/contrib/elftoolchain/ar/ar.c (revision 389e4940069316fe667ffa263fa7d6390d0a960f)
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/queue.h>
62 #include <sys/types.h>
63 #include <archive.h>
64 #include <errno.h>
65 #include <getopt.h>
66 #include <libelftc.h>
67 #include <libgen.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <stdint.h>
71 #include <string.h>
72 
73 #include "ar.h"
74 
75 ELFTC_VCSID("$Id: ar.c 3319 2016-01-13 21:37:53Z jkoshy $");
76 
77 enum options
78 {
79 	OPTION_HELP
80 };
81 
82 static struct option longopts[] =
83 {
84 	{"flavor", required_argument, NULL, 'F'},
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 
97 int
98 main(int argc, char **argv)
99 {
100 	struct bsdar	*bsdar, bsdar_storage;
101 	char		*arcmd, *argv1_saved;
102 	size_t		 len;
103 	int		 i, opt;
104 
105 	bsdar = &bsdar_storage;
106 	memset(bsdar, 0, sizeof(*bsdar));
107 
108 	arcmd = argv1_saved = NULL;
109 	bsdar->output = stdout;
110 
111 	if ((bsdar->progname = ELFTC_GETPROGNAME()) == NULL)
112 		bsdar->progname = "ar";
113 
114 	if (elf_version(EV_CURRENT) == EV_NONE)
115 		bsdar_errc(bsdar, 0, "ELF library initialization failed: %s",
116 		    elf_errmsg(-1));
117 
118 	/*
119 	 * Act like ranlib if our name ends in "ranlib"; this
120 	 * accommodates names like "arm-freebsd7.1-ranlib",
121 	 * "bsdranlib", etc.
122 	 */
123 	len = strlen(bsdar->progname);
124 	if (len >= strlen("ranlib") &&
125 	    strcmp(bsdar->progname + len - strlen("ranlib"), "ranlib") == 0) {
126 		while ((opt = getopt_long(argc, argv, "tDUV", longopts,
127 		    NULL)) != -1) {
128 			switch(opt) {
129 			case 't':
130 				/* Ignored. */
131 				break;
132 			case 'D':
133 				bsdar->options |= AR_D;
134 				break;
135 			case 'U':
136 				bsdar->options &= ~AR_D;
137 				break;
138 			case 'V':
139 				bsdar_version();
140 				break;
141 			case OPTION_HELP:
142 				ranlib_usage();
143 			default:
144 				ranlib_usage();
145 			}
146 		}
147 		argv += optind;
148 		argc -= optind;
149 
150 		if (*argv == NULL)
151 			ranlib_usage();
152 
153 		bsdar->options |= AR_S;
154 		while ((bsdar->filename = *argv++) != NULL)
155 			ar_write_archive(bsdar, 's');
156 
157 		exit(EXIT_SUCCESS);
158 	} else {
159 		if (argc < 2)
160 			bsdar_usage();
161 
162 		/*
163 		 * Tack on a leading '-', for old-style usage.
164 		 */
165 		if (*argv[1] != '-') {
166 			argv1_saved = argv[1];
167 			len = strlen(argv[1]) + 2;
168 			if ((arcmd = malloc(len)) == NULL)
169 				bsdar_errc(bsdar, errno, "malloc failed");
170 			(void) snprintf(arcmd, len, "-%s", argv[1]);
171 			argv[1] = arcmd;
172 		}
173 	}
174 
175 	while ((opt = getopt_long(argc, argv, "abCcdDfF:ijlMmopqrSsTtUuVvxz",
176 	    longopts, NULL)) != -1) {
177 		switch(opt) {
178 		case 'a':
179 			bsdar->options |= AR_A;
180 			break;
181 		case 'b':
182 		case 'i':
183 			bsdar->options |= AR_B;
184 			break;
185 		case 'C':
186 			bsdar->options |= AR_CC;
187 			break;
188 		case 'c':
189 			bsdar->options |= AR_C;
190 			break;
191 		case 'd':
192 			set_mode(bsdar, opt);
193 			break;
194 		case 'D':
195 			bsdar->options |= AR_D;
196 			break;
197 		case 'F':
198 			if (!strcasecmp(optarg, "svr4") ||
199 			    !strcasecmp(optarg, "gnu"))
200 				bsdar->options &= ~AR_BSD;
201 			else if (!strcasecmp(optarg, "bsd"))
202 				bsdar->options |= AR_BSD;
203 			else
204 				bsdar_usage();
205 			break;
206 		case 'f':
207 		case 'T':
208 			bsdar->options |= AR_TR;
209 			break;
210 		case 'j':
211 			/* ignored */
212 			break;
213 		case 'l':
214 			/* ignored, for GNU ar comptibility */
215 			break;
216 		case 'M':
217 			set_mode(bsdar, opt);
218 			break;
219 		case 'm':
220 			set_mode(bsdar, opt);
221 			break;
222 		case 'o':
223 			bsdar->options |= AR_O;
224 			break;
225 		case 'p':
226 			set_mode(bsdar, opt);
227 			break;
228 		case 'q':
229 			set_mode(bsdar, opt);
230 			break;
231 		case 'r':
232 			set_mode(bsdar, opt);
233 			break;
234 		case 'S':
235 			bsdar->options |= AR_SS;
236 			break;
237 		case 's':
238 			bsdar->options |= AR_S;
239 			break;
240 		case 't':
241 			set_mode(bsdar, opt);
242 			break;
243 		case 'U':
244 			bsdar->options &= ~AR_D;
245 			break;
246 		case 'u':
247 			bsdar->options |= AR_U;
248 			break;
249 		case 'V':
250 			bsdar_version();
251 			break;
252 		case 'v':
253 			bsdar->options |= AR_V;
254 			break;
255 		case 'x':
256 			set_mode(bsdar, opt);
257 			break;
258 		case 'z':
259 			/* ignored */
260 			break;
261 		case OPTION_HELP:
262 			bsdar_usage();
263 		default:
264 			bsdar_usage();
265 		}
266 	}
267 
268 	/* Restore argv[1] if we had modified it. */
269 	if (arcmd != NULL) {
270 		argv[1] = argv1_saved;
271 		free(arcmd);
272 		arcmd = argv1_saved = NULL;
273 	}
274 
275 	argv += optind;
276 	argc -= optind;
277 
278 	if (*argv == NULL && bsdar->mode != 'M')
279 		bsdar_usage();
280 
281 	if (bsdar->options & AR_A && bsdar->options & AR_B)
282 		bsdar_errc(bsdar, 0,
283 		    "only one of -a and -[bi] options allowed");
284 
285 	if (bsdar->options & AR_J && bsdar->options & AR_Z)
286 		bsdar_errc(bsdar, 0,
287 		    "only one of -j and -z options allowed");
288 
289 	if (bsdar->options & AR_S && bsdar->options & AR_SS)
290 		bsdar_errc(bsdar, 0,
291 		    "only one of -s and -S options allowed");
292 
293 	if (bsdar->options & (AR_A | AR_B)) {
294 		if (*argv == NULL)
295 			bsdar_errc(bsdar, 0,
296 			    "no position operand specified");
297 		if ((bsdar->posarg = basename(*argv)) == NULL)
298 			bsdar_errc(bsdar, errno,
299 			    "basename failed");
300 		argc--;
301 		argv++;
302 	}
303 
304 	if (bsdar->options & AR_A)
305 		only_mode(bsdar, "-a", "mqr");
306 	if (bsdar->options & AR_B)
307 		only_mode(bsdar, "-b", "mqr");
308 	if (bsdar->options & AR_C)
309 		only_mode(bsdar, "-c", "qr");
310 	if (bsdar->options & AR_CC)
311 		only_mode(bsdar, "-C", "x");
312 	if (bsdar->options & AR_D)
313 		only_mode(bsdar, "-D", "qr");
314 	if (bsdar->options & AR_O)
315 		only_mode(bsdar, "-o", "x");
316 	if (bsdar->options & AR_SS)
317 		only_mode(bsdar, "-S", "mqr");
318 	if (bsdar->options & AR_U)
319 		only_mode(bsdar, "-u", "qrx");
320 
321 	if (bsdar->mode == 'M') {
322 		ar_mode_script(bsdar);
323 		exit(EXIT_SUCCESS);
324 	}
325 
326 	if ((bsdar->filename = *argv) == NULL)
327 		bsdar_usage();
328 
329 	bsdar->argc = --argc;
330 	bsdar->argv = ++argv;
331 
332 	if ((!bsdar->mode || strchr("ptx", bsdar->mode)) &&
333 	    bsdar->options & AR_S) {
334 		ar_write_archive(bsdar, 's');
335 		if (!bsdar->mode)
336 			exit(EXIT_SUCCESS);
337 	}
338 
339 	switch(bsdar->mode) {
340 	case 'd': case 'm': case 'q': case 'r':
341 		ar_write_archive(bsdar, bsdar->mode);
342 		break;
343 
344 	case 'p': case 't': case 'x':
345 		ar_read_archive(bsdar, bsdar->mode);
346 		break;
347 	default:
348 		bsdar_usage();
349 		/* NOTREACHED */
350 	}
351 
352 	for (i = 0; i < bsdar->argc; i++)
353 		if (bsdar->argv[i] != NULL)
354 			bsdar_warnc(bsdar, 0, "%s: not found in archive",
355 			    bsdar->argv[i]);
356 
357 	exit(EXIT_SUCCESS);
358 }
359 
360 static void
361 set_mode(struct bsdar *bsdar, char opt)
362 {
363 
364 	if (bsdar->mode != '\0' && bsdar->mode != opt)
365 		bsdar_errc(bsdar, 0, "Can't specify both -%c and -%c",
366 		    opt, bsdar->mode);
367 	bsdar->mode = opt;
368 }
369 
370 static void
371 only_mode(struct bsdar *bsdar, const char *opt, const char *valid_modes)
372 {
373 
374 	if (strchr(valid_modes, bsdar->mode) == NULL)
375 		bsdar_errc(bsdar, 0, "Option %s is not permitted in mode -%c",
376 		    opt, bsdar->mode);
377 }
378 
379 #define	AR_USAGE_MESSAGE	"\
380 Usage: %s <command> [options] archive file...\n\
381   Manage archives.\n\n\
382   Where <command> is one of:\n\
383   -d            Delete members from the archive.\n\
384   -m            Move archive members within the archive.\n\
385   -p            Write the contents of members to standard output.\n\
386   -q            Append files to an archive.\n\
387   -r            Replace (add) files to an archive.\n\
388   -s            Add an archive symbol to an archive.\n\
389   -t            List files in an archive.\n\
390   -x            Extract members from an archive.\n\
391   -M            Execute MRI librarian commands.\n\
392   -V            Print a version identifier and exit.\n\n\
393   Options:\n\
394   -a MEMBER     Add members after the specified member.\n\
395   -b MEMBER | -i MEMBER\n\
396                 Add members before the specified member.\n\
397   -c            Do not print a message when creating a new archive.\n\
398   -f | -T       Only use the first fifteen characters of the member name.\n\
399   -j            (This option is accepted, but is ignored).\n\
400   -l            (This option is accepted, but is ignored).\n\
401   -o            Preserve modification times when extracting members.\n\
402   -u            Conditionally update or extract members.\n\
403   -v            Be verbose.\n\
404   -z            (This option is accepted, but is ignored).\n\
405   -C            Do not overwrite existing files in the file system.\n\
406   -D            Use fixed metadata, for consistent archive checksums.\n\
407   -F FORMAT | --flavor=FORMAT\n\
408                 Create archives with the specified format.\n\
409   -S            Do not generate an archive symbol table.\n\
410   -U            Use original metadata for archive members.\n"
411 
412 static void
413 bsdar_usage(void)
414 {
415 	(void) fprintf(stderr, AR_USAGE_MESSAGE, ELFTC_GETPROGNAME());
416 	exit(EXIT_FAILURE);
417 }
418 
419 #define	RANLIB_USAGE_MESSAGE	"\
420 Usage: %s [options] archive...\n\
421   Update or create archive symbol tables.\n\n\
422   Options:\n\
423   -t              (This option is accepted, but ignored).\n\
424   -D              Use fixed metadata, for consistent archive checksums.\n\
425   -U              Use original metadata, for unique archive checksums.\n\
426   -V              Print a version identifier and exit.\n"
427 
428 static void
429 ranlib_usage(void)
430 {
431 	(void)fprintf(stderr, RANLIB_USAGE_MESSAGE, ELFTC_GETPROGNAME());
432 	exit(EXIT_FAILURE);
433 }
434 
435 static void
436 bsdar_version(void)
437 {
438 	(void)printf("%s (%s, %s)\n", ELFTC_GETPROGNAME(), archive_version_string(),
439 	    elftc_version());
440 	exit(EXIT_SUCCESS);
441 }
442