merge with 1.8b
[coreutils.git] / lib / userspec.c
blobfc93267f839ee543a9281b6b74291010534ddd02
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 #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). */
25 #include <config.h>
26 #else
27 #include "config.h"
28 #endif
29 #endif
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <pwd.h>
34 #include <grp.h>
36 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
37 #include <string.h>
38 #ifndef index
39 #define index strchr
40 #endif
41 #else
42 #include <strings.h>
43 #endif
45 #ifdef STDC_HEADERS
46 #include <stdlib.h>
47 #else
48 char *malloc ();
49 #endif
51 #ifdef HAVE_UNISTD_H
52 #include <unistd.h>
53 #endif
55 #ifndef _POSIX_VERSION
56 struct passwd *getpwnam ();
57 struct group *getgrnam ();
58 struct group *getgrgid ();
59 #endif
61 #ifdef _POSIX_SOURCE
62 #define endpwent()
63 #define endgrent()
64 #endif
66 #define isdigit(c) ((c) >= '0' && (c) <= '9')
68 char *strdup ();
69 static int isnumber ();
71 /* Extract from NAME, which has the form "[user][:.][group]",
72 a USERNAME, UID U, GROUPNAME, and GID G.
73 Either user or group, or both, must be present.
74 If the group is omitted but the ":" or "." separator is given,
75 use the given user's login group.
77 USERNAME and GROUPNAME will be in newly malloc'd memory.
78 Either one might be NULL instead, indicating that it was not
79 given and the corresponding numeric ID was left unchanged.
80 Might write NULs into NAME.
82 Return NULL if successful, a static error message string if not. */
84 char *
85 parse_user_spec (name, uid, gid, username, groupname)
86 char *name;
87 uid_t *uid;
88 gid_t *gid;
89 char **username, **groupname;
91 static char *tired = "virtual memory exhausted";
92 struct passwd *pwd;
93 struct group *grp;
94 char *cp;
95 int use_login_group = 0;
97 *username = *groupname = NULL;
99 /* Check whether a group is given. */
100 cp = index (name, ':');
101 if (cp == NULL)
102 cp = index (name, '.');
103 if (cp != NULL)
105 *cp++ = '\0';
106 if (*cp == '\0')
108 if (cp == name + 1)
109 /* Neither user nor group given, just "." or ":". */
110 return "can not omit both user and group";
111 else
112 /* "user.". */
113 use_login_group = 1;
115 else
117 /* Explicit group. */
118 *groupname = strdup (cp);
119 if (*groupname == NULL)
120 return tired;
121 grp = getgrnam (cp);
122 if (grp == NULL)
124 if (!isnumber (cp))
125 return "invalid group";
126 *gid = atoi (cp);
128 else
129 *gid = grp->gr_gid;
130 endgrent (); /* Save a file descriptor. */
134 /* Parse the user name, now that any group has been removed. */
136 if (name[0] == '\0')
137 /* No user name was given, just a group. */
138 return NULL;
140 *username = strdup (name);
141 if (*username == NULL)
142 return tired;
144 pwd = getpwnam (name);
145 if (pwd == NULL)
147 if (!isnumber (name))
148 return "invalid user";
149 if (use_login_group)
150 return "cannot get the login group of a numeric UID";
151 *uid = atoi (name);
153 else
155 *uid = pwd->pw_uid;
156 if (use_login_group)
158 *gid = pwd->pw_gid;
159 grp = getgrgid (pwd->pw_gid);
160 if (grp == NULL)
162 *groupname = malloc (15);
163 if (*groupname == NULL)
164 return tired;
165 sprintf (*groupname, "%u", pwd->pw_gid);
167 else
169 *groupname = strdup (grp->gr_name);
170 if (*groupname == NULL)
171 return tired;
173 endgrent ();
176 endpwent ();
177 return NULL;
180 /* Return nonzero if STR represents an unsigned decimal integer,
181 otherwise return 0. */
183 static int
184 isnumber (str)
185 char *str;
187 for (; *str; str++)
188 if (!isdigit (*str))
189 return 0;
190 return 1;