Add prototype for tee.
[glibc/history.git] / sysdeps / unix / sysv / linux / powerpc / fchownat.c
blob1fbae5c44872dfa60897af2de7bb9acce46115f1
1 /* Copyright (C) 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <limits.h>
25 #include <sysdep.h>
26 #include <stdlib.h>
28 #include <kernel-features.h>
31 In Linux 2.1.x the chown functions have been changed. A new function lchown
32 was introduced. The new chown now follows symlinks - the old chown and the
33 new lchown do not follow symlinks.
34 This file emulates chown() under the old kernels.
37 int
38 fchownat (int fd, const char *file, uid_t owner, gid_t group, int flag)
40 if (flag & ~AT_SYMLINK_NOFOLLOW)
42 __set_errno (EINVAL);
43 return -1;
46 char *buf = NULL;
48 if (fd != AT_FDCWD && file[0] != '/')
50 size_t filelen = strlen (file);
51 static const char procfd[] = "/proc/self/fd/%d/%s";
52 /* Buffer for the path name we are going to use. It consists of
53 - the string /proc/self/fd/
54 - the file descriptor number
55 - the file name provided.
56 The final NUL is included in the sizeof. A bit of overhead
57 due to the format elements compensates for possible negative
58 numbers. */
59 size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
60 buf = alloca (buflen);
62 __snprintf (buf, buflen, procfd, fd, file);
63 file = buf;
66 int result;
67 INTERNAL_SYSCALL_DECL (err);
69 #if __ASSUME_LCHOWN_SYSCALL
70 if (flag & AT_SYMLINK_NOFOLLOW)
71 result = INTERNAL_SYSCALL (lchown, err, 3, file, owner, group);
72 else
73 result = INTERNAL_SYSCALL (chown, err, 3, file, owner, group);
74 #else
75 char link[PATH_MAX + 2];
76 char path[2 * PATH_MAX + 4];
77 int loopct;
78 size_t filelen;
79 static int libc_old_chown = 0 /* -1=old linux, 1=new linux, 0=unknown */;
81 if (libc_old_chown == 1)
83 if (flag & AT_SYMLINK_NOFOLLOW)
84 result = INTERNAL_SYSCALL (lchown, err, 3, __ptrvalue (file), owner,
85 group);
86 else
87 result = INTERNAL_SYSCALL (chown, err, 3, __ptrvalue (file), owner,
88 group);
89 goto out;
92 # ifdef __NR_lchown
93 if (flag & AT_SYMLINK_NOFOLLOW)
95 result = INTERNAL_SYSCALL (lchown, err, 3, __ptrvalue (file), owner,
96 group);
97 goto out;
100 if (libc_old_chown == 0)
102 result = INTERNAL_SYSCALL (chown, err, 3, __ptrvalue (file), owner,
103 group);
104 if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
105 return result;
106 if (INTERNAL_SYSCALL_ERRNO (result, err) != ENOSYS)
108 libc_old_chown = 1;
109 goto fail;
111 libc_old_chown = -1;
113 # else
114 if (flag & AT_SYMLINK_NOFOLLOW)
116 result = INTERNAL_SYSCALL (chown, err, 3, __ptrvalue (file), owner,
117 group);
118 goto out;
120 # endif
122 result = __readlink (file, link, PATH_MAX + 1);
123 if (result == -1)
125 # ifdef __NR_lchown
126 result = INTERNAL_SYSCALL (lchown, err, 3, __ptrvalue (file), owner,
127 group);
128 # else
129 result = INTERNAL_SYSCALL (chown, err, 3, __ptrvalue (file), owner,
130 group);
131 # endif
132 goto out;
135 filelen = strlen (file) + 1;
136 if (filelen > sizeof (path))
138 errno = ENAMETOOLONG;
139 return -1;
141 memcpy (path, file, filelen);
143 /* 'The system has an arbitrary limit...' In practise, we'll hit
144 ENAMETOOLONG before this, usually. */
145 for (loopct = 0; loopct < 128; ++loopct)
147 size_t linklen;
149 if (result >= PATH_MAX + 1)
151 errno = ENAMETOOLONG;
152 return -1;
155 link[result] = 0; /* Null-terminate string, just-in-case. */
157 linklen = strlen (link) + 1;
159 if (link[0] == '/')
160 memcpy (path, link, linklen);
161 else
163 filelen = strlen (path);
165 while (filelen > 1 && path[filelen - 1] == '/')
166 --filelen;
167 while (filelen > 0 && path[filelen - 1] != '/')
168 --filelen;
169 if (filelen + linklen > sizeof (path))
171 errno = ENAMETOOLONG;
172 return -1;
174 memcpy (path + filelen, link, linklen);
177 result = __readlink (path, link, PATH_MAX + 1);
179 if (result == -1)
181 # ifdef __NR_lchown
182 result = INTERNAL_SYSCALL (lchown, err, 3, path, owner, group);
183 # else
184 result = INTERNAL_SYSCALL (chown, err, 3, path, owner, group);
185 # endif
186 goto out;
189 __set_errno (ELOOP);
190 return -1;
192 out:
193 #endif
195 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 0))
197 #if !__ASSUME_LCHOWN_SYSCALL
198 fail:
199 #endif
200 __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), fd, buf);
201 result = -1;
204 return result;