Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.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  * $FreeBSD: src/usr.bin/ar/ar.c,v 1.8.2.1 2001/08/02 00:51:00 obrien Exp $
37  * $DragonFly: src/usr.bin/ar/Attic/ar.c,v 1.2 2003/06/17 04:29:25 dillon Exp $
38  *
39  * @(#) Copyright (c) 1990, 1993, 1994 The Regents of the University of California.  All rights reserved.
40  * @(#)ar.c     8.3 (Berkeley) 4/2/94
41  */
42
43 #include <sys/param.h>
44
45 #include <ar.h>
46 #include <dirent.h>
47 #include <err.h>
48 #include <libgen.h>
49 #include <paths.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <locale.h>
55
56 #include "archive.h"
57 #include "extern.h"
58
59 CHDR chdr;
60 u_int options;
61 char *archive, *envtmp, *posarg, *posname;
62 static void badoptions __P((char *));
63 static void usage __P((void));
64
65 /*
66  * main --
67  *      main basically uses getopt to parse options and calls the appropriate
68  *      functions.  Some hacks that let us be backward compatible with 4.3 ar
69  *      option parsing and sanity checking.
70  */
71 int
72 main(argc, argv)
73         int argc;
74         char **argv;
75 {
76         int c;
77         char *p;
78         int (*fcall) __P((char **)) = NULL;
79
80         (void) setlocale(LC_TIME, "");;
81
82         if (argc < 3)
83                 usage();
84
85         /*
86          * Historic versions didn't require a '-' in front of the options.
87          * Fix it, if necessary.
88         */
89         if (*argv[1] != '-') {
90                 if (!(p = malloc((u_int)(strlen(argv[1]) + 2))))
91                         err(1, NULL);
92                 *p = '-';
93                 (void)strcpy(p + 1, argv[1]);
94                 argv[1] = p;
95         }
96
97         while ((c = getopt(argc, argv, "abcdilmopqrTtuvx")) != -1) {
98                 switch(c) {
99                 case 'a':
100                         options |= AR_A;
101                         break;
102                 case 'b':
103                 case 'i':
104                         options |= AR_B;
105                         break;
106                 case 'c':
107                         options |= AR_C;
108                         break;
109                 case 'd':
110                         options |= AR_D;
111                         fcall = delete;
112                         break;
113                 case 'l':               /* not documented, compatibility only */
114                         envtmp = ".";
115                         break;
116                 case 'm':
117                         options |= AR_M;
118                         fcall = move;
119                         break;
120                 case 'o':
121                         options |= AR_O;
122                         break;
123                 case 'p':
124                         options |= AR_P;
125                         fcall = print;
126                         break;
127                 case 'q':
128                         options |= AR_Q;
129                         fcall = append;
130                         break;
131                 case 'r':
132                         options |= AR_R;
133                         fcall = replace;
134                         break;
135                 case 'T':
136                         options |= AR_TR;
137                         break;
138                 case 't':
139                         options |= AR_T;
140                         fcall = contents;
141                         break;
142                 case 'u':
143                         options |= AR_U;
144                         break;
145                 case 'v':
146                         options |= AR_V;
147                         break;
148                 case 'x':
149                         options |= AR_X;
150                         fcall = extract;
151                         break;
152                 default:
153                         usage();
154                 }
155         }
156
157         argv += optind;
158         argc -= optind;
159
160         /* One of -dmpqrtx required. */
161         if (!(options & (AR_D|AR_M|AR_P|AR_Q|AR_R|AR_T|AR_X))) {
162                 warnx("one of options -dmpqrtx is required");
163                 usage();
164         }
165         /* Only one of -a and -bi allowed. */
166         if (options & AR_A && options & AR_B) {
167                 warnx("only one of -a and -[bi] options allowed");
168                 usage();
169         }
170         /* -ab require a position argument. */
171         if (options & (AR_A|AR_B)) {
172                 if (!(posarg = *argv++)) {
173                         warnx("no position operand specified");
174                         usage();
175                 }
176                 posname = basename(posarg);
177         }
178         /* -d only valid with -Tv. */
179         if (options & AR_D && options & ~(AR_D|AR_TR|AR_V))
180                 badoptions("-d");
181         /* -m only valid with -abiTv. */
182         if (options & AR_M && options & ~(AR_A|AR_B|AR_M|AR_TR|AR_V))
183                 badoptions("-m");
184         /* -p only valid with -Tv. */
185         if (options & AR_P && options & ~(AR_P|AR_TR|AR_V))
186                 badoptions("-p");
187         /* -q only valid with -cTv. */
188         if (options & AR_Q && options & ~(AR_C|AR_Q|AR_TR|AR_V))
189                 badoptions("-q");
190         /* -r only valid with -abcuTv. */
191         if (options & AR_R && options & ~(AR_A|AR_B|AR_C|AR_R|AR_U|AR_TR|AR_V))
192                 badoptions("-r");
193         /* -t only valid with -Tv. */
194         if (options & AR_T && options & ~(AR_T|AR_TR|AR_V))
195                 badoptions("-t");
196         /* -x only valid with -ouTv. */
197         if (options & AR_X && options & ~(AR_O|AR_U|AR_TR|AR_V|AR_X))
198                 badoptions("-x");
199
200         if (!(archive = *argv++)) {
201                 warnx("no archive specified");
202                 usage();
203         }
204
205         /* -dmr require a list of archive elements. */
206         if (options & (AR_D|AR_M|AR_R) && !*argv) {
207                 warnx("no archive members specified");
208                 usage();
209         }
210
211         exit((*fcall)(argv));
212 }
213
214 static void
215 badoptions(arg)
216         char *arg;
217 {
218
219         warnx("illegal option combination for %s", arg);
220         usage();
221 }
222
223 static void
224 usage()
225 {
226
227         (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
228                 "usage: ar -d [-Tv] archive file ...",
229                 "       ar -m [-Tv] archive file ...",
230                 "       ar -m [-abiTv] position archive file ...",
231                 "       ar -p [-Tv] archive [file ...]",
232                 "       ar -q [-cTv] archive file ...",
233                 "       ar -r [-cuTv] archive file ...",
234                 "       ar -r [-abciuTv] position archive file ...",
235                 "       ar -t [-Tv] archive [file ...]",
236                 "       ar -x [-ouTv] archive [file ...]");
237         exit(1);
238 }