Back to index

glibc  2.9
fxstatat64.c
Go to the documentation of this file.
00001 /* Copyright (C) 2005,2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <fcntl.h>
00021 #include <stddef.h>
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <sys/stat.h>
00025 #include <kernel_stat.h>
00026 
00027 #include <sysdep.h>
00028 #include <sys/syscall.h>
00029 #include <bp-checks.h>
00030 
00031 #include <kernel-features.h>
00032 
00033 #include <xstatconv.h>
00034 
00035 /* Get information about the file NAME in BUF.  */
00036 
00037 int
00038 __fxstatat64 (int vers, int fd, const char *file, struct stat64 *st, int flag)
00039 {
00040   if (__builtin_expect (vers != _STAT_VER_LINUX, 0))
00041     {
00042       __set_errno (EINVAL);
00043       return -1;
00044     }
00045 
00046   int result;
00047   INTERNAL_SYSCALL_DECL (err);
00048   struct kernel_stat kst;
00049 
00050 #ifdef __NR_newfstatat
00051 # ifndef __ASSUME_ATFCTS
00052   if (__have_atfcts >= 0)
00053 # endif
00054     {
00055       result = INTERNAL_SYSCALL (newfstatat, err, 4, fd, file, &kst, flag);
00056 # ifndef __ASSUME_ATFCTS
00057       if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 1)
00058          && INTERNAL_SYSCALL_ERRNO (result, err) == ENOSYS)
00059        __have_atfcts = -1;
00060       else
00061 # endif
00062        if (!__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 1))
00063          return __xstat64_conv (vers, &kst, st);
00064        else
00065          {
00066            __set_errno (INTERNAL_SYSCALL_ERRNO (result, err));
00067            return -1;
00068          }
00069     }
00070 #endif
00071 
00072 #ifndef __ASSUME_ATFCTS
00073   if (flag & ~AT_SYMLINK_NOFOLLOW)
00074     {
00075       __set_errno (EINVAL);
00076       return -1;
00077     }
00078 
00079   char *buf = NULL;
00080 
00081   if (fd != AT_FDCWD && file[0] != '/')
00082     {
00083       size_t filelen = strlen (file);
00084       static const char procfd[] = "/proc/self/fd/%d/%s";
00085       /* Buffer for the path name we are going to use.  It consists of
00086         - the string /proc/self/fd/
00087         - the file descriptor number
00088         - the file name provided.
00089         The final NUL is included in the sizeof.   A bit of overhead
00090         due to the format elements compensates for possible negative
00091         numbers.  */
00092       size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
00093       buf = alloca (buflen);
00094 
00095       __snprintf (buf, buflen, procfd, fd, file);
00096       file = buf;
00097     }
00098 
00099   if (flag & AT_SYMLINK_NOFOLLOW)
00100     result = INTERNAL_SYSCALL (lstat, err, 2, CHECK_STRING (file),
00101                             __ptrvalue (&kst));
00102   else
00103     result = INTERNAL_SYSCALL (stat, err, 2, CHECK_STRING (file),
00104                             __ptrvalue (&kst));
00105 
00106   if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
00107     return __xstat64_conv (vers, &kst, st);
00108 
00109   __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), fd, buf);
00110   return -1;
00111 #endif
00112 }
00113 libc_hidden_def (__fxstatat64)