ldconfig(8): clean up manual page
[dragonfly.git] / contrib / libarchive / cpio / cmdline.c
blob7e59536957a40806ff0a78ac6e106e3eceaa1b11
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "cpio_platform.h"
29 __FBSDID("$FreeBSD: src/usr.bin/cpio/cmdline.c,v 1.5 2008/12/06 07:30:40 kientzle Exp $");
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #ifdef HAVE_GRP_H
35 #include <grp.h>
36 #endif
37 #ifdef HAVE_PWD_H
38 #include <pwd.h>
39 #endif
40 #include <stdio.h>
41 #ifdef HAVE_STDLIB_H
42 #include <stdlib.h>
43 #endif
44 #ifdef HAVE_STRING_H
45 #include <string.h>
46 #endif
48 #include "cpio.h"
49 #include "err.h"
52 * Short options for cpio. Please keep this sorted.
54 static const char *short_options = "0AaBC:cdE:F:f:H:hI:iJjLlmnO:opR:rtuVvW:yZz";
57 * Long options for cpio. Please keep this sorted.
59 static const struct option {
60 const char *name;
61 int required; /* 1 if this option requires an argument */
62 int equivalent; /* Equivalent short option. */
63 } cpio_longopts[] = {
64 { "b64encode", 0, OPTION_B64ENCODE },
65 { "create", 0, 'o' },
66 { "dot", 0, 'V' },
67 { "extract", 0, 'i' },
68 { "file", 1, 'F' },
69 { "format", 1, 'H' },
70 { "grzip", 0, OPTION_GRZIP },
71 { "help", 0, 'h' },
72 { "insecure", 0, OPTION_INSECURE },
73 { "link", 0, 'l' },
74 { "list", 0, 't' },
75 { "lrzip", 0, OPTION_LRZIP },
76 { "lz4", 0, OPTION_LZ4 },
77 { "lzma", 0, OPTION_LZMA },
78 { "lzop", 0, OPTION_LZOP },
79 { "make-directories", 0, 'd' },
80 { "no-preserve-owner", 0, OPTION_NO_PRESERVE_OWNER },
81 { "null", 0, '0' },
82 { "numeric-uid-gid", 0, 'n' },
83 { "owner", 1, 'R' },
84 { "passphrase", 1, OPTION_PASSPHRASE },
85 { "pass-through", 0, 'p' },
86 { "preserve-modification-time", 0, 'm' },
87 { "preserve-owner", 0, OPTION_PRESERVE_OWNER },
88 { "quiet", 0, OPTION_QUIET },
89 { "unconditional", 0, 'u' },
90 { "uuencode", 0, OPTION_UUENCODE },
91 { "verbose", 0, 'v' },
92 { "version", 0, OPTION_VERSION },
93 { "xz", 0, 'J' },
94 { NULL, 0, 0 }
98 * I used to try to select platform-provided getopt() or
99 * getopt_long(), but that caused a lot of headaches. In particular,
100 * I couldn't consistently use long options in the test harness
101 * because not all platforms have getopt_long(). That in turn led to
102 * overuse of the -W hack in the test harness, which made it rough to
103 * run the test harness against GNU cpio. (I periodically run the
104 * test harness here against GNU cpio as a sanity-check. Yes,
105 * I've found a couple of bugs in GNU cpio that way.)
108 cpio_getopt(struct cpio *cpio)
110 enum { state_start = 0, state_next_word, state_short, state_long };
111 static int state = state_start;
112 static char *opt_word;
114 const struct option *popt, *match = NULL, *match2 = NULL;
115 const char *p, *long_prefix = "--";
116 size_t optlength;
117 int opt = '?';
118 int required = 0;
120 cpio->argument = NULL;
122 /* First time through, initialize everything. */
123 if (state == state_start) {
124 /* Skip program name. */
125 ++cpio->argv;
126 --cpio->argc;
127 state = state_next_word;
131 * We're ready to look at the next word in argv.
133 if (state == state_next_word) {
134 /* No more arguments, so no more options. */
135 if (cpio->argv[0] == NULL)
136 return (-1);
137 /* Doesn't start with '-', so no more options. */
138 if (cpio->argv[0][0] != '-')
139 return (-1);
140 /* "--" marks end of options; consume it and return. */
141 if (strcmp(cpio->argv[0], "--") == 0) {
142 ++cpio->argv;
143 --cpio->argc;
144 return (-1);
146 /* Get next word for parsing. */
147 opt_word = *cpio->argv++;
148 --cpio->argc;
149 if (opt_word[1] == '-') {
150 /* Set up long option parser. */
151 state = state_long;
152 opt_word += 2; /* Skip leading '--' */
153 } else {
154 /* Set up short option parser. */
155 state = state_short;
156 ++opt_word; /* Skip leading '-' */
161 * We're parsing a group of POSIX-style single-character options.
163 if (state == state_short) {
164 /* Peel next option off of a group of short options. */
165 opt = *opt_word++;
166 if (opt == '\0') {
167 /* End of this group; recurse to get next option. */
168 state = state_next_word;
169 return cpio_getopt(cpio);
172 /* Does this option take an argument? */
173 p = strchr(short_options, opt);
174 if (p == NULL)
175 return ('?');
176 if (p[1] == ':')
177 required = 1;
179 /* If it takes an argument, parse that. */
180 if (required) {
181 /* If arg is run-in, opt_word already points to it. */
182 if (opt_word[0] == '\0') {
183 /* Otherwise, pick up the next word. */
184 opt_word = *cpio->argv;
185 if (opt_word == NULL) {
186 lafe_warnc(0,
187 "Option -%c requires an argument",
188 opt);
189 return ('?');
191 ++cpio->argv;
192 --cpio->argc;
194 if (opt == 'W') {
195 state = state_long;
196 long_prefix = "-W "; /* For clearer errors. */
197 } else {
198 state = state_next_word;
199 cpio->argument = opt_word;
204 /* We're reading a long option, including -W long=arg convention. */
205 if (state == state_long) {
206 /* After this long option, we'll be starting a new word. */
207 state = state_next_word;
209 /* Option name ends at '=' if there is one. */
210 p = strchr(opt_word, '=');
211 if (p != NULL) {
212 optlength = (size_t)(p - opt_word);
213 cpio->argument = (char *)(uintptr_t)(p + 1);
214 } else {
215 optlength = strlen(opt_word);
218 /* Search the table for an unambiguous match. */
219 for (popt = cpio_longopts; popt->name != NULL; popt++) {
220 /* Short-circuit if first chars don't match. */
221 if (popt->name[0] != opt_word[0])
222 continue;
223 /* If option is a prefix of name in table, record it.*/
224 if (strncmp(opt_word, popt->name, optlength) == 0) {
225 match2 = match; /* Record up to two matches. */
226 match = popt;
227 /* If it's an exact match, we're done. */
228 if (strlen(popt->name) == optlength) {
229 match2 = NULL; /* Forget the others. */
230 break;
235 /* Fail if there wasn't a unique match. */
236 if (match == NULL) {
237 lafe_warnc(0,
238 "Option %s%s is not supported",
239 long_prefix, opt_word);
240 return ('?');
242 if (match2 != NULL) {
243 lafe_warnc(0,
244 "Ambiguous option %s%s (matches --%s and --%s)",
245 long_prefix, opt_word, match->name, match2->name);
246 return ('?');
249 /* We've found a unique match; does it need an argument? */
250 if (match->required) {
251 /* Argument required: get next word if necessary. */
252 if (cpio->argument == NULL) {
253 cpio->argument = *cpio->argv;
254 if (cpio->argument == NULL) {
255 lafe_warnc(0,
256 "Option %s%s requires an argument",
257 long_prefix, match->name);
258 return ('?');
260 ++cpio->argv;
261 --cpio->argc;
263 } else {
264 /* Argument forbidden: fail if there is one. */
265 if (cpio->argument != NULL) {
266 lafe_warnc(0,
267 "Option %s%s does not allow an argument",
268 long_prefix, match->name);
269 return ('?');
272 return (match->equivalent);
275 return (opt);
280 * Parse the argument to the -R or --owner flag.
282 * The format is one of the following:
283 * <username|uid> - Override user but not group
284 * <username>: - Override both, group is user's default group
285 * <uid>: - Override user but not group
286 * <username|uid>:<groupname|gid> - Override both
287 * :<groupname|gid> - Override group but not user
289 * Where uid/gid are decimal representations and groupname/username
290 * are names to be looked up in system database. Note that we try
291 * to look up an argument as a name first, then try numeric parsing.
293 * A period can be used instead of the colon.
295 * Sets uid/gid return as appropriate, -1 indicates uid/gid not specified.
296 * TODO: If the spec uses uname/gname, then return those to the caller
297 * as well. If the spec provides uid/gid, just return names as NULL.
299 * Returns NULL if no error, otherwise returns error string for display.
302 const char *
303 owner_parse(const char *spec, int *uid, int *gid)
305 static char errbuff[128];
306 const char *u, *ue, *g;
308 *uid = -1;
309 *gid = -1;
311 if (spec[0] == '\0')
312 return ("Invalid empty user/group spec");
315 * Split spec into [user][:.][group]
316 * u -> first char of username, NULL if no username
317 * ue -> first char after username (colon, period, or \0)
318 * g -> first char of group name
320 if (*spec == ':' || *spec == '.') {
321 /* If spec starts with ':' or '.', then just group. */
322 ue = u = NULL;
323 g = spec + 1;
324 } else {
325 /* Otherwise, [user] or [user][:] or [user][:][group] */
326 ue = u = spec;
327 while (*ue != ':' && *ue != '.' && *ue != '\0')
328 ++ue;
329 g = ue;
330 if (*g != '\0') /* Skip : or . to find first char of group. */
331 ++g;
334 if (u != NULL) {
335 /* Look up user: ue is first char after end of user. */
336 char *user;
337 struct passwd *pwent;
339 user = (char *)malloc(ue - u + 1);
340 if (user == NULL)
341 return ("Couldn't allocate memory");
342 memcpy(user, u, ue - u);
343 user[ue - u] = '\0';
344 if ((pwent = getpwnam(user)) != NULL) {
345 *uid = pwent->pw_uid;
346 if (*ue != '\0')
347 *gid = pwent->pw_gid;
348 } else {
349 char *end;
350 errno = 0;
351 *uid = (int)strtoul(user, &end, 10);
352 if (errno || *end != '\0') {
353 snprintf(errbuff, sizeof(errbuff),
354 "Couldn't lookup user ``%s''", user);
355 errbuff[sizeof(errbuff) - 1] = '\0';
356 free(user);
357 return (errbuff);
360 free(user);
363 if (*g != '\0') {
364 struct group *grp;
365 if ((grp = getgrnam(g)) != NULL) {
366 *gid = grp->gr_gid;
367 } else {
368 char *end;
369 errno = 0;
370 *gid = (int)strtoul(g, &end, 10);
371 if (errno || *end != '\0') {
372 snprintf(errbuff, sizeof(errbuff),
373 "Couldn't lookup group ``%s''", g);
374 errbuff[sizeof(errbuff) - 1] = '\0';
375 return (errbuff);
379 return (NULL);