Pay attention to the timeout value passed down by the upper layer. This
[dragonfly.git] / contrib / cpio / userspec.c
blob67f1583335c513b3043a01a83e5484bdf6644f6c
1 /* userspec.c -- Parse a user and group string.
2 Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #ifdef __GNUC__
25 #define alloca __builtin_alloca
26 #else
27 #ifdef HAVE_ALLOCA_H
28 #include <alloca.h>
29 #else
30 #ifdef _AIX
31 #pragma alloca
32 #else
33 char *alloca ();
34 #endif
35 #endif
36 #endif
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <pwd.h>
41 #include <grp.h>
43 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
44 #include <string.h>
45 #ifndef index
46 #define index strchr
47 #endif
48 #else
49 #include <strings.h>
50 #endif
52 #ifdef STDC_HEADERS
53 #include <stdlib.h>
54 #endif
56 #ifdef HAVE_UNISTD_H
57 #include <unistd.h>
58 #endif
60 #ifndef _POSIX_VERSION
61 struct passwd *getpwnam ();
62 struct group *getgrnam ();
63 struct group *getgrgid ();
64 #endif
66 #ifdef _POSIX_SOURCE
67 #define endpwent()
68 #define endgrent()
69 #endif
71 /* Perform the equivalent of the statement `dest = strdup (src);',
72 but obtaining storage via alloca instead of from the heap. */
74 #define V_STRDUP(dest, src) \
75 do \
76 { \
77 int _len = strlen ((src)); \
78 (dest) = (char *) alloca (_len + 1); \
79 strcpy (dest, src); \
80 } \
81 while (0)
83 #define isdigit(c) ((c) >= '0' && (c) <= '9')
85 char *strdup ();
87 /* Return nonzero if STR represents an unsigned decimal integer,
88 otherwise return 0. */
90 static int
91 isnumber (str)
92 const char *str;
94 for (; *str; str++)
95 if (!isdigit (*str))
96 return 0;
97 return 1;
100 /* Extract from NAME, which has the form "[user][:.][group]",
101 a USERNAME, UID U, GROUPNAME, and GID G.
102 Either user or group, or both, must be present.
103 If the group is omitted but the ":" or "." separator is given,
104 use the given user's login group.
106 USERNAME and GROUPNAME will be in newly malloc'd memory.
107 Either one might be NULL instead, indicating that it was not
108 given and the corresponding numeric ID was left unchanged.
110 Return NULL if successful, a static error message string if not. */
112 const char *
113 parse_user_spec (spec_arg, uid, gid, username_arg, groupname_arg)
114 const char *spec_arg;
115 uid_t *uid;
116 gid_t *gid;
117 char **username_arg, **groupname_arg;
119 static const char *tired = "virtual memory exhausted";
120 const char *error_msg;
121 char *spec; /* A copy we can write on. */
122 struct passwd *pwd;
123 struct group *grp;
124 char *g, *u, *separator;
125 char *groupname;
127 error_msg = NULL;
128 *username_arg = *groupname_arg = NULL;
129 groupname = NULL;
131 V_STRDUP (spec, spec_arg);
133 /* Find the separator if there is one. */
134 separator = index (spec, ':');
135 if (separator == NULL)
136 separator = index (spec, '.');
138 /* Replace separator with a NUL. */
139 if (separator != NULL)
140 *separator = '\0';
142 /* Set U and G to non-zero length strings corresponding to user and
143 group specifiers or to NULL. */
144 u = (*spec == '\0' ? NULL : spec);
146 g = (separator == NULL || *(separator + 1) == '\0'
147 ? NULL
148 : separator + 1);
150 if (u == NULL && g == NULL)
151 return "can not omit both user and group";
153 if (u != NULL)
155 pwd = getpwnam (u);
156 if (pwd == NULL)
159 if (!isnumber (u))
160 error_msg = "invalid user";
161 else
163 int use_login_group;
164 use_login_group = (separator != NULL && g == NULL);
165 if (use_login_group)
166 error_msg = "cannot get the login group of a numeric UID";
167 else
168 *uid = atoi (u);
171 else
173 *uid = pwd->pw_uid;
174 if (g == NULL && separator != NULL)
176 /* A separator was given, but a group was not specified,
177 so get the login group. */
178 *gid = pwd->pw_gid;
179 grp = getgrgid (pwd->pw_gid);
180 if (grp == NULL)
182 /* This is enough room to hold the unsigned decimal
183 representation of any 32-bit quantity and the trailing
184 zero byte. */
185 char uint_buf[21];
186 sprintf (uint_buf, "%u", (unsigned) (pwd->pw_gid));
187 V_STRDUP (groupname, uint_buf);
189 else
191 V_STRDUP (groupname, grp->gr_name);
193 endgrent ();
196 endpwent ();
199 if (g != NULL && error_msg == NULL)
201 /* Explicit group. */
202 grp = getgrnam (g);
203 if (grp == NULL)
205 if (!isnumber (g))
206 error_msg = "invalid group";
207 else
208 *gid = atoi (g);
210 else
211 *gid = grp->gr_gid;
212 endgrent (); /* Save a file descriptor. */
214 if (error_msg == NULL)
215 V_STRDUP (groupname, g);
218 if (error_msg == NULL)
220 if (u != NULL)
222 *username_arg = strdup (u);
223 if (*username_arg == NULL)
224 error_msg = tired;
227 if (groupname != NULL && error_msg == NULL)
229 *groupname_arg = strdup (groupname);
230 if (*groupname_arg == NULL)
232 if (*username_arg != NULL)
234 free (*username_arg);
235 *username_arg = NULL;
237 error_msg = tired;
242 return error_msg;
245 #ifdef TEST
247 #define NULL_CHECK(s) ((s) == NULL ? "(null)" : (s))
250 main (int argc, char **argv)
252 int i;
254 for (i = 1; i < argc; i++)
256 const char *e;
257 char *username, *groupname;
258 uid_t uid;
259 gid_t gid;
260 char *tmp;
262 tmp = strdup (argv[i]);
263 e = parse_user_spec (tmp, &uid, &gid, &username, &groupname);
264 free (tmp);
265 printf ("%s: %u %u %s %s %s\n",
266 argv[i],
267 (unsigned int) uid,
268 (unsigned int) gid,
269 NULL_CHECK (username),
270 NULL_CHECK (groupname),
271 NULL_CHECK (e));
274 exit (0);
277 #endif