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