]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/ar/ar.c
This commit was generated by cvs2svn to compensate for changes in r115367,
[FreeBSD/FreeBSD.git] / usr.bin / ar / ar.c
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(int argc, char **argv)
83 {
84         int c;
85         char *p;
86         int (*fcall)(char **) = NULL;
87
88         (void) setlocale(LC_TIME, "");;
89
90         if (argc < 3)
91                 usage();
92
93         /*
94          * Historic versions didn't require a '-' in front of the options.
95          * Fix it, if necessary.
96         */
97         if (*argv[1] != '-') {
98                 if (!(p = malloc((u_int)(strlen(argv[1]) + 2))))
99                         err(1, NULL);
100                 *p = '-';
101                 (void)strcpy(p + 1, argv[1]);
102                 argv[1] = p;
103         }
104
105         while ((c = getopt(argc, argv, "abcdilmopqrTtuvx")) != -1) {
106                 switch(c) {
107                 case 'a':
108                         options |= AR_A;
109                         break;
110                 case 'b':
111                 case 'i':
112                         options |= AR_B;
113                         break;
114                 case 'c':
115                         options |= AR_C;
116                         break;
117                 case 'd':
118                         options |= AR_D;
119                         fcall = delete;
120                         break;
121                 case 'l':               /* not documented, compatibility only */
122                         envtmp = ".";
123                         break;
124                 case 'm':
125                         options |= AR_M;
126                         fcall = move;
127                         break;
128                 case 'o':
129                         options |= AR_O;
130                         break;
131                 case 'p':
132                         options |= AR_P;
133                         fcall = print;
134                         break;
135                 case 'q':
136                         options |= AR_Q;
137                         fcall = append;
138                         break;
139                 case 'r':
140                         options |= AR_R;
141                         fcall = replace;
142                         break;
143                 case 'T':
144                         options |= AR_TR;
145                         break;
146                 case 't':
147                         options |= AR_T;
148                         fcall = contents;
149                         break;
150                 case 'u':
151                         options |= AR_U;
152                         break;
153                 case 'v':
154                         options |= AR_V;
155                         break;
156                 case 'x':
157                         options |= AR_X;
158                         fcall = extract;
159                         break;
160                 default:
161                         usage();
162                 }
163         }
164
165         argv += optind;
166         argc -= optind;
167
168         /* One of -dmpqrtx required. */
169         if (!(options & (AR_D|AR_M|AR_P|AR_Q|AR_R|AR_T|AR_X))) {
170                 warnx("one of options -dmpqrtx is required");
171                 usage();
172         }
173         /* Only one of -a and -bi allowed. */
174         if (options & AR_A && options & AR_B) {
175                 warnx("only one of -a and -[bi] options allowed");
176                 usage();
177         }
178         /* -ab require a position argument. */
179         if (options & (AR_A|AR_B)) {
180                 if (!(posarg = *argv++)) {
181                         warnx("no position operand specified");
182                         usage();
183                 }
184                 posname = basename(posarg);
185         }
186         /* -d only valid with -Tv. */
187         if (options & AR_D && options & ~(AR_D|AR_TR|AR_V))
188                 badoptions("-d");
189         /* -m only valid with -abiTv. */
190         if (options & AR_M && options & ~(AR_A|AR_B|AR_M|AR_TR|AR_V))
191                 badoptions("-m");
192         /* -p only valid with -Tv. */
193         if (options & AR_P && options & ~(AR_P|AR_TR|AR_V))
194                 badoptions("-p");
195         /* -q only valid with -cTv. */
196         if (options & AR_Q && options & ~(AR_C|AR_Q|AR_TR|AR_V))
197                 badoptions("-q");
198         /* -r only valid with -abcuTv. */
199         if (options & AR_R && options & ~(AR_A|AR_B|AR_C|AR_R|AR_U|AR_TR|AR_V))
200                 badoptions("-r");
201         /* -t only valid with -Tv. */
202         if (options & AR_T && options & ~(AR_T|AR_TR|AR_V))
203                 badoptions("-t");
204         /* -x only valid with -ouTv. */
205         if (options & AR_X && options & ~(AR_O|AR_U|AR_TR|AR_V|AR_X))
206                 badoptions("-x");
207
208         if (!(archive = *argv++)) {
209                 warnx("no archive specified");
210                 usage();
211         }
212
213         /* -dmr require a list of archive elements. */
214         if (options & (AR_D|AR_M|AR_R) && !*argv) {
215                 warnx("no archive members specified");
216                 usage();
217         }
218
219         exit((*fcall)(argv));
220 }
221
222 static void
223 badoptions(arg)
224         const char *arg;
225 {
226
227         warnx("illegal option combination for %s", arg);
228         usage();
229 }
230
231 static void
232 usage()
233 {
234
235         (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
236                 "usage: ar -d [-Tv] archive file ...",
237                 "       ar -m [-Tv] archive file ...",
238                 "       ar -m [-abiTv] position archive file ...",
239                 "       ar -p [-Tv] archive [file ...]",
240                 "       ar -q [-cTv] archive file ...",
241                 "       ar -r [-cuTv] archive file ...",
242                 "       ar -r [-abciuTv] position archive file ...",
243                 "       ar -t [-Tv] archive [file ...]",
244                 "       ar -x [-ouTv] archive [file ...]");
245         exit(1);
246 }