Mercurial > hg > octave-kai > gnulib-hg
annotate lib/userspec.c @ 9352:970d5cb5b003
Relax the license.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Wed, 17 Oct 2007 02:11:05 +0200 |
parents | bbbbbf4cd1c5 |
children | 5442b2430859 |
rev | line source |
---|---|
5 | 1 /* userspec.c -- Parse a user and group string. |
7909
23cd146c58fd
* lib/userspec.c (parse_with_separator): If a user or group string
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
2 Copyright (C) 1989-1992, 1997-1998, 2000, 2002-2007 Free Software |
4844 | 3 Foundation, Inc. |
5 | 4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7944
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
5 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7944
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7944
diff
changeset
|
8 (at your option) any later version. |
5 | 9 |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7944
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
5 | 17 |
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */ | |
966 | 19 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6969
diff
changeset
|
20 #include <config.h> |
105 | 21 |
4962
6bec4bfbd56e
Include strdup.h. From Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4844
diff
changeset
|
22 /* Specification. */ |
6bec4bfbd56e
Include strdup.h. From Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4844
diff
changeset
|
23 #include "userspec.h" |
6bec4bfbd56e
Include strdup.h. From Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4844
diff
changeset
|
24 |
5159 | 25 #include <stdbool.h> |
5 | 26 #include <stdio.h> |
27 #include <sys/types.h> | |
28 #include <pwd.h> | |
29 #include <grp.h> | |
30 | |
2854 | 31 #if HAVE_SYS_PARAM_H |
32 # include <sys/param.h> | |
33 #endif | |
34 | |
4333 | 35 #include <limits.h> |
4691 | 36 #include <stdlib.h> |
37 #include <string.h> | |
5 | 38 |
6275 | 39 #include <unistd.h> |
5 | 40 |
5691
ec62790f0938
Factor int-properties macros into a single file, except for
Paul Eggert <eggert@cs.ucla.edu>
parents:
5322
diff
changeset
|
41 #include "intprops.h" |
5159 | 42 #include "inttostr.h" |
2810
aff553209434
(same_name): Invoke xalloc_die instead of printing our own message.
Jim Meyering <jim@meyering.net>
parents:
2284
diff
changeset
|
43 #include "xalloc.h" |
2854 | 44 #include "xstrtol.h" |
2810
aff553209434
(same_name): Invoke xalloc_die instead of printing our own message.
Jim Meyering <jim@meyering.net>
parents:
2284
diff
changeset
|
45 |
3966
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3700
diff
changeset
|
46 #include "gettext.h" |
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3700
diff
changeset
|
47 #define _(msgid) gettext (msgid) |
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3700
diff
changeset
|
48 #define N_(msgid) msgid |
2273 | 49 |
499
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
50 #ifndef HAVE_ENDGRENT |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
51 # define endgrent() ((void) 0) |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
52 #endif |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
53 |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
54 #ifndef HAVE_ENDPWENT |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
55 # define endpwent() ((void) 0) |
5 | 56 #endif |
57 | |
2854 | 58 #ifndef UID_T_MAX |
59 # define UID_T_MAX TYPE_MAXIMUM (uid_t) | |
60 #endif | |
61 | |
62 #ifndef GID_T_MAX | |
63 # define GID_T_MAX TYPE_MAXIMUM (gid_t) | |
64 #endif | |
65 | |
66 /* MAXUID may come from limits.h or sys/params.h. */ | |
67 #ifndef MAXUID | |
68 # define MAXUID UID_T_MAX | |
69 #endif | |
70 #ifndef MAXGID | |
71 # define MAXGID GID_T_MAX | |
72 #endif | |
73 | |
2271 | 74 /* ISDIGIT differs from isdigit, as follows: |
6932
6aeb4d6c28d9
* lib/.cppi-disable: Add wcwidth.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
75 - Its arg may be any int or unsigned int; it need not be an unsigned char |
6aeb4d6c28d9
* lib/.cppi-disable: Add wcwidth.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
76 or EOF. |
2271 | 77 - It's typically faster. |
3699 | 78 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to |
6932
6aeb4d6c28d9
* lib/.cppi-disable: Add wcwidth.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
79 isdigit unless it's important to use the locale's definition |
3699 | 80 of `digit' even when the host does not conform to POSIX. */ |
5159 | 81 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9) |
82 | |
83 #ifdef __DJGPP__ | |
5 | 84 |
5159 | 85 /* Return true if STR represents an unsigned decimal integer. */ |
194 | 86 |
5159 | 87 static bool |
1557 | 88 is_number (const char *str) |
194 | 89 { |
5159 | 90 do |
91 { | |
92 if (!ISDIGIT (*str)) | |
93 return false; | |
94 } | |
95 while (*++str); | |
96 | |
97 return true; | |
194 | 98 } |
5159 | 99 #endif |
5 | 100 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
101 static char const * |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
102 parse_with_separator (char const *spec, char const *separator, |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
103 uid_t *uid, gid_t *gid, |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
104 char **username, char **groupname) |
5 | 105 { |
2273 | 106 static const char *E_invalid_user = N_("invalid user"); |
107 static const char *E_invalid_group = N_("invalid group"); | |
6969 | 108 static const char *E_bad_spec = N_("invalid spec"); |
2273 | 109 |
194 | 110 const char *error_msg; |
5 | 111 struct passwd *pwd; |
112 struct group *grp; | |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
113 char *u; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
114 char const *g; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
115 char *gname = NULL; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
116 uid_t unum = *uid; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
117 gid_t gnum = *gid; |
5 | 118 |
194 | 119 error_msg = NULL; |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
120 *username = *groupname = NULL; |
194 | 121 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
122 /* Set U and G to nonzero length strings corresponding to user and |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
123 group specifiers or to NULL. If U is not NULL, it is a newly |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
124 allocated string. */ |
2273 | 125 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
126 u = NULL; |
4996 | 127 if (separator == NULL) |
2284
ce1bca7b77d5
(parse_user_spec): If there is no `:' but there is a `.',
Jim Meyering <jim@meyering.net>
parents:
2273
diff
changeset
|
128 { |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
129 if (*spec) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
130 u = xstrdup (spec); |
2284
ce1bca7b77d5
(parse_user_spec): If there is no `:' but there is a `.',
Jim Meyering <jim@meyering.net>
parents:
2273
diff
changeset
|
131 } |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
132 else |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
133 { |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
134 size_t ulen = separator - spec; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
135 if (ulen != 0) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
136 { |
5322 | 137 u = xmemdup (spec, ulen + 1); |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
138 u[ulen] = '\0'; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
139 } |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
140 } |
194 | 141 |
142 g = (separator == NULL || *(separator + 1) == '\0' | |
143 ? NULL | |
144 : separator + 1); | |
145 | |
1260
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
146 #ifdef __DJGPP__ |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
147 /* Pretend that we are the user U whose group is G. This makes |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
148 pwd and grp functions ``know'' about the UID and GID of these. */ |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
149 if (u && !is_number (u)) |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
150 setenv ("USER", u, 1); |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
151 if (g && !is_number (g)) |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
152 setenv ("GROUP", g, 1); |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
153 #endif |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
154 |
194 | 155 if (u != NULL) |
5 | 156 { |
7909
23cd146c58fd
* lib/userspec.c (parse_with_separator): If a user or group string
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
157 /* If it starts with "+", skip the look-up. */ |
23cd146c58fd
* lib/userspec.c (parse_with_separator): If a user or group string
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
158 pwd = (*u == '+' ? NULL : getpwnam (u)); |
194 | 159 if (pwd == NULL) |
5 | 160 { |
5159 | 161 bool use_login_group = (separator != NULL && g == NULL); |
162 if (use_login_group) | |
6969 | 163 { |
164 /* If there is no group, | |
165 then there may not be a trailing ":", either. */ | |
166 error_msg = E_bad_spec; | |
167 } | |
5 | 168 else |
194 | 169 { |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
170 unsigned long int tmp; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
171 if (xstrtoul (u, NULL, 10, &tmp, "") == LONGINT_OK |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
172 && tmp <= MAXUID) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
173 unum = tmp; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
174 else |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
175 error_msg = E_invalid_user; |
194 | 176 } |
5 | 177 } |
178 else | |
179 { | |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
180 unum = pwd->pw_uid; |
194 | 181 if (g == NULL && separator != NULL) |
5 | 182 { |
194 | 183 /* A separator was given, but a group was not specified, |
184 so get the login group. */ | |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
185 char buf[INT_BUFSIZE_BOUND (uintmax_t)]; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
186 gnum = pwd->pw_gid; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
187 grp = getgrgid (gnum); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
188 gname = xstrdup (grp ? grp->gr_name : umaxtostr (gnum, buf)); |
194 | 189 endgrent (); |
5 | 190 } |
194 | 191 } |
192 endpwent (); | |
5 | 193 } |
194 | |
195 | 195 if (g != NULL && error_msg == NULL) |
194 | 196 { |
197 /* Explicit group. */ | |
7909
23cd146c58fd
* lib/userspec.c (parse_with_separator): If a user or group string
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
198 /* If it starts with "+", skip the look-up. */ |
23cd146c58fd
* lib/userspec.c (parse_with_separator): If a user or group string
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
199 grp = (*g == '+' ? NULL : getgrnam (g)); |
194 | 200 if (grp == NULL) |
201 { | |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
202 unsigned long int tmp; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
203 if (xstrtoul (g, NULL, 10, &tmp, "") == LONGINT_OK && tmp <= MAXGID) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
204 gnum = tmp; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
205 else |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
206 error_msg = E_invalid_group; |
194 | 207 } |
208 else | |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
209 gnum = grp->gr_gid; |
194 | 210 endgrent (); /* Save a file descriptor. */ |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
211 gname = xstrdup (g); |
5 | 212 } |
194 | 213 |
230 | 214 if (error_msg == NULL) |
5 | 215 { |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
216 *uid = unum; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
217 *gid = gnum; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
218 *username = u; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
219 *groupname = gname; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
220 u = NULL; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
221 } |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
222 else |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
223 free (gname); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
224 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
225 free (u); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
226 return _(error_msg); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
227 } |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
228 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
229 /* Extract from SPEC, which has the form "[user][:.][group]", |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
230 a USERNAME, UID U, GROUPNAME, and GID G. |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
231 Either user or group, or both, must be present. |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
232 If the group is omitted but the separator is given, |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
233 use the given user's login group. |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
234 If SPEC contains a `:', then use that as the separator, ignoring |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
235 any `.'s. If there is no `:', but there is a `.', then first look |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
236 up the entire SPEC as a login name. If that look-up fails, then |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
237 try again interpreting the `.' as a separator. |
230 | 238 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
239 USERNAME and GROUPNAME will be in newly malloc'd memory. |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
240 Either one might be NULL instead, indicating that it was not |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
241 given and the corresponding numeric ID was left unchanged. |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
242 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
243 Return NULL if successful, a static error message string if not. */ |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
244 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
245 char const * |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
246 parse_user_spec (char const *spec, uid_t *uid, gid_t *gid, |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
247 char **username, char **groupname) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
248 { |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
249 char const *colon = strchr (spec, ':'); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
250 char const *error_msg = |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
251 parse_with_separator (spec, colon, uid, gid, username, groupname); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
252 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
253 if (!colon && error_msg) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
254 { |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
255 /* If there's no colon but there is a dot, and if looking up the |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
256 whole spec failed (i.e., the spec is not a owner name that |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
257 includes a dot), then try again, but interpret the dot as a |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
258 separator. This is a compatible extension to POSIX, since |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
259 the POSIX-required behavior is always tried first. */ |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
260 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
261 char const *dot = strchr (spec, '.'); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
262 if (dot |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
263 && ! parse_with_separator (spec, dot, uid, gid, username, groupname)) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
264 error_msg = NULL; |
5 | 265 } |
194 | 266 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
267 return error_msg; |
5 | 268 } |
269 | |
232
0d9395790eb7
(main): Change #ifdef TESTING to TEST.
Jim Meyering <jim@meyering.net>
parents:
231
diff
changeset
|
270 #ifdef TEST |
194 | 271 |
966 | 272 # define NULL_CHECK(s) ((s) == NULL ? "(null)" : (s)) |
194 | 273 |
274 int | |
275 main (int argc, char **argv) | |
276 { | |
277 int i; | |
278 | |
279 for (i = 1; i < argc; i++) | |
280 { | |
281 const char *e; | |
282 char *username, *groupname; | |
283 uid_t uid; | |
284 gid_t gid; | |
285 char *tmp; | |
5 | 286 |
194 | 287 tmp = strdup (argv[i]); |
288 e = parse_user_spec (tmp, &uid, &gid, &username, &groupname); | |
289 free (tmp); | |
5159 | 290 printf ("%s: %lu %lu %s %s %s\n", |
194 | 291 argv[i], |
5159 | 292 (unsigned long int) uid, |
293 (unsigned long int) gid, | |
194 | 294 NULL_CHECK (username), |
295 NULL_CHECK (groupname), | |
296 NULL_CHECK (e)); | |
297 } | |
298 | |
299 exit (0); | |
5 | 300 } |
194 | 301 |
302 #endif |