Mercurial > hg > octave-lojdl > gnulib-hg
annotate lib/lchown.c @ 17464:290d581e2e24
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Sat, 10 Aug 2013 07:10:55 -0700 |
parents | e542fd46ad6f |
children |
rev | line source |
---|---|
1365 | 1 /* Provide a stub lchown function for systems that lack it. |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
2 |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16201
diff
changeset
|
3 Copyright (C) 1998-1999, 2002, 2004, 2006-2007, 2009-2013 Free Software |
12518
b5e42ef33b49
update nearly all FSF copyright year lists to include 2009
Jim Meyering <meyering@redhat.com>
parents:
12314
diff
changeset
|
4 Foundation, Inc. |
1365 | 5 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8982
diff
changeset
|
6 This program is free software: you can redistribute it and/or modify |
1365 | 7 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:
8982
diff
changeset
|
8 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:
8982
diff
changeset
|
9 (at your option) any later version. |
1365 | 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 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8982
diff
changeset
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
1365 | 18 |
19 /* written by Jim Meyering */ | |
20 | |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
21 #include <config.h> |
1365 | 22 |
8982
04a20e8bea33
Revamp lchown so that it lives in unistd.h where it belongs.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7581
diff
changeset
|
23 #include <unistd.h> |
04a20e8bea33
Revamp lchown so that it lives in unistd.h where it belongs.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7581
diff
changeset
|
24 |
04a20e8bea33
Revamp lchown so that it lives in unistd.h where it belongs.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7581
diff
changeset
|
25 #include <errno.h> |
12314 | 26 #include <stdbool.h> |
12290
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
27 #include <string.h> |
8982
04a20e8bea33
Revamp lchown so that it lives in unistd.h where it belongs.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7581
diff
changeset
|
28 #include <sys/stat.h> |
5159 | 29 |
12290
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
30 #if !HAVE_LCHOWN |
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
31 |
12289
e973be2fb39f
chown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
11964
diff
changeset
|
32 /* If the system chown does not follow symlinks, we don't want it |
e973be2fb39f
chown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
11964
diff
changeset
|
33 replaced by gnulib's chown, which does follow symlinks. */ |
12290
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
34 # if CHOWN_MODIFIES_SYMLINK |
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
35 # undef chown |
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
36 # endif |
12289
e973be2fb39f
chown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
11964
diff
changeset
|
37 |
1365 | 38 /* Work just like chown, except when FILE is a symbolic link. |
5159 | 39 In that case, set errno to EOPNOTSUPP and return -1. |
5078
146129232fe0
Merge from coreutils CVS.
Paul Eggert <eggert@cs.ucla.edu>
parents:
3868
diff
changeset
|
40 But if autoconf tests determined that chown modifies |
146129232fe0
Merge from coreutils CVS.
Paul Eggert <eggert@cs.ucla.edu>
parents:
3868
diff
changeset
|
41 symlinks, then just call chown. */ |
1365 | 42 |
43 int | |
1377 | 44 lchown (const char *file, uid_t uid, gid_t gid) |
1365 | 45 { |
12290
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
46 # if HAVE_CHOWN |
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
47 # if ! CHOWN_MODIFIES_SYMLINK |
1365 | 48 struct stat stats; |
49 | |
1378
5b37c6d8cd35
Fix braino: check S_ISLNK only if lstat succeeds.
Jim Meyering <jim@meyering.net>
parents:
1377
diff
changeset
|
50 if (lstat (file, &stats) == 0 && S_ISLNK (stats.st_mode)) |
1365 | 51 { |
5159 | 52 errno = EOPNOTSUPP; |
1365 | 53 return -1; |
54 } | |
12290
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
55 # endif |
1365 | 56 |
57 return chown (file, uid, gid); | |
11920
79f4b013feab
chown: avoid compilation warning on mingw
Eric Blake <ebb9@byu.net>
parents:
9309
diff
changeset
|
58 |
12290
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
59 # else /* !HAVE_CHOWN */ |
11964
ebddaf9cb5db
Set errno to ENOSYS when a function is entirely unsupported.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
60 errno = ENOSYS; |
11920
79f4b013feab
chown: avoid compilation warning on mingw
Eric Blake <ebb9@byu.net>
parents:
9309
diff
changeset
|
61 return -1; |
12290
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
62 # endif |
1365 | 63 } |
12290
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
64 |
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
65 #else /* HAVE_LCHOWN */ |
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
66 |
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
67 # undef lchown |
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
68 |
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
69 /* Work around trailing slash bugs in lchown. */ |
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
70 int |
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
71 rpl_lchown (const char *file, uid_t uid, gid_t gid) |
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
72 { |
12314 | 73 bool stat_valid = false; |
74 int result; | |
75 | |
76 # if CHOWN_CHANGE_TIME_BUG | |
13017
d4b51e8a5c23
lchown: Avoid "unused variable" warning.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
77 struct stat st; |
d4b51e8a5c23
lchown: Avoid "unused variable" warning.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
78 |
12314 | 79 if (gid != (gid_t) -1 || uid != (uid_t) -1) |
80 { | |
81 if (lstat (file, &st)) | |
82 return -1; | |
83 stat_valid = true; | |
84 if (!S_ISLNK (st.st_mode)) | |
85 return chown (file, uid, gid); | |
86 } | |
87 # endif | |
88 | |
89 # if CHOWN_TRAILING_SLASH_BUG | |
90 if (!stat_valid) | |
91 { | |
92 size_t len = strlen (file); | |
93 if (len && file[len - 1] == '/') | |
94 return chown (file, uid, gid); | |
95 } | |
96 # endif | |
97 | |
98 result = lchown (file, uid, gid); | |
99 | |
100 # if CHOWN_CHANGE_TIME_BUG && HAVE_LCHMOD | |
101 if (result == 0 && stat_valid | |
102 && (uid == st.st_uid || uid == (uid_t) -1) | |
103 && (gid == st.st_gid || gid == (gid_t) -1)) | |
104 { | |
105 /* No change in ownership, but at least one argument was not -1, | |
106 so we are required to update ctime. Since lchown succeeded, | |
107 we assume that lchmod will do likewise. But if the system | |
108 lacks lchmod and lutimes, we are out of luck. Oh well. */ | |
109 result = lchmod (file, st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | |
110 | S_ISUID | S_ISGID | S_ISVTX)); | |
111 } | |
112 # endif | |
113 | |
114 return result; | |
12290
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
115 } |
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
116 |
7e3695d9b328
lchown: detect Solaris and FreeBSD bug
Eric Blake <ebb9@byu.net>
parents:
12289
diff
changeset
|
117 #endif /* HAVE_LCHOWN */ |