Back to index

glibc  2.9
futimesat.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 <utime.h>
00025 #include <sys/time.h>
00026 #include <sysdep.h>
00027 #include <kernel-features.h>
00028 
00029 
00030 /* Change the access time of FILE relative to FD to TVP[0] and
00031    the modification time of FILE to TVP[1].  */
00032 int
00033 futimesat (fd, file, tvp)
00034      int fd;
00035      const char *file;
00036      const struct timeval tvp[2];
00037 {
00038   int result;
00039 
00040 #ifdef __NR_futimesat
00041 # ifndef __ASSUME_ATFCTS
00042   if (__have_atfcts >= 0)
00043 # endif
00044     {
00045       if (file == NULL)
00046        return __futimes (fd, tvp);
00047 
00048       result = INLINE_SYSCALL (futimesat, 3, fd, file, tvp);
00049 # ifndef __ASSUME_ATFCTS
00050       if (result == -1 && errno == ENOSYS)
00051        __have_atfcts = -1;
00052       else
00053 # endif
00054        return result;
00055     }
00056 #endif
00057 
00058 #ifndef __ASSUME_ATFCTS
00059   char *buf = NULL;
00060 
00061   if (file == NULL)
00062     {
00063       static const char procfd[] = "/proc/self/fd/%d";
00064       /* Buffer for the path name we are going to use.  It consists of
00065         - the string /proc/self/fd/
00066         - the file descriptor number.
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;
00071       buf = alloca (buflen);
00072 
00073       __snprintf (buf, buflen, procfd, fd);
00074       file = buf;
00075     }
00076   else if (fd != AT_FDCWD && file[0] != '/')
00077     {
00078       size_t filelen = strlen (file);
00079       static const char procfd[] = "/proc/self/fd/%d/%s";
00080       /* Buffer for the path name we are going to use.  It consists of
00081         - the string /proc/self/fd/
00082         - the file descriptor number
00083         - the file name provided.
00084         The final NUL is included in the sizeof.   A bit of overhead
00085         due to the format elements compensates for possible negative
00086         numbers.  */
00087       size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
00088       buf = alloca (buflen);
00089 
00090       __snprintf (buf, buflen, procfd, fd, file);
00091       file = buf;
00092     }
00093 
00094   INTERNAL_SYSCALL_DECL (err);
00095 
00096 # ifdef __NR_utimes
00097   result = INTERNAL_SYSCALL (utimes, err, 2, file, tvp);
00098   if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
00099     return result;
00100 
00101 #  ifndef __ASSUME_UTIMES
00102   if (INTERNAL_SYSCALL_ERRNO (result, err) != ENOSYS)
00103     goto fail;
00104 #  endif
00105 # endif
00106 
00107   /* The utimes() syscall does not exist or is not available in the
00108      used kernel.  Use utime().  For this we have to convert to the
00109      data format utime() expects.  */
00110 # ifndef __ASSUME_UTIMES
00111   struct utimbuf tmp;
00112   struct utimbuf *times;
00113 
00114   if (tvp != NULL)
00115     {
00116       times = &tmp;
00117       tmp.actime = tvp[0].tv_sec + tvp[0].tv_usec / 1000000;
00118       tmp.modtime = tvp[1].tv_sec + tvp[1].tv_usec / 1000000;
00119     }
00120   else
00121     times = NULL;
00122 
00123   result = INTERNAL_SYSCALL (utime, err, 2, file, times);
00124   if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
00125     return result;
00126 
00127  fail:
00128 # endif
00129 
00130   __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), fd, buf);
00131 
00132   return -1;
00133 #endif
00134 }