Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/userspec.c @ 5322:65237260f160
xclone->userspec
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Tue, 05 Oct 2004 06:47:18 +0000 |
parents | ef929daf6639 |
children | ec62790f0938 |
rev | line source |
---|---|
5 | 1 /* userspec.c -- Parse a user and group string. |
4962
6bec4bfbd56e
Include strdup.h. From Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4844
diff
changeset
|
2 Copyright (C) 1989-1992, 1997-1998, 2000, 2002-2004 Free Software |
4844 | 3 Foundation, Inc. |
5 | 4 |
5 This program is free software; you can redistribute it and/or modify | |
6 it under the terms of the GNU General Public License as published by | |
7 the Free Software Foundation; either version 2, or (at your option) | |
8 any later version. | |
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 | |
650
b4ef1c1a0171
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
499
diff
changeset
|
16 along with this program; if not, write to the Free Software Foundation, |
b4ef1c1a0171
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
499
diff
changeset
|
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
5 | 18 |
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */ | |
966 | 20 |
21 #if HAVE_CONFIG_H | |
22 # include <config.h> | |
105 | 23 #endif |
24 | |
4962
6bec4bfbd56e
Include strdup.h. From Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4844
diff
changeset
|
25 /* Specification. */ |
6bec4bfbd56e
Include strdup.h. From Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4844
diff
changeset
|
26 #include "userspec.h" |
6bec4bfbd56e
Include strdup.h. From Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4844
diff
changeset
|
27 |
5159 | 28 #include <stdbool.h> |
5 | 29 #include <stdio.h> |
30 #include <sys/types.h> | |
31 #include <pwd.h> | |
32 #include <grp.h> | |
33 | |
2854 | 34 #if HAVE_SYS_PARAM_H |
35 # include <sys/param.h> | |
36 #endif | |
37 | |
4333 | 38 #include <limits.h> |
4691 | 39 #include <stdlib.h> |
40 #include <string.h> | |
5 | 41 |
966 | 42 #if HAVE_UNISTD_H |
432
a56993d69333
(parse_user_spec): Use strchr, not index.
Jim Meyering <jim@meyering.net>
parents:
316
diff
changeset
|
43 # include <unistd.h> |
5 | 44 #endif |
45 | |
5159 | 46 #include "inttostr.h" |
4962
6bec4bfbd56e
Include strdup.h. From Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4844
diff
changeset
|
47 #include "strdup.h" |
2810
aff553209434
(same_name): Invoke xalloc_die instead of printing our own message.
Jim Meyering <jim@meyering.net>
parents:
2284
diff
changeset
|
48 #include "xalloc.h" |
2854 | 49 #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
|
50 |
3966
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3700
diff
changeset
|
51 #include "gettext.h" |
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3700
diff
changeset
|
52 #define _(msgid) gettext (msgid) |
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3700
diff
changeset
|
53 #define N_(msgid) msgid |
2273 | 54 |
5 | 55 #ifndef _POSIX_VERSION |
56 struct passwd *getpwnam (); | |
57 struct group *getgrnam (); | |
58 struct group *getgrgid (); | |
59 #endif | |
60 | |
499
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
61 #ifndef HAVE_ENDGRENT |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
62 # define endgrent() ((void) 0) |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
63 #endif |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
64 |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
65 #ifndef HAVE_ENDPWENT |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
66 # define endpwent() ((void) 0) |
5 | 67 #endif |
68 | |
2854 | 69 /* The extra casts work around common compiler bugs. */ |
70 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) | |
71 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0. | |
72 It is necessary at least when t == time_t. */ | |
73 #define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \ | |
74 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0)) | |
75 #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t))) | |
76 | |
77 #ifndef UID_T_MAX | |
78 # define UID_T_MAX TYPE_MAXIMUM (uid_t) | |
79 #endif | |
80 | |
81 #ifndef GID_T_MAX | |
82 # define GID_T_MAX TYPE_MAXIMUM (gid_t) | |
83 #endif | |
84 | |
85 /* MAXUID may come from limits.h or sys/params.h. */ | |
86 #ifndef MAXUID | |
87 # define MAXUID UID_T_MAX | |
88 #endif | |
89 #ifndef MAXGID | |
90 # define MAXGID GID_T_MAX | |
91 #endif | |
92 | |
2271 | 93 /* ISDIGIT differs from isdigit, as follows: |
94 - Its arg may be any int or unsigned int; it need not be an unsigned char. | |
95 - It's guaranteed to evaluate its argument exactly once. | |
96 - It's typically faster. | |
3699 | 97 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to |
98 ISDIGIT_LOCALE unless it's important to use the locale's definition | |
99 of `digit' even when the host does not conform to POSIX. */ | |
5159 | 100 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9) |
101 | |
102 #ifdef __DJGPP__ | |
5 | 103 |
5159 | 104 /* Return true if STR represents an unsigned decimal integer. */ |
194 | 105 |
5159 | 106 static bool |
1557 | 107 is_number (const char *str) |
194 | 108 { |
5159 | 109 do |
110 { | |
111 if (!ISDIGIT (*str)) | |
112 return false; | |
113 } | |
114 while (*++str); | |
115 | |
116 return true; | |
194 | 117 } |
5159 | 118 #endif |
5 | 119 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
120 static char const * |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
121 parse_with_separator (char const *spec, char const *separator, |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
122 uid_t *uid, gid_t *gid, |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
123 char **username, char **groupname) |
5 | 124 { |
2273 | 125 static const char *E_invalid_user = N_("invalid user"); |
126 static const char *E_invalid_group = N_("invalid group"); | |
127 static const char *E_bad_spec = | |
128 N_("cannot get the login group of a numeric UID"); | |
129 | |
194 | 130 const char *error_msg; |
5 | 131 struct passwd *pwd; |
132 struct group *grp; | |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
133 char *u; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
134 char const *g; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
135 char *gname = NULL; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
136 uid_t unum = *uid; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
137 gid_t gnum = *gid; |
5 | 138 |
194 | 139 error_msg = NULL; |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
140 *username = *groupname = NULL; |
194 | 141 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
142 /* 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
|
143 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
|
144 allocated string. */ |
2273 | 145 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
146 u = NULL; |
4996 | 147 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
|
148 { |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
149 if (*spec) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
150 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
|
151 } |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
152 else |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
153 { |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
154 size_t ulen = separator - spec; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
155 if (ulen != 0) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
156 { |
5322 | 157 u = xmemdup (spec, ulen + 1); |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
158 u[ulen] = '\0'; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
159 } |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
160 } |
194 | 161 |
162 g = (separator == NULL || *(separator + 1) == '\0' | |
163 ? NULL | |
164 : separator + 1); | |
165 | |
1260
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
166 #ifdef __DJGPP__ |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
167 /* 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
|
168 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
|
169 if (u && !is_number (u)) |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
170 setenv ("USER", u, 1); |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
171 if (g && !is_number (g)) |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
172 setenv ("GROUP", g, 1); |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
173 #endif |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
174 |
194 | 175 if (u != NULL) |
5 | 176 { |
194 | 177 pwd = getpwnam (u); |
178 if (pwd == NULL) | |
5 | 179 { |
5159 | 180 bool use_login_group = (separator != NULL && g == NULL); |
181 if (use_login_group) | |
182 error_msg = E_bad_spec; | |
5 | 183 else |
194 | 184 { |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
185 unsigned long int tmp; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
186 if (xstrtoul (u, NULL, 10, &tmp, "") == LONGINT_OK |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
187 && tmp <= MAXUID) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
188 unum = tmp; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
189 else |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
190 error_msg = E_invalid_user; |
194 | 191 } |
5 | 192 } |
193 else | |
194 { | |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
195 unum = pwd->pw_uid; |
194 | 196 if (g == NULL && separator != NULL) |
5 | 197 { |
194 | 198 /* A separator was given, but a group was not specified, |
199 so get the login group. */ | |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
200 char buf[INT_BUFSIZE_BOUND (uintmax_t)]; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
201 gnum = pwd->pw_gid; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
202 grp = getgrgid (gnum); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
203 gname = xstrdup (grp ? grp->gr_name : umaxtostr (gnum, buf)); |
194 | 204 endgrent (); |
5 | 205 } |
194 | 206 } |
207 endpwent (); | |
5 | 208 } |
209 | |
195 | 210 if (g != NULL && error_msg == NULL) |
194 | 211 { |
212 /* Explicit group. */ | |
213 grp = getgrnam (g); | |
214 if (grp == NULL) | |
215 { | |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
216 unsigned long int tmp; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
217 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
|
218 gnum = tmp; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
219 else |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
220 error_msg = E_invalid_group; |
194 | 221 } |
222 else | |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
223 gnum = grp->gr_gid; |
194 | 224 endgrent (); /* Save a file descriptor. */ |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
225 gname = xstrdup (g); |
5 | 226 } |
194 | 227 |
230 | 228 if (error_msg == NULL) |
5 | 229 { |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
230 *uid = unum; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
231 *gid = gnum; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
232 *username = u; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
233 *groupname = gname; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
234 u = NULL; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
235 } |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
236 else |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
237 free (gname); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
238 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
239 free (u); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
240 return _(error_msg); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
241 } |
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 /* Extract from SPEC, which has the form "[user][:.][group]", |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
244 a USERNAME, UID U, GROUPNAME, and GID G. |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
245 Either user or group, or both, must be present. |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
246 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
|
247 use the given user's login group. |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
248 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
|
249 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
|
250 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
|
251 try again interpreting the `.' as a separator. |
230 | 252 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
253 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
|
254 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
|
255 given and the corresponding numeric ID was left unchanged. |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
256 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
257 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
|
258 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
259 char const * |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
260 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
|
261 char **username, char **groupname) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
262 { |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
263 char const *colon = strchr (spec, ':'); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
264 char const *error_msg = |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
265 parse_with_separator (spec, colon, uid, gid, username, groupname); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
266 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
267 if (!colon && error_msg) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
268 { |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
269 /* 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
|
270 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
|
271 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
|
272 separator. This is a compatible extension to POSIX, since |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
273 the POSIX-required behavior is always tried first. */ |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
274 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
275 char const *dot = strchr (spec, '.'); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
276 if (dot |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
277 && ! parse_with_separator (spec, dot, uid, gid, username, groupname)) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
278 error_msg = NULL; |
5 | 279 } |
194 | 280 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
281 return error_msg; |
5 | 282 } |
283 | |
232
0d9395790eb7
(main): Change #ifdef TESTING to TEST.
Jim Meyering <jim@meyering.net>
parents:
231
diff
changeset
|
284 #ifdef TEST |
194 | 285 |
966 | 286 # define NULL_CHECK(s) ((s) == NULL ? "(null)" : (s)) |
194 | 287 |
288 int | |
289 main (int argc, char **argv) | |
290 { | |
291 int i; | |
292 | |
293 for (i = 1; i < argc; i++) | |
294 { | |
295 const char *e; | |
296 char *username, *groupname; | |
297 uid_t uid; | |
298 gid_t gid; | |
299 char *tmp; | |
5 | 300 |
194 | 301 tmp = strdup (argv[i]); |
302 e = parse_user_spec (tmp, &uid, &gid, &username, &groupname); | |
303 free (tmp); | |
5159 | 304 printf ("%s: %lu %lu %s %s %s\n", |
194 | 305 argv[i], |
5159 | 306 (unsigned long int) uid, |
307 (unsigned long int) gid, | |
194 | 308 NULL_CHECK (username), |
309 NULL_CHECK (groupname), | |
310 NULL_CHECK (e)); | |
311 } | |
312 | |
313 exit (0); | |
5 | 314 } |
194 | 315 |
316 #endif |