Move mips from ports to libc.
[jlayton/glibc.git] / sysdeps / unix / sysv / linux / mips / mips64 / fxstatat64.c
1 /* Copyright (C) 2005-2014 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
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.
8
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.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library.  If not, see
16    <http://www.gnu.org/licenses/>.  */
17
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <stddef.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <kernel_stat.h>
25
26 #include <sysdep.h>
27 #include <sys/syscall.h>
28
29 #include <kernel-features.h>
30
31 #include <xstatconv.h>
32
33 /* Get information about the file NAME in BUF.  */
34
35 int
36 __fxstatat64 (int vers, int fd, const char *file, struct stat64 *st, int flag)
37 {
38   if (__builtin_expect (vers != _STAT_VER_LINUX, 0))
39     {
40       __set_errno (EINVAL);
41       return -1;
42     }
43
44   int result;
45   INTERNAL_SYSCALL_DECL (err);
46   struct kernel_stat kst;
47
48 #ifdef __NR_newfstatat
49 # ifndef __ASSUME_ATFCTS
50   if (__have_atfcts >= 0)
51 # endif
52     {
53       result = INTERNAL_SYSCALL (newfstatat, err, 4, fd, file, &kst, flag);
54 # ifndef __ASSUME_ATFCTS
55       if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 1)
56           && INTERNAL_SYSCALL_ERRNO (result, err) == ENOSYS)
57         __have_atfcts = -1;
58       else
59 # endif
60         if (!__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 1))
61           return __xstat64_conv (vers, &kst, st);
62         else
63           {
64             __set_errno (INTERNAL_SYSCALL_ERRNO (result, err));
65             return -1;
66           }
67     }
68 #endif
69
70 #ifndef __ASSUME_ATFCTS
71   if (flag & ~AT_SYMLINK_NOFOLLOW)
72     {
73       __set_errno (EINVAL);
74       return -1;
75     }
76
77   char *buf = NULL;
78
79   if (fd != AT_FDCWD && file[0] != '/')
80     {
81       size_t filelen = strlen (file);
82       static const char procfd[] = "/proc/self/fd/%d/%s";
83       /* Buffer for the path name we are going to use.  It consists of
84          - the string /proc/self/fd/
85          - the file descriptor number
86          - the file name provided.
87          The final NUL is included in the sizeof.   A bit of overhead
88          due to the format elements compensates for possible negative
89          numbers.  */
90       size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
91       buf = alloca (buflen);
92
93       __snprintf (buf, buflen, procfd, fd, file);
94       file = buf;
95     }
96
97   if (flag & AT_SYMLINK_NOFOLLOW)
98     result = INTERNAL_SYSCALL (lstat, err, 2, file, &kst);
99   else
100     result = INTERNAL_SYSCALL (stat, err, 2, file, &kst);
101
102   if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
103     return __xstat64_conv (vers, &kst, st);
104
105   __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), fd, buf);
106   return -1;
107 #endif
108 }
109 libc_hidden_def (__fxstatat64)