Back to index

glibc  2.9
symlinkat.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 /* Make a symbolic link to FROM named TO relative to TOFD.  */
00031 int
00032 symlinkat (from, tofd, to)
00033      const char *from;
00034      int tofd;
00035      const char *to;
00036 {
00037   int result;
00038 
00039 #ifdef __NR_symlinkat
00040 # ifndef __ASSUME_ATFCTS
00041   if (__have_atfcts >= 0)
00042 # endif
00043     {
00044       result = INLINE_SYSCALL (symlinkat, 3, from, tofd, to);
00045 # ifndef __ASSUME_ATFCTS
00046       if (result == -1 && errno == ENOSYS)
00047        __have_atfcts = -1;
00048       else
00049 # endif
00050        return result;
00051     }
00052 #endif
00053 
00054 #ifndef __ASSUME_ATFCTS
00055   char *buf = NULL;
00056 
00057   if (tofd != AT_FDCWD && to[0] != '/')
00058     {
00059       size_t tolen = strlen (to);
00060       static const char procfd[] = "/proc/self/fd/%d/%s";
00061       /* Buffer for the path name we are going to use.  It consists of
00062         - the string /proc/self/fd/
00063         - the file descriptor number
00064         - the file name provided.
00065         The final NUL is included in the sizeof.   A bit of overhead
00066         due to the format elements compensates for possible negative
00067         numbers.  */
00068       size_t buflen = sizeof (procfd) + sizeof (int) * 3 + tolen;
00069       buf = __alloca (buflen);
00070 
00071       __snprintf (buf, buflen, procfd, tofd, to);
00072       to = buf;
00073     }
00074 
00075   INTERNAL_SYSCALL_DECL (err);
00076 
00077   result = INTERNAL_SYSCALL (symlink, err, 2, from, to);
00078 
00079   if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 0))
00080     {
00081       __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), tofd, buf);
00082       result = -1;
00083     }
00084 
00085   return result;
00086 #endif
00087 }