Back to index

glibc  2.9
linkat.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 <string.h>
00022 #include <stdio.h>
00023 #include <sysdep.h>
00024 #include <unistd.h>
00025 #include <kernel-features.h>
00026 
00027 
00028 /* Make a link to FROM named TO but relative paths in TO and FROM are
00029    interpreted relative to FROMFD and TOFD respectively.  */
00030 int
00031 linkat (fromfd, from, tofd, to, flags)
00032      int fromfd;
00033      const char *from;
00034      int tofd;
00035      const char *to;
00036      int flags;
00037 {
00038   int result;
00039 
00040 #ifdef __NR_linkat
00041 # ifndef __ASSUME_ATFCTS
00042   if (__have_atfcts >= 0)
00043 # endif
00044     {
00045       result = INLINE_SYSCALL (linkat, 5, fromfd, from, tofd, to, flags);
00046 # ifndef __ASSUME_ATFCTS
00047       if (result == -1 && errno == ENOSYS)
00048        __have_atfcts = -1;
00049       else
00050 # endif
00051        return result;
00052     }
00053 #endif
00054 
00055 #ifndef __ASSUME_ATFCTS
00056   /* Without kernel support we cannot handle AT_SYMLINK_FOLLOW.  */
00057   if (flags != 0)
00058     {
00059       __set_errno (EINVAL);
00060       return -1;
00061     }
00062 
00063   static const char procfd[] = "/proc/self/fd/%d/%s";
00064   char *buffrom = NULL;
00065 
00066   if (fromfd != AT_FDCWD && from[0] != '/')
00067     {
00068       size_t filelen = strlen (from);
00069       /* Buffer for the path name we are going to use.  It consists of
00070         - the string /proc/self/fd/
00071         - the file descriptor number
00072         - the file name provided.
00073         The final NUL is included in the sizeof.   A bit of overhead
00074         due to the format elements compensates for possible negative
00075         numbers.  */
00076       size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
00077       buffrom = alloca (buflen);
00078 
00079       __snprintf (buffrom, buflen, procfd, fromfd, from);
00080       from = buffrom;
00081     }
00082 
00083   char *bufto = NULL;
00084 
00085   if (tofd != AT_FDCWD && to[0] != '/')
00086     {
00087       size_t filelen = strlen (to);
00088       /* Buffer for the path name we are going to use.  It consists of
00089         - the string /proc/self/fd/
00090         - the file descriptor number
00091         - the file name provided.
00092         The final NUL is included in the sizeof.   A bit of overhead
00093         due to the format elements compensates for possible negative
00094         numbers.  */
00095       size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
00096       bufto = alloca (buflen);
00097 
00098       __snprintf (bufto, buflen, procfd, tofd, to);
00099       to = bufto;
00100     }
00101 
00102   INTERNAL_SYSCALL_DECL (err);
00103 
00104   result = INTERNAL_SYSCALL (link, err, 2, from,  to);
00105 
00106   if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 0))
00107     {
00108       __atfct_seterrno_2 (INTERNAL_SYSCALL_ERRNO (result, err), tofd, bufto,
00109                        fromfd, buffrom);
00110       result = -1;
00111     }
00112 
00113   return result;
00114 #endif
00115 }