Back to index

glibc  2.9
unlinkat.c
Go to the documentation of this file.
00001 /* unlinkat -- Remove a link by relative name.
00002    Copyright (C) 2005, 2006 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <fcntl.h>
00022 #include <stddef.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <sysdep.h>
00027 #include <unistd.h>
00028 #include <kernel-features.h>
00029 
00030 
00031 /* Remove the link named NAME.  */
00032 int
00033 unlinkat (fd, file, flag)
00034      int fd;
00035      const char *file;
00036      int flag;
00037 {
00038   int result;
00039 
00040 #ifdef __NR_unlinkat
00041 # ifndef __ASSUME_ATFCTS
00042   if (__have_atfcts >= 0)
00043 # endif
00044     {
00045       result = INLINE_SYSCALL (unlinkat, 3, fd, file, flag);
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   if (flag & ~AT_REMOVEDIR)
00057     {
00058       __set_errno (EINVAL);
00059       return -1;
00060     }
00061 
00062   char *buf = NULL;
00063 
00064   if (fd != AT_FDCWD && file[0] != '/')
00065     {
00066       size_t filelen = strlen (file);
00067       static const char procfd[] = "/proc/self/fd/%d/%s";
00068       /* Buffer for the path name we are going to use.  It consists of
00069         - the string /proc/self/fd/
00070         - the file descriptor number
00071         - the file name provided.
00072         The final NUL is included in the sizeof.   A bit of overhead
00073         due to the format elements compensates for possible negative
00074         numbers.  */
00075       size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
00076       buf = __alloca (buflen);
00077 
00078       __snprintf (buf, buflen, procfd, fd, file);
00079       file = buf;
00080     }
00081 
00082   INTERNAL_SYSCALL_DECL (err);
00083 
00084   if (flag & AT_REMOVEDIR)
00085     result = INTERNAL_SYSCALL (rmdir, err, 1, file);
00086   else
00087     result = INTERNAL_SYSCALL (unlink, err, 1, file);
00088 
00089   if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 0))
00090     {
00091       __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), fd, buf);
00092       result = -1;
00093     }
00094 
00095   return result;
00096 #endif
00097 }