Back to index

glibc  2.9
readlinkat.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 <stdlib.h>
00024 #include <string.h>
00025 #include <sysdep.h>
00026 #include <unistd.h>
00027 #include <kernel-features.h>
00028 
00029 
00030 /* Read the contents of the symbolic link PATH relative to FD into no
00031    more than LEN bytes of BUF.  */
00032 ssize_t
00033 readlinkat (fd, path, buf, len)
00034      int fd;
00035      const char *path;
00036      char *buf;
00037      size_t len;
00038 {
00039   int result;
00040 
00041 #ifdef __NR_readlinkat
00042 # ifndef __ASSUME_ATFCTS
00043   if (__have_atfcts >= 0)
00044 # endif
00045     {
00046       result = INLINE_SYSCALL (readlinkat, 4, fd, path, buf, len);
00047 # ifndef __ASSUME_ATFCTS
00048       if (result == -1 && errno == ENOSYS)
00049        __have_atfcts = -1;
00050       else
00051 # endif
00052        return result;
00053     }
00054 #endif
00055 
00056 #ifndef __ASSUME_ATFCTS
00057   char *pathbuf = NULL;
00058 
00059   if (fd != AT_FDCWD && path[0] != '/')
00060     {
00061       size_t pathlen = strlen (path);
00062       static const char procfd[] = "/proc/self/fd/%d/%s";
00063       /* Buffer for the path name we are going to use.  It consists of
00064         - the string /proc/self/fd/
00065         - the file descriptor number
00066         - the file name provided.
00067         The final NUL is included in the sizeof.   A bit of overhead
00068         due to the format elements compensates for possible negative
00069         numbers.  */
00070       size_t buflen = sizeof (procfd) + sizeof (int) * 3 + pathlen;
00071       pathbuf = __alloca (buflen);
00072 
00073       __snprintf (pathbuf, buflen, procfd, fd, path);
00074       path = pathbuf;
00075     }
00076 
00077   INTERNAL_SYSCALL_DECL (err);
00078 
00079   result = INTERNAL_SYSCALL (readlink, err, 3, path, buf, len);
00080 
00081   if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 0))
00082     {
00083       __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), fd, pathbuf);
00084       result = -1;
00085     }
00086 
00087   return result;
00088 #endif
00089 }
00090 libc_hidden_def (readlinkat)