Back to index

glibc  2.9
mkdirat.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 <string.h>
00024 #include <sys/stat.h>
00025 #include <kernel-features.h>
00026 #include <sysdep-cancel.h>
00027 
00028 
00029 /* Create a new directory with permission bits MODE.  But interpret
00030    relative PATH names relative to the directory associated with FD.  */
00031 int
00032 mkdirat (fd, file, mode)
00033      int fd;
00034      const char *file;
00035      mode_t mode;
00036 {
00037   int res;
00038 
00039 #ifdef __NR_mkdirat
00040 #  ifndef __ASSUME_ATFCTS
00041   if (__have_atfcts >= 0)
00042 # endif
00043     {
00044       res = INLINE_SYSCALL (mkdirat, 3, fd, file, mode);
00045 # ifndef __ASSUME_ATFCTS
00046       if (res == -1 && res == ENOSYS)
00047        __have_atfcts = -1;
00048       else
00049 # endif
00050        return res;
00051     }
00052 #endif
00053 
00054 #ifndef __ASSUME_ATFCTS
00055   char *buf = NULL;
00056 
00057   if (fd != AT_FDCWD && file[0] != '/')
00058     {
00059       size_t filelen = strlen (file);
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 + filelen;
00069       buf = alloca (buflen);
00070 
00071       __snprintf (buf, buflen, procfd, fd, file);
00072       file = buf;
00073     }
00074 
00075   INTERNAL_SYSCALL_DECL (err);
00076   res = INTERNAL_SYSCALL (mkdir, err, 2, file, mode);
00077 
00078   if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (res, err), 0))
00079     {
00080       __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (res, err), fd, buf);
00081       res = -1;
00082     }
00083 
00084   return res;
00085 #endif
00086 }