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)
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>. */
21 #if defined (CONFIG_BROKETS)
22 /* We use <config.h> instead of "config.h" so that a compilation
23 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
24 (which it would do because it found this file in $srcdir). */
32 #include <sys/types.h>
36 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
55 #ifndef _POSIX_VERSION
56 struct passwd *getpwnam ();
57 struct group *getgrnam ();
58 struct group *getgrgid ();
66 #define isdigit(c) ((c) >= '0' && (c) <= '9')
70 /* Return nonzero if STR represents an unsigned decimal integer,
71 otherwise return 0. */
83 /* Extract from NAME, which has the form "[user][:.][group]",
84 a USERNAME, UID U, GROUPNAME, and GID G.
85 Either user or group, or both, must be present.
86 If the group is omitted but the ":" or "." separator is given,
87 use the given user's login group.
89 USERNAME and GROUPNAME will be in newly malloc'd memory.
90 Either one might be NULL instead, indicating that it was not
91 given and the corresponding numeric ID was left unchanged.
93 Return NULL if successful, a static error message string if not. */
96 parse_user_spec (spec_arg, uid, gid, username, groupname)
100 char **username, **groupname;
102 static const char *tired = "virtual memory exhausted";
103 const char *error_msg;
104 char *spec; /* A copy we can write on. */
108 char *g, *u, *separator;
111 *username = *groupname = NULL;
113 spec_len = strlen (spec_arg);
114 spec = (char *) alloca (strlen (spec_arg) + 1);
115 strcpy (spec, spec_arg);
117 /* Find the separator if there is one. */
118 separator = index (spec, ':');
119 if (separator == NULL)
120 separator = index (spec, '.');
122 /* Replace separator with a NUL. */
123 if (separator != NULL)
126 /* Set U and G to non-zero length strings corresponding to user and
127 group specifiers or to NULL. */
128 u = (*spec == '\0' ? NULL : spec);
130 g = (separator == NULL || *(separator + 1) == '\0'
134 if (u == NULL && g == NULL)
135 return "can not omit both user and group";
144 error_msg = "invalid user";
148 use_login_group = (separator != NULL && g == NULL);
150 error_msg = "cannot get the login group of a numeric UID";
158 if (g == NULL && separator != NULL)
160 /* A separator was given, but a group was not specified,
161 so get the login group. */
163 grp = getgrgid (pwd->pw_gid);
166 /* This is enough room to hold the unsigned decimal
167 representation of any 32-bit quantity and the trailing
170 sprintf (uint_buf, "%u", (unsigned) (pwd->pw_gid));
171 *groupname = strdup (uint_buf);
175 *groupname = strdup (grp->gr_name);
177 if (*groupname == NULL)
184 if (error_msg == NULL)
186 *username = strdup (u);
187 if (*username == NULL)
194 /* Explicit group. */
199 error_msg = "invalid group";
205 endgrent (); /* Save a file descriptor. */
207 if (error_msg == NULL)
209 *groupname = strdup (g);
210 if (*groupname == NULL)
217 if (*groupname != NULL)
222 if (*username != NULL)
234 #define NULL_CHECK(s) ((s) == NULL ? "(null)" : (s))
237 main (int argc, char **argv)
241 for (i = 1; i < argc; i++)
244 char *username, *groupname;
249 tmp = strdup (argv[i]);
250 e = parse_user_spec (tmp, &uid, &gid, &username, &groupname);
252 printf ("%s: %u %u %s %s %s\n",
256 NULL_CHECK (username),
257 NULL_CHECK (groupname),