Back to index

glibc  2.9
fxstatat.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 /* Ho hum, since fxstatat == fxstatat64 we must get rid of the
00020    prototype or gcc 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 <string.h>
00028 #include <sys/stat.h>
00029 
00030 #include <sysdep.h>
00031 #include <kernel-features.h>
00032 #include <sys/syscall.h>
00033 #include <bp-checks.h>
00034 
00035 
00036 /* Get information about the file NAME relative to FD in ST.  */
00037 int
00038 __fxstatat (int vers, int fd, const char *file, struct stat *st, int flag)
00039 {
00040   if (vers != _STAT_VER_KERNEL && vers != _STAT_VER_LINUX)
00041     {
00042       __set_errno (EINVAL);
00043       return -1;
00044     }
00045 
00046   int res;
00047 
00048 #ifdef __NR_newfstatat
00049 # ifndef __ASSUME_ATFCTS
00050   if (__have_atfcts >= 0)
00051 # endif
00052     {
00053       res = INLINE_SYSCALL (newfstatat, 4, fd, file, st, flag);
00054 # ifndef __ASSUME_ATFCTS
00055       if (res == -1 && errno == ENOSYS)
00056        __have_atfcts = -1;
00057       else
00058 # endif
00059        return res;
00060     }
00061 #endif
00062 
00063 #ifndef __ASSUME_ATFCTS
00064   if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
00065     {
00066       __set_errno (EINVAL);
00067       return -1;
00068     }
00069 
00070   char *buf = NULL;
00071 
00072   if (fd != AT_FDCWD && file[0] != '/')
00073     {
00074       size_t filelen = strlen (file);
00075       static const char procfd[] = "/proc/self/fd/%d/%s";
00076       /* Buffer for the path name we are going to use.  It consists of
00077         - the string /proc/self/fd/
00078         - the file descriptor number
00079         - the file name provided.
00080         The final NUL is included in the sizeof.   A bit of overhead
00081         due to the format elements compensates for possible negative
00082         numbers.  */
00083       size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
00084       buf = alloca (buflen);
00085 
00086       __snprintf (buf, buflen, procfd, fd, file);
00087       file = buf;
00088     }
00089 
00090   INTERNAL_SYSCALL_DECL (err);
00091 
00092   if (flag & AT_SYMLINK_NOFOLLOW)
00093     res = INTERNAL_SYSCALL (lstat, err, 2, file, CHECK_1 (st));
00094   else
00095     res = INTERNAL_SYSCALL (stat, err, 2, file, CHECK_1 (st));
00096 
00097   if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (res, err), 0))
00098     {
00099       __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (res, err), fd, buf);
00100       res = -1;
00101     }
00102 
00103   return res;
00104 #endif
00105 }
00106 libc_hidden_def (__fxstatat)
00107 #undef __fxstatat64
00108 strong_alias (__fxstatat, __fxstatat64);
00109 strong_alias (__fxstatat64, __GI___fxstatat64)