Updated to fedora-glibc-20051116T0829
[glibc.git] / sysdeps / unix / sysv / linux / i386 / fxstatat.c
blobd5bc6021bc9b1c071ab7f2cdc961f288c292d932
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 /* 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 if (__builtin_expect (flag & ~AT_SYMLINK_NOFOLLOW, 0))
53 __set_errno (EINVAL);
54 return -1;
57 char *buf = NULL;
59 if (fd != AT_FDCWD && file[0] != '/')
61 size_t filelen = strlen (file);
62 static const char procfd[] = "/proc/self/fd/%d/%s";
63 /* Buffer for the path name we are going to use. It consists of
64 - the string /proc/self/fd/
65 - the file descriptor number
66 - the file name provided.
67 The final NUL is included in the sizeof. A bit of overhead
68 due to the format elements compensates for possible negative
69 numbers. */
70 size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
71 buf = alloca (buflen);
73 __snprintf (buf, buflen, procfd, fd, file);
74 file = buf;
77 #if __ASSUME_STAT64_SYSCALL == 0
78 struct kernel_stat kst;
79 #endif
80 int result;
81 INTERNAL_SYSCALL_DECL (err);
83 if (vers == _STAT_VER_KERNEL)
85 if (flag & AT_SYMLINK_NOFOLLOW)
86 result = INTERNAL_SYSCALL (lstat, err, 2, CHECK_STRING (file),
87 CHECK_1 ((struct kernel_stat *) st));
88 else
89 result = INTERNAL_SYSCALL (stat, err, 2, CHECK_STRING (file),
90 CHECK_1 ((struct kernel_stat *) st));
91 goto out;
94 #if __ASSUME_STAT64_SYSCALL > 0
95 struct stat64 st64;
97 if (flag & AT_SYMLINK_NOFOLLOW)
98 result = INTERNAL_SYSCALL (lstat64, err, 2, CHECK_STRING (file),
99 __ptrvalue (&st64));
100 else
101 result = INTERNAL_SYSCALL (stat64, err, 2, CHECK_STRING (file),
102 __ptrvalue (&st64));
103 if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
104 return __xstat32_conv (vers, &st64, st);
105 #else
106 # if defined __NR_stat64
107 /* To support 32 bit UIDs, we have to use stat64. The normal stat
108 call only returns 16 bit UIDs. */
109 if (! __have_no_stat64)
111 struct stat64 st64;
113 if (flag & AT_SYMLINK_NOFOLLOW)
114 result = INTERNAL_SYSCALL (lstat64, err, 2, CHECK_STRING (file),
115 __ptrvalue (&st64));
116 else
117 result = INTERNAL_SYSCALL (stat64, err, 2, CHECK_STRING (file),
118 __ptrvalue (&st64));
120 if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
121 result = __xstat32_conv (vers, &st64, st);
123 if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1)
124 || INTERNAL_SYSCALL_ERRNO (result, err) != ENOSYS)
125 goto out;
127 __have_no_stat64 = 1;
129 # endif
130 if (flag & AT_SYMLINK_NOFOLLOW)
131 result = INTERNAL_SYSCALL (lstat, err, 2, CHECK_STRING (file),
132 __ptrvalue (&kst));
133 else
134 result = INTERNAL_SYSCALL (stat, err, 2, CHECK_STRING (file),
135 __ptrvalue (&kst));
136 if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
137 return __xstat_conv (vers, &kst, st);
138 #endif /* __ASSUME_STAT64_SYSCALL */
140 out:
141 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 0))
143 __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), fd, buf);
144 result = -1;
147 return result;
149 #ifdef XSTAT_IS_XSTAT64
150 # undef __fxstatat64
151 strong_alias (__fxstatat, __fxstatat64);
152 #endif