2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.3 2008/06/21 02:20:20 kientzle Exp $");
34 #ifdef HAVE_GETOPT_LONG
44 #define required_argument 1
64 * Option parsing routines for bsdcpio.
69 static const char *cpio_opts = "0AaBC:F:O:cdE:f:H:hijLlmopR:rtuvW:yZz";
72 * On systems that lack getopt_long, long options can be specified
73 * using -W longopt and -W longopt=value, e.g. "-W version" is the
74 * same as "--version" and "-W format=ustar" is the same as "--format
75 * ustar". This does not rely the GNU getopt() "W;" extension, so
76 * should work correctly on any system with a POSIX-compliant
81 * If you add anything, be very careful to keep this list properly
82 * sorted, as the -W logic below relies on it.
84 static const struct option cpio_longopts[] = {
85 { "create", no_argument, NULL, 'o' },
86 { "extract", no_argument, NULL, 'i' },
87 { "file", required_argument, NULL, 'F' },
88 { "format", required_argument, NULL, 'H' },
89 { "help", no_argument, NULL, 'h' },
90 { "insecure", no_argument, NULL, OPTION_INSECURE },
91 { "link", no_argument, NULL, 'l' },
92 { "list", no_argument, NULL, 't' },
93 { "make-directories", no_argument, NULL, 'd' },
94 { "null", no_argument, NULL, '0' },
95 { "owner", required_argument, NULL, 'R' },
96 { "pass-through", no_argument, NULL, 'p' },
97 { "preserve-modification-time", no_argument, NULL, 'm' },
98 { "quiet", no_argument, NULL, OPTION_QUIET },
99 { "unconditional", no_argument, NULL, 'u' },
100 { "verbose", no_argument, NULL, 'v' },
101 { "version", no_argument, NULL, OPTION_VERSION },
106 * Parse command-line options using system-provided getopt() or getopt_long().
107 * If option is -W, then parse argument as a long option.
110 cpio_getopt(struct cpio *cpio)
113 const struct option *option, *option2;
116 size_t option_length;
120 #ifdef HAVE_GETOPT_LONG
121 opt = getopt_long(cpio->argc, cpio->argv, cpio_opts,
122 cpio_longopts, &option_index);
124 opt = getopt(cpio->argc, cpio->argv, cpio_opts);
127 /* Support long options through -W longopt=value */
130 q = strchr(optarg, '=');
132 option_length = (size_t)(q - p);
135 option_length = strlen(p);
138 option = cpio_longopts;
139 while (option->name != NULL &&
140 (strlen(option->name) < option_length ||
141 strncmp(p, option->name, option_length) != 0 )) {
145 if (option->name != NULL) {
149 /* If the first match was exact, we're done. */
150 if (strncmp(p, option->name, strlen(option->name)) == 0) {
151 while (option->name != NULL)
154 /* Check if there's another match. */
156 while (option->name != NULL &&
157 (strlen(option->name) < option_length ||
158 strncmp(p, option->name, option_length) != 0)) {
162 if (option->name != NULL)
164 "Ambiguous option %s "
165 "(matches both %s and %s)",
166 p, option2->name, option->name);
168 if (option2->has_arg == required_argument
171 "Option \"%s\" requires argument", p);
182 * Parse the argument to the -R or --owner flag.
184 * The format is one of the following:
185 * <user> - Override user but not group
186 * <user>: - Override both, group is user's default group
187 * <user>:<group> - Override both
188 * :<group> - Override group but not user
190 * A period can be used instead of the colon.
192 * Sets uid/gid as appropriate, -1 indicates uid/gid not specified.
196 owner_parse(const char *spec, int *uid, int *gid)
198 const char *u, *ue, *g;
204 * Split spec into [user][:.][group]
205 * u -> first char of username, NULL if no username
206 * ue -> first char after username (colon, period, or \0)
207 * g -> first char of group name
209 if (*spec == ':' || *spec == '.') {
210 /* If spec starts with ':' or '.', then just group. */
214 /* Otherwise, [user] or [user][:] or [user][:][group] */
216 while (*ue != ':' && *ue != '.' && *ue != '\0')
219 if (*g != '\0') /* Skip : or . to find first char of group. */
224 /* Look up user: ue is first char after end of user. */
226 struct passwd *pwent;
228 user = (char *)malloc(ue - u + 1);
230 cpio_warnc(errno, "Couldn't allocate memory");
233 memcpy(user, u, ue - u);
235 pwent = getpwnam(user);
237 cpio_warnc(errno, "Couldn't lookup user ``%s''", user);
241 *uid = pwent->pw_uid;
242 if (*ue != '\0' && *g == '\0')
243 *gid = pwent->pw_gid;
251 cpio_warnc(errno, "Couldn't look up group ``%s''", g);