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