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>. */
25 #define alloca __builtin_alloca
39 #include <sys/types.h>
43 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
60 #ifndef _POSIX_VERSION
61 struct passwd
*getpwnam ();
62 struct group
*getgrnam ();
63 struct group
*getgrgid ();
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) \
77 int _len = strlen ((src)); \
78 (dest) = (char *) alloca (_len + 1); \
83 #define isdigit(c) ((c) >= '0' && (c) <= '9')
87 /* Return nonzero if STR represents an unsigned decimal integer,
88 otherwise return 0. */
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. */
113 parse_user_spec (spec_arg
, uid
, gid
, username_arg
, groupname_arg
)
114 const char *spec_arg
;
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. */
124 char *g
, *u
, *separator
;
128 *username_arg
= *groupname_arg
= 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
)
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'
150 if (u
== NULL
&& g
== NULL
)
151 return "can not omit both user and group";
160 error_msg
= "invalid user";
164 use_login_group
= (separator
!= NULL
&& g
== NULL
);
166 error_msg
= "cannot get the login group of a numeric UID";
174 if (g
== NULL
&& separator
!= NULL
)
176 /* A separator was given, but a group was not specified,
177 so get the login group. */
179 grp
= getgrgid (pwd
->pw_gid
);
182 /* This is enough room to hold the unsigned decimal
183 representation of any 32-bit quantity and the trailing
186 sprintf (uint_buf
, "%u", (unsigned) (pwd
->pw_gid
));
187 V_STRDUP (groupname
, uint_buf
);
191 V_STRDUP (groupname
, grp
->gr_name
);
199 if (g
!= NULL
&& error_msg
== NULL
)
201 /* Explicit group. */
206 error_msg
= "invalid group";
212 endgrent (); /* Save a file descriptor. */
214 if (error_msg
== NULL
)
215 V_STRDUP (groupname
, g
);
218 if (error_msg
== NULL
)
222 *username_arg
= strdup (u
);
223 if (*username_arg
== NULL
)
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
;
247 #define NULL_CHECK(s) ((s) == NULL ? "(null)" : (s))
250 main (int argc
, char **argv
)
254 for (i
= 1; i
< argc
; i
++)
257 char *username
, *groupname
;
262 tmp
= strdup (argv
[i
]);
263 e
= parse_user_spec (tmp
, &uid
, &gid
, &username
, &groupname
);
265 printf ("%s: %u %u %s %s %s\n",
269 NULL_CHECK (username
),
270 NULL_CHECK (groupname
),