Add ip6 and icmp6 displays to systat(1).
[dragonfly/vkernel-mp.git] / usr.bin / ar / ar.c
blob8e443e452e68c708e1ee5e0ec96dc31f84285c71
1 /*-
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Hugh Smith at The University of Guelph.
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.
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.
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/ar.c,v 1.6 2006/08/03 18:40:48 swildner Exp $
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
43 #include <sys/param.h>
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>
56 #include "archive.h"
57 #include "extern.h"
59 CHDR chdr;
60 u_int options;
61 char *archive, *posarg, *posname;
62 char const *envtmp;
63 static void badoptions(char const*);
64 static void usage(void);
67 * main --
68 * main basically uses getopt to parse options and calls the appropriate
69 * functions. Some hacks that let us be backward compatible with 4.3 ar
70 * option parsing and sanity checking.
72 int
73 main(int argc, char **argv)
75 int c;
76 char *p;
77 int (*fcall)(char **) = NULL;
79 setlocale(LC_TIME, "");
81 if (argc < 3)
82 usage();
85 * Historic versions didn't require a '-' in front of the options.
86 * Fix it, if necessary.
88 if (*argv[1] != '-') {
89 if (!(p = malloc((u_int)(strlen(argv[1]) + 2))))
90 err(1, NULL);
91 *p = '-';
92 strcpy(p + 1, argv[1]);
93 argv[1] = p;
96 while ((c = getopt(argc, argv, "abcdilmopqrTtuvx")) != -1) {
97 switch(c) {
98 case 'a':
99 options |= AR_A;
100 break;
101 case 'b':
102 case 'i':
103 options |= AR_B;
104 break;
105 case 'c':
106 options |= AR_C;
107 break;
108 case 'd':
109 options |= AR_D;
110 fcall = delete;
111 break;
112 case 'l': /* not documented, compatibility only */
113 envtmp = ".";
114 break;
115 case 'm':
116 options |= AR_M;
117 fcall = move;
118 break;
119 case 'o':
120 options |= AR_O;
121 break;
122 case 'p':
123 options |= AR_P;
124 fcall = print;
125 break;
126 case 'q':
127 options |= AR_Q;
128 fcall = append;
129 break;
130 case 'r':
131 options |= AR_R;
132 fcall = replace;
133 break;
134 case 'T':
135 options |= AR_TR;
136 break;
137 case 't':
138 options |= AR_T;
139 fcall = contents;
140 break;
141 case 'u':
142 options |= AR_U;
143 break;
144 case 'v':
145 options |= AR_V;
146 break;
147 case 'x':
148 options |= AR_X;
149 fcall = extract;
150 break;
151 default:
152 usage();
156 argv += optind;
157 argc -= optind;
159 /* One of -dmpqrtx required. */
160 if (!(options & (AR_D|AR_M|AR_P|AR_Q|AR_R|AR_T|AR_X))) {
161 warnx("one of options -dmpqrtx is required");
162 usage();
164 /* Only one of -a and -bi allowed. */
165 if (options & AR_A && options & AR_B) {
166 warnx("only one of -a and -[bi] options allowed");
167 usage();
169 /* -ab require a position argument. */
170 if (options & (AR_A|AR_B)) {
171 if (!(posarg = *argv++)) {
172 warnx("no position operand specified");
173 usage();
175 posname = basename(posarg);
177 /* -d only valid with -Tv. */
178 if (options & AR_D && options & ~(AR_D|AR_TR|AR_V))
179 badoptions("-d");
180 /* -m only valid with -abiTv. */
181 if (options & AR_M && options & ~(AR_A|AR_B|AR_M|AR_TR|AR_V))
182 badoptions("-m");
183 /* -p only valid with -Tv. */
184 if (options & AR_P && options & ~(AR_P|AR_TR|AR_V))
185 badoptions("-p");
186 /* -q only valid with -cTv. */
187 if (options & AR_Q && options & ~(AR_C|AR_Q|AR_TR|AR_V))
188 badoptions("-q");
189 /* -r only valid with -abcuTv. */
190 if (options & AR_R && options & ~(AR_A|AR_B|AR_C|AR_R|AR_U|AR_TR|AR_V))
191 badoptions("-r");
192 /* -t only valid with -Tv. */
193 if (options & AR_T && options & ~(AR_T|AR_TR|AR_V))
194 badoptions("-t");
195 /* -x only valid with -ouTv. */
196 if (options & AR_X && options & ~(AR_O|AR_U|AR_TR|AR_V|AR_X))
197 badoptions("-x");
199 if (!(archive = *argv++)) {
200 warnx("no archive specified");
201 usage();
204 /* -dmr require a list of archive elements. */
205 if (options & (AR_D|AR_M|AR_R) && !*argv) {
206 warnx("no archive members specified");
207 usage();
210 exit((*fcall)(argv));
213 static void
214 badoptions(char const *arg)
217 warnx("illegal option combination for %s", arg);
218 usage();
221 static void
222 usage(void)
225 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
226 "usage: ar -d [-Tv] archive file ...",
227 " ar -m [-Tv] archive file ...",
228 " ar -m [-abiTv] position archive file ...",
229 " ar -p [-Tv] archive [file ...]",
230 " ar -q [-cTv] archive file ...",
231 " ar -r [-cuTv] archive file ...",
232 " ar -r [-abciuTv] position archive file ...",
233 " ar -t [-Tv] archive [file ...]",
234 " ar -x [-ouTv] archive [file ...]");
235 exit(1);