Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/idcache.c @ 5907:c47674a83a78
Sync from coreutils.
Use "file name" when talking about file names, instead of "filename"
or "path", as per the GNU coding standards.
* MODULES.html.sh: mkdir-p renamed from makepath.
filenamecat renamed from path-concat.
* modules/filenamecat: Renamed from modules/path-concat.
(Files): filenamecat.h and filenamecat.c renamed from
path-concat.h and path-concat.c.
(configure.ac): gl_FILE_NAME_CONCAT, not gl_PATH_CONCAT.
(Include): filenamecat.h, not path-concat.h.
* modules/mkdir-p: Renamed from modules/makepath.
(Files): mkdir-p.h and mkdir-p.c renamed from makepath.h and makepath.c.
(configure.ac): gl_MKDIR_PARENTS, not gl_MAKEPATH.
(Include): mkdir-p.h, not makepath.h.
* lib/mkdir-p.c: Renamed from makepath.c.
(make_dir_parents): Renamed from make_path. All callers changed.
* lib/mkdir-p.h: Likewise. All includers changed.
* lib/filenamecat.c: Renamed from path-concat.c.
(file_name_concat): Renamed from path_concat. All callers changed.
[TEST_FILE_NAME_CONCAT]: Renamed from TEST_PATH_CONCAT.
* lib/filenamecat.h: Likewise. All includers changed.
* lib/acl.c: Don't use "path" or "filename" to mean "file name"
in comments or local variable names.
* lib/basename.c: Likewise.
* lib/canonicalize.c, canonicalize.h: Likewise.
* lib/dirname.c, dirname.h: Likewise.
* lib/euidaccess.c: Likewise.
* lib/exclude.c: Likewise
* lib/fnmatch_.h, fnmatch_loop.c: Likewise.
* lib/fsusage.c, fsuage.h: Likewise.
* lib/fts.c, fts_.h: Likewise.
* lib/getcwd.c: Likewise.
* lib/getloadavg.c: Likewise.
* lib/mkstemp.c: Likewise.
* lib/mountlist.c, mountlist.h: Likewise.
* lib/openat.c, openat.h: Likewise.
* lib/readlink-stub.c: Likewise.
* lib/readutmp.c, readutmp.h: Likewise.
* lib/rename.c: Likewise.
* lib/rmdir.c: Likewise.
* lib/same.c: Likewise.
* lib/savedir.c: Likewise.
* lib/stripslash.c: Likewise.
* lib/tempname.c: Likewise.
* lib/xreadlink.c: Likewise.
* lib/exclude.c (excluded_file_name): Renamed from excluded_filename.
All uses changed.
* lib/exclude.h: Likewise.
* m4/mkdir-p.m4: Renamed from makepath.m4.
(gl_MKDIR_PARENTS): Renamed from gl_MAKEPATH. All uses changed.
Rename files from makepath.c to mkdir-p.c, and from
makepath.h to mkdir-p.h.
* m4/filenamecat.m4: Renamed from path-concat.m4.
(gl_FILE_NAME_CONCAT): Renamed from gl_PATH_CONCAT. All uses changed.
Rename files from path-concat.c to filenamecat.c,
and from path-concat.h to filenamecat.h.
* m4/getcwd-path-max.m4: Don't use "path" or "filename" to mean
"file name" in local variables or comments.
* rename.m4: Likewise.
* lib/euidaccess.c (getuid, getgid, getuid, getegid)
[!defined _POSIX_VERSION]: Remove decls; not needed these days.
* lib/idcache.c (getpwuid, getpwnam, getgrgid, getgrnam)
[!defined _POSIX_VERSION]: Remove decls; not needed these days.
* lib/pathmax.h: Include <limits.h> unconditionally, since other
files have been getting away with it for years (MORE/BSD 4.3
is extinct now).
* lib/userspec.c (getpwnam, getgrnam, getgrgid)
[!defined _POSIX_VERSION]: Remove decls; not needed these days.
* lib/pathmax.h (_POSIX_PATH_MAX) [!defined _POSIX_PATH_MAX]:
Define to 256, not 255, as per modern POSIX.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Thu, 02 Jun 2005 20:41:04 +0000 |
parents | a48fb0e98c8c |
children | 9300a4e513e9 |
rev | line source |
---|---|
5 | 1 /* idcache.c -- map user and group IDs, cached for speed |
5907 | 2 |
3 Copyright (C) 1985, 1988, 1989, 1990, 1997, 1998, 2003, 2005 Free | |
4 Software Foundation, Inc. | |
5 | 5 |
6 This program is free software; you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation; either version 2, or (at your option) | |
9 any later version. | |
10 | |
11 This program is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 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:
5
diff
changeset
|
17 along with this program; if not, write to the Free Software Foundation, |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
650
b4ef1c1a0171
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
5
diff
changeset
|
19 |
967
029c8dd12d60
(getuser): Return NULL (rather than stringified uid) upon lookup failure.
Jim Meyering <jim@meyering.net>
parents:
650
diff
changeset
|
20 #if HAVE_CONFIG_H |
029c8dd12d60
(getuser): Return NULL (rather than stringified uid) upon lookup failure.
Jim Meyering <jim@meyering.net>
parents:
650
diff
changeset
|
21 # include <config.h> |
650
b4ef1c1a0171
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
5
diff
changeset
|
22 #endif |
5 | 23 |
24 #include <stdio.h> | |
4662 | 25 #include <string.h> |
5 | 26 #include <sys/types.h> |
27 #include <pwd.h> | |
28 #include <grp.h> | |
29 | |
967
029c8dd12d60
(getuser): Return NULL (rather than stringified uid) upon lookup failure.
Jim Meyering <jim@meyering.net>
parents:
650
diff
changeset
|
30 #if HAVE_UNISTD_H |
029c8dd12d60
(getuser): Return NULL (rather than stringified uid) upon lookup failure.
Jim Meyering <jim@meyering.net>
parents:
650
diff
changeset
|
31 # include <unistd.h> |
5 | 32 #endif |
967
029c8dd12d60
(getuser): Return NULL (rather than stringified uid) upon lookup failure.
Jim Meyering <jim@meyering.net>
parents:
650
diff
changeset
|
33 |
4544 | 34 #include "xalloc.h" |
35 | |
1261
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
36 #ifdef __DJGPP__ |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
37 static char digits[] = "0123456789"; |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
38 #endif |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
39 |
5 | 40 struct userid |
41 { | |
42 union | |
43 { | |
44 uid_t u; | |
45 gid_t g; | |
46 } id; | |
47 char *name; | |
48 struct userid *next; | |
49 }; | |
50 | |
51 static struct userid *user_alist; | |
52 | |
1253 | 53 /* The members of this list have names not in the local passwd file. */ |
5 | 54 static struct userid *nouser_alist; |
55 | |
1253 | 56 /* Translate UID to a login name, with cache, or NULL if unresolved. */ |
5 | 57 |
58 char * | |
1253 | 59 getuser (uid_t uid) |
5 | 60 { |
61 register struct userid *tail; | |
62 struct passwd *pwent; | |
63 | |
64 for (tail = user_alist; tail; tail = tail->next) | |
65 if (tail->id.u == uid) | |
66 return tail->name; | |
67 | |
68 pwent = getpwuid (uid); | |
4544 | 69 tail = xmalloc (sizeof (struct userid)); |
5 | 70 tail->id.u = uid; |
1253 | 71 tail->name = pwent ? xstrdup (pwent->pw_name) : NULL; |
5 | 72 |
1253 | 73 /* Add to the head of the list, so most recently used is first. */ |
5 | 74 tail->next = user_alist; |
75 user_alist = tail; | |
76 return tail->name; | |
77 } | |
78 | |
79 /* Translate USER to a UID, with cache. | |
80 Return NULL if there is no such user. | |
81 (We also cache which user names have no passwd entry, | |
82 so we don't keep looking them up.) */ | |
83 | |
84 uid_t * | |
1253 | 85 getuidbyname (const char *user) |
5 | 86 { |
87 register struct userid *tail; | |
88 struct passwd *pwent; | |
89 | |
90 for (tail = user_alist; tail; tail = tail->next) | |
91 /* Avoid a function call for the most common case. */ | |
1253 | 92 if (*tail->name == *user && !strcmp (tail->name, user)) |
5 | 93 return &tail->id.u; |
94 | |
95 for (tail = nouser_alist; tail; tail = tail->next) | |
96 /* Avoid a function call for the most common case. */ | |
97 if (*tail->name == *user && !strcmp (tail->name, user)) | |
98 return 0; | |
99 | |
100 pwent = getpwnam (user); | |
1261
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
101 #ifdef __DJGPP__ |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
102 /* We need to pretend to be the user USER, to make |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
103 pwd functions know about an arbitrary user name. */ |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
104 if (!pwent && strspn (user, digits) < strlen (user)) |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
105 { |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
106 setenv ("USER", user, 1); |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
107 pwent = getpwnam (user); /* now it will succeed */ |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
108 } |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
109 #endif |
5 | 110 |
4544 | 111 tail = xmalloc (sizeof (struct userid)); |
5 | 112 tail->name = xstrdup (user); |
113 | |
1253 | 114 /* Add to the head of the list, so most recently used is first. */ |
5 | 115 if (pwent) |
116 { | |
117 tail->id.u = pwent->pw_uid; | |
118 tail->next = user_alist; | |
119 user_alist = tail; | |
120 return &tail->id.u; | |
121 } | |
122 | |
123 tail->next = nouser_alist; | |
124 nouser_alist = tail; | |
125 return 0; | |
126 } | |
127 | |
128 /* Use the same struct as for userids. */ | |
129 static struct userid *group_alist; | |
130 static struct userid *nogroup_alist; | |
131 | |
1253 | 132 /* Translate GID to a group name, with cache, or NULL if unresolved. */ |
5 | 133 |
134 char * | |
1253 | 135 getgroup (gid_t gid) |
5 | 136 { |
137 register struct userid *tail; | |
138 struct group *grent; | |
139 | |
140 for (tail = group_alist; tail; tail = tail->next) | |
141 if (tail->id.g == gid) | |
142 return tail->name; | |
143 | |
144 grent = getgrgid (gid); | |
4544 | 145 tail = xmalloc (sizeof (struct userid)); |
5 | 146 tail->id.g = gid; |
1253 | 147 tail->name = grent ? xstrdup (grent->gr_name) : NULL; |
5 | 148 |
149 /* Add to the head of the list, so most recently used is first. */ | |
150 tail->next = group_alist; | |
151 group_alist = tail; | |
152 return tail->name; | |
153 } | |
154 | |
967
029c8dd12d60
(getuser): Return NULL (rather than stringified uid) upon lookup failure.
Jim Meyering <jim@meyering.net>
parents:
650
diff
changeset
|
155 /* Translate GROUP to a GID, with cache. |
5 | 156 Return NULL if there is no such group. |
157 (We also cache which group names have no group entry, | |
158 so we don't keep looking them up.) */ | |
159 | |
160 gid_t * | |
1253 | 161 getgidbyname (const char *group) |
5 | 162 { |
163 register struct userid *tail; | |
164 struct group *grent; | |
165 | |
166 for (tail = group_alist; tail; tail = tail->next) | |
167 /* Avoid a function call for the most common case. */ | |
1253 | 168 if (*tail->name == *group && !strcmp (tail->name, group)) |
5 | 169 return &tail->id.g; |
170 | |
171 for (tail = nogroup_alist; tail; tail = tail->next) | |
172 /* Avoid a function call for the most common case. */ | |
173 if (*tail->name == *group && !strcmp (tail->name, group)) | |
174 return 0; | |
175 | |
176 grent = getgrnam (group); | |
1261
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
177 #ifdef __DJGPP__ |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
178 /* We need to pretend to belong to group GROUP, to make |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
179 grp functions know about any arbitrary group name. */ |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
180 if (!grent && strspn (group, digits) < strlen (group)) |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
181 { |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
182 setenv ("GROUP", group, 1); |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
183 grent = getgrnam (group); /* now it will succeed */ |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
184 } |
67cadf165f7f
(getuidbyname) [__DJGPP__]: Make function know about
Jim Meyering <jim@meyering.net>
parents:
1253
diff
changeset
|
185 #endif |
5 | 186 |
4544 | 187 tail = xmalloc (sizeof (struct userid)); |
5 | 188 tail->name = xstrdup (group); |
189 | |
190 /* Add to the head of the list, so most recently used is first. */ | |
191 if (grent) | |
192 { | |
193 tail->id.g = grent->gr_gid; | |
194 tail->next = group_alist; | |
195 group_alist = tail; | |
196 return &tail->id.g; | |
197 } | |
650
b4ef1c1a0171
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
5
diff
changeset
|
198 |
5 | 199 tail->next = nogroup_alist; |
200 nogroup_alist = tail; | |
201 return 0; | |
202 } |