xref: /freebsd/usr.bin/ar/ar.c (revision 2a4a1db342263067035ce69a4017c645da63455d)
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Hugh Smith at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
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  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 static const char copyright[] =
39 "@(#) Copyright (c) 1990, 1993, 1994\n\
40 	The Regents of the University of California.  All rights reserved.\n";
41 #endif
42 
43 #if 0
44 #ifndef lint
45 static char sccsid[] = "@(#)ar.c	8.3 (Berkeley) 4/2/94";
46 #endif
47 #endif
48 
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD$");
51 
52 #include <sys/param.h>
53 
54 #include <ar.h>
55 #include <dirent.h>
56 #include <err.h>
57 #include <libgen.h>
58 #include <paths.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 #include <locale.h>
64 
65 #include "archive.h"
66 #include "extern.h"
67 
68 CHDR chdr;
69 u_int options;
70 char *archive, *posarg, *posname;
71 const char *envtmp;
72 static void badoptions(const char *);
73 static void usage(void);
74 
75 /*
76  * main --
77  *	main basically uses getopt to parse options and calls the appropriate
78  *	functions.  Some hacks that let us be backward compatible with 4.3 ar
79  *	option parsing and sanity checking.
80  */
81 int
82 main(argc, argv)
83 	int argc;
84 	char **argv;
85 {
86 	int c;
87 	char *p;
88 	int (*fcall)(char **) = NULL;
89 
90 	(void) setlocale(LC_TIME, "");;
91 
92 	if (argc < 3)
93 		usage();
94 
95 	/*
96 	 * Historic versions didn't require a '-' in front of the options.
97 	 * Fix it, if necessary.
98 	*/
99 	if (*argv[1] != '-') {
100 		if (!(p = malloc((u_int)(strlen(argv[1]) + 2))))
101 			err(1, NULL);
102 		*p = '-';
103 		(void)strcpy(p + 1, argv[1]);
104 		argv[1] = p;
105 	}
106 
107 	while ((c = getopt(argc, argv, "abcdilmopqrTtuvx")) != -1) {
108 		switch(c) {
109 		case 'a':
110 			options |= AR_A;
111 			break;
112 		case 'b':
113 		case 'i':
114 			options |= AR_B;
115 			break;
116 		case 'c':
117 			options |= AR_C;
118 			break;
119 		case 'd':
120 			options |= AR_D;
121 			fcall = delete;
122 			break;
123 		case 'l':		/* not documented, compatibility only */
124 			envtmp = ".";
125 			break;
126 		case 'm':
127 			options |= AR_M;
128 			fcall = move;
129 			break;
130 		case 'o':
131 			options |= AR_O;
132 			break;
133 		case 'p':
134 			options |= AR_P;
135 			fcall = print;
136 			break;
137 		case 'q':
138 			options |= AR_Q;
139 			fcall = append;
140 			break;
141 		case 'r':
142 			options |= AR_R;
143 			fcall = replace;
144 			break;
145 		case 'T':
146 			options |= AR_TR;
147 			break;
148 		case 't':
149 			options |= AR_T;
150 			fcall = contents;
151 			break;
152 		case 'u':
153 			options |= AR_U;
154 			break;
155 		case 'v':
156 			options |= AR_V;
157 			break;
158 		case 'x':
159 			options |= AR_X;
160 			fcall = extract;
161 			break;
162 		default:
163 			usage();
164 		}
165 	}
166 
167 	argv += optind;
168 	argc -= optind;
169 
170 	/* One of -dmpqrtx required. */
171 	if (!(options & (AR_D|AR_M|AR_P|AR_Q|AR_R|AR_T|AR_X))) {
172 		warnx("one of options -dmpqrtx is required");
173 		usage();
174 	}
175 	/* Only one of -a and -bi allowed. */
176 	if (options & AR_A && options & AR_B) {
177 		warnx("only one of -a and -[bi] options allowed");
178 		usage();
179 	}
180 	/* -ab require a position argument. */
181 	if (options & (AR_A|AR_B)) {
182 		if (!(posarg = *argv++)) {
183 			warnx("no position operand specified");
184 			usage();
185 		}
186 		posname = basename(posarg);
187 	}
188 	/* -d only valid with -Tv. */
189 	if (options & AR_D && options & ~(AR_D|AR_TR|AR_V))
190 		badoptions("-d");
191 	/* -m only valid with -abiTv. */
192 	if (options & AR_M && options & ~(AR_A|AR_B|AR_M|AR_TR|AR_V))
193 		badoptions("-m");
194 	/* -p only valid with -Tv. */
195 	if (options & AR_P && options & ~(AR_P|AR_TR|AR_V))
196 		badoptions("-p");
197 	/* -q only valid with -cTv. */
198 	if (options & AR_Q && options & ~(AR_C|AR_Q|AR_TR|AR_V))
199 		badoptions("-q");
200 	/* -r only valid with -abcuTv. */
201 	if (options & AR_R && options & ~(AR_A|AR_B|AR_C|AR_R|AR_U|AR_TR|AR_V))
202 		badoptions("-r");
203 	/* -t only valid with -Tv. */
204 	if (options & AR_T && options & ~(AR_T|AR_TR|AR_V))
205 		badoptions("-t");
206 	/* -x only valid with -ouTv. */
207 	if (options & AR_X && options & ~(AR_O|AR_U|AR_TR|AR_V|AR_X))
208 		badoptions("-x");
209 
210 	if (!(archive = *argv++)) {
211 		warnx("no archive specified");
212 		usage();
213 	}
214 
215 	/* -dmr require a list of archive elements. */
216 	if (options & (AR_D|AR_M|AR_R) && !*argv) {
217 		warnx("no archive members specified");
218 		usage();
219 	}
220 
221 	exit((*fcall)(argv));
222 }
223 
224 static void
225 badoptions(arg)
226 	const char *arg;
227 {
228 
229 	warnx("illegal option combination for %s", arg);
230 	usage();
231 }
232 
233 static void
234 usage()
235 {
236 
237 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
238 		"usage: ar -d [-Tv] archive file ...",
239 		"       ar -m [-Tv] archive file ...",
240 		"       ar -m [-abiTv] position archive file ...",
241 		"       ar -p [-Tv] archive [file ...]",
242 		"       ar -q [-cTv] archive file ...",
243 		"       ar -r [-cuTv] archive file ...",
244 		"       ar -r [-abciuTv] position archive file ...",
245 		"       ar -t [-Tv] archive [file ...]",
246 		"       ar -x [-ouTv] archive [file ...]");
247 	exit(1);
248 }
249