2 * Copyright (c) 2007 Kai Wang
3 * Copyright (c) 2007 Tim Kientzle
4 * Copyright (c) 2007 Joseph Koshy
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 * Copyright (c) 1990, 1993, 1994
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * Hugh Smith at The University of Guelph.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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
61 #include <sys/queue.h>
62 #include <sys/types.h>
75 ELFTC_VCSID("$Id: ar.c 3183 2015-04-10 16:18:42Z emaste $");
82 static struct option longopts[] =
84 {"flavor", required_argument, NULL, 'F'},
85 {"help", no_argument, NULL, OPTION_HELP},
86 {"version", no_argument, NULL, 'V'},
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);
98 main(int argc, char **argv)
100 struct bsdar *bsdar, bsdar_storage;
101 char *arcmd, *argv1_saved;
105 bsdar = &bsdar_storage;
106 memset(bsdar, 0, sizeof(*bsdar));
108 arcmd = argv1_saved = NULL;
109 bsdar->output = stdout;
111 if ((bsdar->progname = ELFTC_GETPROGNAME()) == NULL)
112 bsdar->progname = "ar";
114 if (elf_version(EV_CURRENT) == EV_NONE)
115 bsdar_errc(bsdar, 0, "ELF library initialization failed: %s",
119 * Act like ranlib if our name ends in "ranlib"; this
120 * accommodates names like "arm-freebsd7.1-ranlib",
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, "tDV", longopts,
133 bsdar->options |= AR_D;
150 bsdar->options |= AR_S;
151 for (;(bsdar->filename = *argv++) != NULL;)
152 ar_write_archive(bsdar, 's');
160 * Tack on a leading '-', for old-style usage.
162 if (*argv[1] != '-') {
163 argv1_saved = argv[1];
164 len = strlen(argv[1]) + 2;
165 if ((arcmd = malloc(len)) == NULL)
166 bsdar_errc(bsdar, errno, "malloc failed");
167 (void) snprintf(arcmd, len, "-%s", argv[1]);
172 while ((opt = getopt_long(argc, argv, "abCcdDfF:ijlMmopqrSsTtuVvxz",
173 longopts, NULL)) != -1) {
176 bsdar->options |= AR_A;
180 bsdar->options |= AR_B;
183 bsdar->options |= AR_CC;
186 bsdar->options |= AR_C;
189 set_mode(bsdar, opt);
192 bsdar->options |= AR_D;
195 if (!strcasecmp(optarg, "svr4") ||
196 !strcasecmp(optarg, "gnu"))
197 bsdar->options &= ~AR_BSD;
198 else if (!strcasecmp(optarg, "bsd"))
199 bsdar->options |= AR_BSD;
205 bsdar->options |= AR_TR;
211 /* ignored, for GNU ar comptibility */
214 set_mode(bsdar, opt);
217 set_mode(bsdar, opt);
220 bsdar->options |= AR_O;
223 set_mode(bsdar, opt);
226 set_mode(bsdar, opt);
229 set_mode(bsdar, opt);
232 bsdar->options |= AR_SS;
235 bsdar->options |= AR_S;
238 set_mode(bsdar, opt);
241 bsdar->options |= AR_U;
247 bsdar->options |= AR_V;
250 set_mode(bsdar, opt);
262 /* Restore argv[1] if we had modified it. */
264 argv[1] = argv1_saved;
266 arcmd = argv1_saved = NULL;
272 if (*argv == NULL && bsdar->mode != 'M')
275 if (bsdar->options & AR_A && bsdar->options & AR_B)
277 "only one of -a and -[bi] options allowed");
279 if (bsdar->options & AR_J && bsdar->options & AR_Z)
281 "only one of -j and -z options allowed");
283 if (bsdar->options & AR_S && bsdar->options & AR_SS)
285 "only one of -s and -S options allowed");
287 if (bsdar->options & (AR_A | AR_B)) {
290 "no position operand specified");
291 if ((bsdar->posarg = basename(*argv)) == NULL)
292 bsdar_errc(bsdar, errno,
298 if (bsdar->options & AR_A)
299 only_mode(bsdar, "-a", "mqr");
300 if (bsdar->options & AR_B)
301 only_mode(bsdar, "-b", "mqr");
302 if (bsdar->options & AR_C)
303 only_mode(bsdar, "-c", "qr");
304 if (bsdar->options & AR_CC)
305 only_mode(bsdar, "-C", "x");
306 if (bsdar->options & AR_D)
307 only_mode(bsdar, "-D", "qr");
308 if (bsdar->options & AR_O)
309 only_mode(bsdar, "-o", "x");
310 if (bsdar->options & AR_SS)
311 only_mode(bsdar, "-S", "mqr");
312 if (bsdar->options & AR_U)
313 only_mode(bsdar, "-u", "qrx");
315 if (bsdar->mode == 'M') {
316 ar_mode_script(bsdar);
320 if ((bsdar->filename = *argv) == NULL)
323 bsdar->argc = --argc;
324 bsdar->argv = ++argv;
326 if ((!bsdar->mode || strchr("ptx", bsdar->mode)) &&
327 bsdar->options & AR_S) {
328 ar_write_archive(bsdar, 's');
333 switch(bsdar->mode) {
334 case 'd': case 'm': case 'q': case 'r':
335 ar_write_archive(bsdar, bsdar->mode);
338 case 'p': case 't': case 'x':
339 ar_read_archive(bsdar, bsdar->mode);
346 for (i = 0; i < bsdar->argc; i++)
347 if (bsdar->argv[i] != NULL)
348 bsdar_warnc(bsdar, 0, "%s: not found in archive",
355 set_mode(struct bsdar *bsdar, char opt)
358 if (bsdar->mode != '\0' && bsdar->mode != opt)
359 bsdar_errc(bsdar, 0, "Can't specify both -%c and -%c",
365 only_mode(struct bsdar *bsdar, const char *opt, const char *valid_modes)
368 if (strchr(valid_modes, bsdar->mode) == NULL)
369 bsdar_errc(bsdar, 0, "Option %s is not permitted in mode -%c",
373 #define AR_USAGE_MESSAGE "\
374 Usage: %s <command> [options] archive file...\n\
375 Manage archives.\n\n\
376 Where <command> is one of:\n\
377 -d Delete members from the archive.\n\
378 -m Move archive members within the archive.\n\
379 -p Write the contents of members to standard output.\n\
380 -q Append files to an archive.\n\
381 -r Replace (add) files to an archive.\n\
382 -s Add an archive symbol to an archive.\n\
383 -t List files in an archive.\n\
384 -x Extract members from an archive.\n\
385 -M Execute MRI librarian commands.\n\
386 -V Print a version identifier and exit.\n\n\
388 -a MEMBER Add members after the specified member.\n\
389 -b MEMBER | -i MEMBER\n\
390 Add members before the specified member.\n\
391 -c Do not print a message when creating a new archive.\n\
392 -f | -T Only use the first fifteen characters of the member name.\n\
393 -j (This option is accepted, but is ignored).\n\
394 -l (This option is accepted, but is ignored).\n\
395 -o Preserve modification times when extracting members.\n\
396 -u Conditionally update or extract members.\n\
398 -z (This option is accepted, but is ignored).\n\
399 -C Do not overwrite existing files in the file system.\n\
400 -D Use fixed metadata, for consistent archive checksums.\n\
401 -F FORMAT | --flavor=FORMAT\n\
402 Create archives with the specified format.\n\
403 -S Do not generate an archive symbol table.\n"
408 (void) fprintf(stderr, AR_USAGE_MESSAGE, ELFTC_GETPROGNAME());
412 #define RANLIB_USAGE_MESSAGE "\
413 Usage: %s [options] archive...\n\
414 Update or create archive symbol tables.\n\n\
416 -t (This option is accepted, but ignored).\n\
417 -D Use fixed metadata, for consistent archive checksums.\n\
418 -V Print a version identifier and exit.\n"
423 (void)fprintf(stderr, RANLIB_USAGE_MESSAGE, ELFTC_GETPROGNAME());
430 (void)printf("%s (%s, %s)\n", ELFTC_GETPROGNAME(), archive_version_string(),