Back to index

glibc  2.9
fxstatat.c
Go to the documentation of this file.
00001 /* Copyright (C) 2005, 2006, 2007 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 /* Ho hum, if fxstatat == fxstatat64 we must get rid of the prototype or gcc
00020    will complain since they don't strictly match.  */
00021 #define __fxstatat64 __fxstatat64_disable
00022 
00023 #include <errno.h>
00024 #include <fcntl.h>
00025 #include <stddef.h>
00026 #include <stdio.h>
00027 #include <sys/stat.h>
00028 #include <kernel_stat.h>
00029 
00030 #include <sysdep.h>
00031 #include <sys/syscall.h>
00032 #include <bp-checks.h>
00033 #include <kernel-features.h>
00034 
00035 #include <xstatconv.h>
00036 
00037 /* Get information about the file NAME in BUF.  */
00038 int
00039 __fxstatat (int vers, int fd, const char *file, struct stat *st, int flag)
00040 {
00041   int result;
00042   INTERNAL_SYSCALL_DECL (err);
00043 #ifdef STAT_IS_KERNEL_STAT
00044 # define kst (*st)
00045 #else
00046   struct kernel_stat kst;
00047 #endif
00048 
00049 #ifdef __NR_newfstatat
00050 # ifndef __ASSUME_ATFCTS
00051   if (__have_atfcts >= 0)
00052 # endif
00053     {
00054       result = INTERNAL_SYSCALL (newfstatat, err, 4, fd, file, &kst, flag);
00055 # ifndef __ASSUME_ATFCTS
00056       if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 1)
00057          && INTERNAL_SYSCALL_ERRNO (result, err) == ENOSYS)
00058        __have_atfcts = -1;
00059       else
00060 # endif
00061        if (!__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 1))
00062          {
00063 #ifdef STAT_IS_KERNEL_STAT
00064            return 0;
00065 #else
00066            return __xstat_conv (vers, &kst, st);
00067 #endif
00068          }
00069        else
00070          {
00071            __set_errno (INTERNAL_SYSCALL_ERRNO (result, err));
00072            return -1;
00073          }
00074     }
00075 #endif
00076 
00077   if (flag & ~AT_SYMLINK_NOFOLLOW)
00078     {
00079       __set_errno (EINVAL);
00080       return -1;
00081     }
00082 
00083   char *buf = NULL;
00084 
00085   if (fd != AT_FDCWD && file[0] != '/')
00086     {
00087       size_t filelen = strlen (file);
00088       static const char procfd[] = "/proc/self/fd/%d/%s";
00089       /* Buffer for the path name we are going to use.  It consists of
00090         - the string /proc/self/fd/
00091         - the file descriptor number
00092         - the file name provided.
00093         The final NUL is included in the sizeof.   A bit of overhead
00094         due to the format elements compensates for possible negative
00095         numbers.  */
00096       size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
00097       buf = alloca (buflen);
00098 
00099       __snprintf (buf, buflen, procfd, fd, file);
00100       file = buf;
00101     }
00102 
00103   if (vers == _STAT_VER_KERNEL)
00104     {
00105       if (flag & AT_SYMLINK_NOFOLLOW)
00106        result = INTERNAL_SYSCALL (lstat, err, 2, CHECK_STRING (file),
00107                                CHECK_1 ((struct kernel_stat *) st));
00108       else
00109        result = INTERNAL_SYSCALL (stat, err, 2, CHECK_STRING (file),
00110                                CHECK_1 ((struct kernel_stat *) st));
00111 
00112       if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
00113        return result;
00114     }
00115 #ifdef STAT_IS_KERNEL_STAT
00116   else
00117     {
00118       __set_errno (EINVAL);
00119       return -1;
00120     }
00121 #else
00122   if (flag & AT_SYMLINK_NOFOLLOW)
00123     result = INTERNAL_SYSCALL (lstat, err, 2, CHECK_STRING (file),
00124                             __ptrvalue (&kst));
00125   else
00126     result = INTERNAL_SYSCALL (stat, err, 2, CHECK_STRING (file),
00127                             __ptrvalue (&kst));
00128 
00129   if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
00130     return __xstat_conv (vers, &kst, st);
00131 #endif
00132 
00133   __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), fd, buf);
00134 
00135   return -1;
00136 }
00137 libc_hidden_def (__fxstatat)
00138 #ifdef XSTAT_IS_XSTAT64
00139 # undef __fxstatat64
00140 strong_alias (__fxstatat, __fxstatat64);
00141 libc_hidden_def (__fxstatat64)
00142 #endif