Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/euidaccess.c @ 5157:62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
eaccess on Solaris and SVR4-like systems.
(euidaccess): Use HAVE_EACCESS, not HAVE_DECL_EACCESS.
[!defined LIBC]: Included group-member.h, stat-macros.h.
(S_IXUSR, S_IXGRP, S_IXOTH, S_IROTH, S_IWOTH, S_IXOTH):
Remove; now done by stat-macros.h.
(NGROUPS_MAX, group_member): Remove; now done by group-member.h.
No need to include <limits.h>.
(errno): Remove decl; we now assume C89 or better.
(access, getuid, getgid, geteuid, getegid, stat) [defined _LIBC]:
New macros.
(uid, gid, have_ids): Remove these static variables.
They weren't accurate for programs that also invoked setreuid etc.
(euidaccess) [defined EFF_ONLY_OK || defined ACC_SELF ||
HAVE_DECL_EACCSS]: Use builtin substitutes.
[defined _LIBC]: Ignore __libc_enable_secure; it's not a
correct optimization for programs run as root that later
invoke setreuid.
[no builtin substitutes && HAVE_DECL_SETREGID &&
PREFER_NONREENTRANT_EUIDACCESS]:
Use setreuid+setregid to get the correct answer.
[no builtin substitutes && ! (HAVE_DECL_SETREGID &&
PREFER_NONREENTRANT_EUIDACCESS)]:
Don't assume that the stat macros have their historical values,
as POSIX doesn't require this.
[defined TEST]: Include <stdlib.h>; don't include errno.h
twice; include <error.h> rather than "error.h".
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Fri, 06 Aug 2004 23:44:27 +0000 |
parents | f736e2167837 |
children | 8df21945af17 |
rev | line source |
---|---|
341 | 1 /* euidaccess -- check if effective user id can access file |
4531
67c56a27d86c
Merge euidaccess etc. from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4422
diff
changeset
|
2 |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
3 Copyright (C) 1990, 1991, 1995, 1998, 2000, 2003, 2004 Free |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
4 Software Foundation, Inc. |
4531
67c56a27d86c
Merge euidaccess etc. from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4422
diff
changeset
|
5 |
4422 | 6 This file is part of the GNU C Library. |
341 | 7 |
4422 | 8 This program is free software; you can redistribute it and/or modify |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation; either version 2, or (at your option) | |
11 any later version. | |
341 | 12 |
4422 | 13 This program is distributed in the hope that it will be useful, |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
341 | 17 |
4422 | 18 You should have received a copy of the GNU General Public License along |
19 with this program; if not, write to the Free Software Foundation, | |
20 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
341 | 21 |
509 | 22 /* Written by David MacKenzie and Torbjorn Granlund. |
23 Adapted for GNU C library by Roland McGrath. */ | |
341 | 24 |
1278
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
650
diff
changeset
|
25 #if HAVE_CONFIG_H |
615
1f485b09b153
(euidaccess) [!S_IROTH]: Define.
Jim Meyering <jim@meyering.net>
parents:
509
diff
changeset
|
26 # include <config.h> |
341 | 27 #endif |
28 | |
4531
67c56a27d86c
Merge euidaccess etc. from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4422
diff
changeset
|
29 #ifndef _LIBC |
67c56a27d86c
Merge euidaccess etc. from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4422
diff
changeset
|
30 # include "euidaccess.h" |
67c56a27d86c
Merge euidaccess etc. from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4422
diff
changeset
|
31 #endif |
67c56a27d86c
Merge euidaccess etc. from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4422
diff
changeset
|
32 |
341 | 33 #include <sys/types.h> |
34 #include <sys/stat.h> | |
35 | |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
36 #if HAVE_UNISTD_H || defined _LIBC |
615
1f485b09b153
(euidaccess) [!S_IROTH]: Define.
Jim Meyering <jim@meyering.net>
parents:
509
diff
changeset
|
37 # include <unistd.h> |
341 | 38 #endif |
39 | |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
40 #if HAVE_LIBGEN_H |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
41 # include <libgen.h> |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
42 #endif |
341 | 43 |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
44 #ifndef _POSIX_VERSION |
341 | 45 uid_t getuid (); |
46 gid_t getgid (); | |
47 uid_t geteuid (); | |
48 gid_t getegid (); | |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
49 #endif |
341 | 50 |
51 #include <errno.h> | |
2094
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
52 #ifndef __set_errno |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
53 # define __set_errno(val) errno = (val) |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
54 #endif |
341 | 55 |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
56 #if defined EACCES && !defined EACCESS |
615
1f485b09b153
(euidaccess) [!S_IROTH]: Define.
Jim Meyering <jim@meyering.net>
parents:
509
diff
changeset
|
57 # define EACCESS EACCES |
341 | 58 #endif |
59 | |
60 #ifndef F_OK | |
615
1f485b09b153
(euidaccess) [!S_IROTH]: Define.
Jim Meyering <jim@meyering.net>
parents:
509
diff
changeset
|
61 # define F_OK 0 |
1f485b09b153
(euidaccess) [!S_IROTH]: Define.
Jim Meyering <jim@meyering.net>
parents:
509
diff
changeset
|
62 # define X_OK 1 |
1f485b09b153
(euidaccess) [!S_IROTH]: Define.
Jim Meyering <jim@meyering.net>
parents:
509
diff
changeset
|
63 # define W_OK 2 |
1f485b09b153
(euidaccess) [!S_IROTH]: Define.
Jim Meyering <jim@meyering.net>
parents:
509
diff
changeset
|
64 # define R_OK 4 |
341 | 65 #endif |
66 | |
509 | 67 |
68 #ifdef _LIBC | |
69 | |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
70 # define access __access |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
71 # define getuid __getuid |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
72 # define getgid __getgid |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
73 # define geteuid __geteuid |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
74 # define getegid __getegid |
615
1f485b09b153
(euidaccess) [!S_IROTH]: Define.
Jim Meyering <jim@meyering.net>
parents:
509
diff
changeset
|
75 # define group_member __group_member |
2094
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
76 # define euidaccess __euidaccess |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
77 # undef stat |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
78 # define stat stat64 |
509 | 79 |
80 #else | |
81 | |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
82 # include "group-member.h" |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
83 # include "stat-macros.h" |
2094
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
84 |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
85 #endif |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
86 |
341 | 87 /* Return 0 if the user has permission of type MODE on file PATH; |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
88 otherwise, return -1 and set `errno'. |
341 | 89 Like access, except that it uses the effective user and group |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
90 id's instead of the real ones, and it does not always check for read-only |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
91 file system, text busy, etc. */ |
341 | 92 |
93 int | |
1557 | 94 euidaccess (const char *path, int mode) |
341 | 95 { |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
96 #if defined EFF_ONLY_OK |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
97 return access (path, mode | EFF_ONLY_OK); |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
98 #elif defined ACC_SELF |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
99 return accessx (path, mode, ACC_SELF); |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
100 #elif HAVE_EACCESS |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
101 return eaccess (path, mode); |
509 | 102 #else |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
103 |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
104 uid_t uid = getuid (); |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
105 gid_t gid = getgid (); |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
106 uid_t euid = geteuid (); |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
107 gid_t egid = getegid (); |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
108 struct stat stats; |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
109 |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
110 # if HAVE_DECL_SETREGID && PREFER_NONREENTRANT_EUIDACCESS |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
111 |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
112 /* Define PREFER_NONREENTRANT_EUIDACCESS if you prefer euidaccess to |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
113 return the correct result even if this would make it |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
114 nonreentrant. Define this only if your entire application is |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
115 safe even if the uid or gid might temporarily change. If your |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
116 application uses signal handlers or threads it is probably not |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
117 safe. */ |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
118 |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
119 if (mode == F_OK) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
120 return stat (path, &stats); |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
121 else |
341 | 122 { |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
123 int result; |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
124 int saved_errno; |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
125 |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
126 if (uid != euid) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
127 setreuid (euid, uid); |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
128 if (gid != egid) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
129 setregid (egid, gid); |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
130 |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
131 result = access (path, mode); |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
132 saved_errno = errno; |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
133 |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
134 /* Restore them. */ |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
135 if (uid != euid) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
136 setreuid (uid, euid); |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
137 if (gid != egid) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
138 setregid (gid, egid); |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
139 |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
140 errno = saved_errno; |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
141 return result; |
341 | 142 } |
143 | |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
144 # else |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
145 |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
146 /* The following code assumes the traditional Unix model, and is not |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
147 correct on systems that have ACLs or the like. However, it's |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
148 better than nothing, and it is reentrant. */ |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
149 |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
150 unsigned int granted; |
341 | 151 if (uid == euid && gid == egid) |
509 | 152 /* If we are not set-uid or set-gid, access does the same. */ |
153 return access (path, mode); | |
341 | 154 |
425
4895c6a012c5
(eaccess_stat): Make statp and path arguments const.
Jim Meyering <jim@meyering.net>
parents:
384
diff
changeset
|
155 if (stat (path, &stats)) |
341 | 156 return -1; |
157 | |
509 | 158 /* The super-user can read and write any file, and execute any file |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
159 that anyone can execute. */ |
509 | 160 if (euid == 0 && ((mode & X_OK) == 0 |
161 || (stats.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))) | |
162 return 0; | |
341 | 163 |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
164 /* Convert the mode to traditional form, clearing any bogus bits. */ |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
165 if (R_OK == 4 && W_OK == 2 && X_OK == 1 && F_OK == 0) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
166 mode &= 7; |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
167 else |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
168 mode = ((mode & R_OK ? 4 : 0) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
169 + (mode & W_OK ? 2 : 0) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
170 + (mode & X_OK ? 1 : 0)); |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
171 |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
172 if (mode == 0) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
173 return 0; /* The file exists. */ |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
174 |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
175 /* Convert the file's permission bits to traditional form. */ |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
176 if (S_IRUSR == (4 << 6) && S_IWUSR == (2 << 6) && S_IXUSR == (1 << 6) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
177 && S_IRGRP == (4 << 3) && S_IWGRP == (2 << 3) && S_IXGRP == (1 << 3) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
178 && S_IROTH == (4 << 0) && S_IWOTH == (2 << 0) && S_IXOTH == (1 << 0)) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
179 granted = stats.st_mode; |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
180 else |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
181 granted = ((stats.st_mode & S_IRUSR ? 4 << 6 : 0) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
182 + (stats.st_mode & S_IWUSR ? 2 << 6 : 0) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
183 + (stats.st_mode & S_IXUSR ? 1 << 6 : 0) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
184 + (stats.st_mode & S_IRGRP ? 4 << 3 : 0) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
185 + (stats.st_mode & S_IWGRP ? 2 << 3 : 0) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
186 + (stats.st_mode & S_IXGRP ? 1 << 3 : 0) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
187 + (stats.st_mode & S_IROTH ? 4 << 0 : 0) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
188 + (stats.st_mode & S_IWOTH ? 2 << 0 : 0) |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
189 + (stats.st_mode & S_IXOTH ? 1 << 0 : 0)); |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
190 |
509 | 191 if (euid == stats.st_uid) |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
192 granted >>= 6; |
509 | 193 else if (egid == stats.st_gid || group_member (stats.st_gid)) |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
194 granted >>= 3; |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
195 |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
196 if ((mode & ~granted) == 0) |
509 | 197 return 0; |
2094
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
198 __set_errno (EACCESS); |
509 | 199 return -1; |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
200 |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
201 # endif |
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
202 #endif |
341 | 203 } |
2094
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
204 #undef euidaccess |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
205 #ifdef weak_alias |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
206 weak_alias (__euidaccess, euidaccess) |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
207 #endif |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
208 |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
209 #ifdef TEST |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
210 # include <error.h> |
2094
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
211 # include <stdio.h> |
5157
62fa339919f4
[HAVE_LIBGEN_H]: Include <libgen.h>, for
Paul Eggert <eggert@cs.ucla.edu>
parents:
4637
diff
changeset
|
212 # include <stdlib.h> |
2094
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
213 |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
214 char *program_name; |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
215 |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
216 int |
4637
f736e2167837
(main): Define with a prototype.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4531
diff
changeset
|
217 main (int argc, char **argv) |
2094
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
218 { |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
219 char *file; |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
220 int mode; |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
221 int err; |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
222 |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
223 program_name = argv[0]; |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
224 if (argc < 3) |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
225 abort (); |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
226 file = argv[1]; |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
227 mode = atoi (argv[2]); |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
228 |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
229 err = euidaccess (file, mode); |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
230 printf ("%d\n", err); |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
231 if (err != 0) |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
232 error (0, errno, "%s", file); |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
233 exit (0); |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
234 } |
92c4b5c4b148
Sync with the GNU C library.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
235 #endif |