2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / i386 / fxstatat.c
blob94f6e811863320d031e3a55bc9d70d2a8bdda9c5
1 /* Copyright (C) 2005, 2006 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 /* Ho hum, if fxstatat == fxstatat64 we must get rid of the prototype or gcc
20 will complain since they don't strictly match. */
21 #define __fxstatat64 __fxstatat64_disable
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <kernel_stat.h>
31 #include <sysdep.h>
32 #include <sys/syscall.h>
33 #include <bp-checks.h>
35 #include <kernel-features.h>
37 #include <xstatconv.h>
39 #ifdef __NR_stat64
40 # if __ASSUME_STAT64_SYSCALL == 0
41 /* The variable is shared between all wrappers around *stat64 calls. */
42 extern int __have_no_stat64;
43 # endif
44 #endif
47 /* Get information about the file NAME relative to FD in ST. */
48 int
49 __fxstatat (int vers, int fd, const char *file, struct stat *st, int flag)
51 int result;
52 INTERNAL_SYSCALL_DECL (err);
53 struct stat64 st64;
55 #ifdef __NR_fstatat64
56 # ifndef __ASSUME_ATFCTS
57 if (__have_atfcts >= 0)
58 # endif
60 result = INTERNAL_SYSCALL (fstatat64, err, 4, fd, file, &st64, flag);
61 # ifndef __ASSUME_ATFCTS
62 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 1)
63 && INTERNAL_SYSCALL_ERRNO (result, err) == ENOSYS)
64 __have_atfcts = -1;
65 else
66 # endif
67 if (!__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 1))
68 return __xstat32_conv (vers, &st64, st);
69 else
71 __set_errno (INTERNAL_SYSCALL_ERRNO (result, err));
72 return -1;
75 #endif
77 #ifndef __ASSUME_ATFCTS
78 if (__builtin_expect (flag & ~AT_SYMLINK_NOFOLLOW, 0))
80 __set_errno (EINVAL);
81 return -1;
84 char *buf = NULL;
86 if (fd != AT_FDCWD && file[0] != '/')
88 size_t filelen = strlen (file);
89 static const char procfd[] = "/proc/self/fd/%d/%s";
90 /* Buffer for the path name we are going to use. It consists of
91 - the string /proc/self/fd/
92 - the file descriptor number
93 - the file name provided.
94 The final NUL is included in the sizeof. A bit of overhead
95 due to the format elements compensates for possible negative
96 numbers. */
97 size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
98 buf = alloca (buflen);
100 __snprintf (buf, buflen, procfd, fd, file);
101 file = buf;
104 # if __ASSUME_STAT64_SYSCALL == 0
105 struct kernel_stat kst;
106 # endif
107 if (vers == _STAT_VER_KERNEL)
109 if (flag & AT_SYMLINK_NOFOLLOW)
110 result = INTERNAL_SYSCALL (lstat, err, 2, CHECK_STRING (file),
111 CHECK_1 ((struct kernel_stat *) st));
112 else
113 result = INTERNAL_SYSCALL (stat, err, 2, CHECK_STRING (file),
114 CHECK_1 ((struct kernel_stat *) st));
115 goto out;
118 # if __ASSUME_STAT64_SYSCALL > 0
120 if (flag & AT_SYMLINK_NOFOLLOW)
121 result = INTERNAL_SYSCALL (lstat64, err, 2, CHECK_STRING (file),
122 __ptrvalue (&st64));
123 else
124 result = INTERNAL_SYSCALL (stat64, err, 2, CHECK_STRING (file),
125 __ptrvalue (&st64));
126 if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
127 return __xstat32_conv (vers, &st64, st);
128 # else
129 # if defined __NR_stat64
130 /* To support 32 bit UIDs, we have to use stat64. The normal stat
131 call only returns 16 bit UIDs. */
132 if (! __have_no_stat64)
134 if (flag & AT_SYMLINK_NOFOLLOW)
135 result = INTERNAL_SYSCALL (lstat64, err, 2, CHECK_STRING (file),
136 __ptrvalue (&st64));
137 else
138 result = INTERNAL_SYSCALL (stat64, err, 2, CHECK_STRING (file),
139 __ptrvalue (&st64));
141 if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
142 result = __xstat32_conv (vers, &st64, st);
144 if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1)
145 || INTERNAL_SYSCALL_ERRNO (result, err) != ENOSYS)
146 goto out;
148 __have_no_stat64 = 1;
150 # endif
151 if (flag & AT_SYMLINK_NOFOLLOW)
152 result = INTERNAL_SYSCALL (lstat, err, 2, CHECK_STRING (file),
153 __ptrvalue (&kst));
154 else
155 result = INTERNAL_SYSCALL (stat, err, 2, CHECK_STRING (file),
156 __ptrvalue (&kst));
157 if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
158 return __xstat_conv (vers, &kst, st);
159 # endif /* __ASSUME_STAT64_SYSCALL */
161 out:
162 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 0))
164 __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), fd, buf);
165 result = -1;
168 return result;
169 #endif
171 libc_hidden_def (__fxstatat)
172 #ifdef XSTAT_IS_XSTAT64
173 # undef __fxstatat64
174 strong_alias (__fxstatat, __fxstatat64);
175 libc_hidden_ver (__fxstatat, __fxstatat64)
176 #endif