Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/userspec.c @ 6275:fd0ccce602e4
Sync from coreutils.
* .cppi-disable: Add regcomp.c, regex_internal.c, regex_internal.h,
stat-time.h.
* argmatch.h: Include verify.h
(ARGMATCH_VERIFY): Use verify rather than rolling our own.
(ARGMATCH_ASSERT): Remove; unused.
* canonicalize.c: Assume STDC_HEADERS.
* exclude.c: Include "strcase.h".
* regex_internal.h [!defined _LIBC]: Likewise.
* getusershell.c: Include stdio--.h rather than stdio.h
and stdio-safer.h.
(getusershell): Call fopen, not fopen_safer.
* save-cwd.c: Include fcntl--.h rather than fcntl.h.
Do not include unistd-safer.h.
(save_cwd): Don't call fd_safer; no longer needed
now that we include fcntl--.h.
* modules/argmatch (Depends-on): Add verify.
* modules/getloadavg (Depends-on): Depend on fcntl-safer, not
unistd-safer.
* modules/save-cwd (Depends-on): Likewise.
* backupfile.m4, calloc.m4, chown.m4, cloexec.m4, dup2.m4:
* fileblocks.m4, free.m4, ftruncate.m4, getcwd.m4, getpagesize.m4:
* getugroups.m4, group-member.m4, idcache.m4, link-follow.m4:
* mkstemp.m4, mktime.m4, mountlist.m4, nanosleep.m4, pathmax.m4:
* physmem.m4, posixver.m4, putenv.m4, safe-read.m4, same.m4:
* save-cwd.m4, stdio-safer.m4, unistd-safer.m4, unlinkdir.m4:
* userspec.m4, xgetcwd.m4, xreadlink.m4:
Don't bother checking for string.h, stdlib.h, unistd.h.
* fts.m4 (gl_FUNC_FTS_CORE): Don't require
AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK; that's now the lstat
module's job.
* jm-macros.m4 (gl_MACROS): Likewise.
* prereq.m4 (gl_PREREQ): Add gl_FUNC_LSTAT.
* backupfile.c: Use ARGMATCH_VERIFY, just in case.
* posixtm.c (posixtime) [lint]: Initialize *all* of tm0, not just
the .tm_year member, since otherwise gcc-4.0 would now warn about
tm_zone, tm_gmtoff, tm_isdst, tm_yday, tm_wday.
* quotearg.c (quotearg_n_options): Change code to be suboptimal, in
order to avoid an unsuppressible warning from gcc on 64-bit systems.
* lstat.m4 (gl_FUNC_LSTAT):
Use AC_LIBSOURCES to require lstat.c and lstat.h.
Remove obsolete comment.
* xreadlink.m4: Use AC_LIBSOURCES and AC_LIBOBJ.
* xstrtod.m4: Likewise.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Fri, 23 Sep 2005 04:15:13 +0000 |
parents | 96c32553b4c6 |
children | 6aeb4d6c28d9 |
rev | line source |
---|---|
5 | 1 /* userspec.c -- Parse a user and group string. |
5691
ec62790f0938
Factor int-properties macros into a single file, except for
Paul Eggert <eggert@cs.ucla.edu>
parents:
5322
diff
changeset
|
2 Copyright (C) 1989-1992, 1997-1998, 2000, 2002-2005 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, |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5691
diff
changeset
|
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
5 | 18 |
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */ | |
966 | 20 |
6259
96c32553b4c6
Use a consistent style for including <config.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5907
diff
changeset
|
21 #ifdef HAVE_CONFIG_H |
966 | 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 |
6275 | 42 #include <unistd.h> |
5 | 43 |
5691
ec62790f0938
Factor int-properties macros into a single file, except for
Paul Eggert <eggert@cs.ucla.edu>
parents:
5322
diff
changeset
|
44 #include "intprops.h" |
5159 | 45 #include "inttostr.h" |
4962
6bec4bfbd56e
Include strdup.h. From Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4844
diff
changeset
|
46 #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
|
47 #include "xalloc.h" |
2854 | 48 #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
|
49 |
3966
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3700
diff
changeset
|
50 #include "gettext.h" |
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3700
diff
changeset
|
51 #define _(msgid) gettext (msgid) |
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3700
diff
changeset
|
52 #define N_(msgid) msgid |
2273 | 53 |
499
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
54 #ifndef HAVE_ENDGRENT |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
55 # define endgrent() ((void) 0) |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
56 #endif |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
57 |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
58 #ifndef HAVE_ENDPWENT |
62aa0b766a11
[HAVE_ENDGRENT]: Define away endgrent.
Jim Meyering <jim@meyering.net>
parents:
475
diff
changeset
|
59 # define endpwent() ((void) 0) |
5 | 60 #endif |
61 | |
2854 | 62 #ifndef UID_T_MAX |
63 # define UID_T_MAX TYPE_MAXIMUM (uid_t) | |
64 #endif | |
65 | |
66 #ifndef GID_T_MAX | |
67 # define GID_T_MAX TYPE_MAXIMUM (gid_t) | |
68 #endif | |
69 | |
70 /* MAXUID may come from limits.h or sys/params.h. */ | |
71 #ifndef MAXUID | |
72 # define MAXUID UID_T_MAX | |
73 #endif | |
74 #ifndef MAXGID | |
75 # define MAXGID GID_T_MAX | |
76 #endif | |
77 | |
2271 | 78 /* ISDIGIT differs from isdigit, as follows: |
79 - Its arg may be any int or unsigned int; it need not be an unsigned char. | |
80 - It's guaranteed to evaluate its argument exactly once. | |
81 - It's typically faster. | |
3699 | 82 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to |
83 ISDIGIT_LOCALE unless it's important to use the locale's definition | |
84 of `digit' even when the host does not conform to POSIX. */ | |
5159 | 85 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9) |
86 | |
87 #ifdef __DJGPP__ | |
5 | 88 |
5159 | 89 /* Return true if STR represents an unsigned decimal integer. */ |
194 | 90 |
5159 | 91 static bool |
1557 | 92 is_number (const char *str) |
194 | 93 { |
5159 | 94 do |
95 { | |
96 if (!ISDIGIT (*str)) | |
97 return false; | |
98 } | |
99 while (*++str); | |
100 | |
101 return true; | |
194 | 102 } |
5159 | 103 #endif |
5 | 104 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
105 static char const * |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
106 parse_with_separator (char const *spec, char const *separator, |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
107 uid_t *uid, gid_t *gid, |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
108 char **username, char **groupname) |
5 | 109 { |
2273 | 110 static const char *E_invalid_user = N_("invalid user"); |
111 static const char *E_invalid_group = N_("invalid group"); | |
112 static const char *E_bad_spec = | |
113 N_("cannot get the login group of a numeric UID"); | |
114 | |
194 | 115 const char *error_msg; |
5 | 116 struct passwd *pwd; |
117 struct group *grp; | |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
118 char *u; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
119 char const *g; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
120 char *gname = NULL; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
121 uid_t unum = *uid; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
122 gid_t gnum = *gid; |
5 | 123 |
194 | 124 error_msg = NULL; |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
125 *username = *groupname = NULL; |
194 | 126 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
127 /* 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
|
128 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
|
129 allocated string. */ |
2273 | 130 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
131 u = NULL; |
4996 | 132 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
|
133 { |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
134 if (*spec) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
135 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
|
136 } |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
137 else |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
138 { |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
139 size_t ulen = separator - spec; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
140 if (ulen != 0) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
141 { |
5322 | 142 u = xmemdup (spec, ulen + 1); |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
143 u[ulen] = '\0'; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
144 } |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
145 } |
194 | 146 |
147 g = (separator == NULL || *(separator + 1) == '\0' | |
148 ? NULL | |
149 : separator + 1); | |
150 | |
1260
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
151 #ifdef __DJGPP__ |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
152 /* 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
|
153 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
|
154 if (u && !is_number (u)) |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
155 setenv ("USER", u, 1); |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
156 if (g && !is_number (g)) |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
157 setenv ("GROUP", g, 1); |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
158 #endif |
7aff74949325
(parse_user_spec) [__DJGPP__]: Make function know
Jim Meyering <jim@meyering.net>
parents:
966
diff
changeset
|
159 |
194 | 160 if (u != NULL) |
5 | 161 { |
194 | 162 pwd = getpwnam (u); |
163 if (pwd == NULL) | |
5 | 164 { |
5159 | 165 bool use_login_group = (separator != NULL && g == NULL); |
166 if (use_login_group) | |
167 error_msg = E_bad_spec; | |
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. */ | |
198 grp = getgrnam (g); | |
199 if (grp == NULL) | |
200 { | |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
201 unsigned long int tmp; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
202 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
|
203 gnum = tmp; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
204 else |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
205 error_msg = E_invalid_group; |
194 | 206 } |
207 else | |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
208 gnum = grp->gr_gid; |
194 | 209 endgrent (); /* Save a file descriptor. */ |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
210 gname = xstrdup (g); |
5 | 211 } |
194 | 212 |
230 | 213 if (error_msg == NULL) |
5 | 214 { |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
215 *uid = unum; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
216 *gid = gnum; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
217 *username = u; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
218 *groupname = gname; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
219 u = NULL; |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
220 } |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
221 else |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
222 free (gname); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
223 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
224 free (u); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
225 return _(error_msg); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
226 } |
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 /* Extract from SPEC, which has the form "[user][:.][group]", |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
229 a USERNAME, UID U, GROUPNAME, and GID G. |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
230 Either user or group, or both, must be present. |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
231 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
|
232 use the given user's login group. |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
233 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
|
234 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
|
235 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
|
236 try again interpreting the `.' as a separator. |
230 | 237 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
238 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
|
239 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
|
240 given and the corresponding numeric ID was left unchanged. |
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 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
|
243 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
244 char const * |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
245 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
|
246 char **username, char **groupname) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
247 { |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
248 char const *colon = strchr (spec, ':'); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
249 char const *error_msg = |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
250 parse_with_separator (spec, colon, uid, gid, username, groupname); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
251 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
252 if (!colon && error_msg) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
253 { |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
254 /* 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
|
255 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
|
256 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
|
257 separator. This is a compatible extension to POSIX, since |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
258 the POSIX-required behavior is always tried first. */ |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
259 |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
260 char const *dot = strchr (spec, '.'); |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
261 if (dot |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
262 && ! parse_with_separator (spec, dot, uid, gid, username, groupname)) |
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
263 error_msg = NULL; |
5 | 264 } |
194 | 265 |
5209
ef929daf6639
Import userspec from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5166
diff
changeset
|
266 return error_msg; |
5 | 267 } |
268 | |
232
0d9395790eb7
(main): Change #ifdef TESTING to TEST.
Jim Meyering <jim@meyering.net>
parents:
231
diff
changeset
|
269 #ifdef TEST |
194 | 270 |
966 | 271 # define NULL_CHECK(s) ((s) == NULL ? "(null)" : (s)) |
194 | 272 |
273 int | |
274 main (int argc, char **argv) | |
275 { | |
276 int i; | |
277 | |
278 for (i = 1; i < argc; i++) | |
279 { | |
280 const char *e; | |
281 char *username, *groupname; | |
282 uid_t uid; | |
283 gid_t gid; | |
284 char *tmp; | |
5 | 285 |
194 | 286 tmp = strdup (argv[i]); |
287 e = parse_user_spec (tmp, &uid, &gid, &username, &groupname); | |
288 free (tmp); | |
5159 | 289 printf ("%s: %lu %lu %s %s %s\n", |
194 | 290 argv[i], |
5159 | 291 (unsigned long int) uid, |
292 (unsigned long int) gid, | |
194 | 293 NULL_CHECK (username), |
294 NULL_CHECK (groupname), | |
295 NULL_CHECK (e)); | |
296 } | |
297 | |
298 exit (0); | |
5 | 299 } |
194 | 300 |
301 #endif |