Updated to fedora-glibc-20051116T0829
[glibc.git] / sysdeps / unix / sysv / linux / fxstatat64.c
blob2360f50d98e1a2738c2a41350974a5255a01ef20
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 <stddef.h>
22 #include <stdio.h>
23 #include <sys/stat.h>
24 #include <kernel_stat.h>
26 #include <sysdep.h>
27 #include <sys/syscall.h>
28 #include <bp-checks.h>
30 #include "kernel-features.h"
32 #if __ASSUME_STAT64_SYSCALL == 0
33 # include <xstatconv.h>
34 #endif
36 #ifdef __NR_stat64
37 # if __ASSUME_STAT64_SYSCALL == 0
38 /* The variable is shared between all wrappers around *stat64 calls.
39 This is the definition. */
40 extern int __have_no_stat64;
41 # endif
42 #endif
44 /* Get information about the file NAME in BUF. */
46 int
47 __fxstatat64 (int vers, int fd, const char *file, struct stat64 *st, int flag)
49 if (flag & ~AT_SYMLINK_NOFOLLOW)
51 __set_errno (EINVAL);
52 return -1;
55 char *buf = NULL;
57 if (fd != AT_FDCWD && file[0] != '/')
59 size_t filelen = strlen (file);
60 static const char procfd[] = "/proc/self/fd/%d/%s";
61 /* Buffer for the path name we are going to use. It consists of
62 - the string /proc/self/fd/
63 - the file descriptor number
64 - the file name provided.
65 The final NUL is included in the sizeof. A bit of overhead
66 due to the format elements compensates for possible negative
67 numbers. */
68 size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
69 buf = alloca (buflen);
71 __snprintf (buf, buflen, procfd, fd, file);
72 file = buf;
75 int result;
76 INTERNAL_SYSCALL_DECL (err);
78 #if __ASSUME_STAT64_SYSCALL > 0
79 if (flag & AT_SYMLINK_NOFOLLOW)
80 result = INTERNAL_SYSCALL (lstat64, err, 2, CHECK_STRING (file),
81 CHECK_1 (st));
82 else
83 result = INTERNAL_SYSCALL (stat64, err, 2, CHECK_STRING (file),
84 CHECK_1 (st));
85 if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
87 # if defined _HAVE_STAT64___ST_INO && __ASSUME_ST_INO_64_BIT == 0
88 if (st->__st_ino != (__ino_t) st->st_ino)
89 st->st_ino = st->__st_ino;
90 # endif
91 return result;
93 #else
94 struct kernel_stat kst;
95 # if defined __NR_stat64
96 if (! __have_no_stat64)
98 if (flag & AT_SYMLINK_NOFOLLOW)
99 result = INTERNAL_SYSCALL (lstat64, err, 2, CHECK_STRING (file),
100 CHECK_1 (st));
101 else
102 result = INTERNAL_SYSCALL (stat64, err, 2, CHECK_STRING (file),
103 CHECK_1 (st));
105 if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
107 # if defined _HAVE_STAT64___ST_INO && __ASSUME_ST_INO_64_BIT == 0
108 if (st->__st_ino != (__ino_t) st->st_ino)
109 st->st_ino = st->__st_ino;
110 # endif
111 return result;
113 if (INTERNAL_SYSCALL_ERRNO (result, err) != ENOSYS)
114 goto fail;
116 __have_no_stat64 = 1;
118 # endif
120 if (flag & AT_SYMLINK_NOFOLLOW)
121 result = INTERNAL_SYSCALL (lstat, err, 2, CHECK_STRING (file),
122 __ptrvalue (&kst));
123 else
124 result = INTERNAL_SYSCALL (stat, err, 2, CHECK_STRING (file),
125 __ptrvalue (&kst));
127 if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
128 return __xstat64_conv (vers, &kst, st);
130 fail:
131 #endif
132 __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), fd, buf);
134 return -1;