- timeval[0].tv_sec = timespec[0].tv_sec;
- timeval[0].tv_usec = timespec[0].tv_nsec / 1000;
- timeval[1].tv_sec = timespec[1].tv_sec;
- timeval[1].tv_usec = timespec[1].tv_nsec / 1000;
- return utimes (file, timeval);
+ struct timeval const *t;
+ if (timespec)
+ {
+ timeval[0].tv_sec = timespec[0].tv_sec;
+ timeval[0].tv_usec = timespec[0].tv_nsec / 1000;
+ timeval[1].tv_sec = timespec[1].tv_sec;
+ timeval[1].tv_usec = timespec[1].tv_nsec / 1000;
+ t = timeval;
+ }
+ else
+ t = NULL;
+# if HAVE_FUTIMES
+ if (0 <= fd)
+ {
+ if (futimes (fd, t) == 0)
+ return 0;
+
+ /* On GNU/Linux without the futimes syscall and without /proc
+ mounted, glibc futimes fails with errno == ENOENT. Fall back
+ on utimes if we get a weird error number like that. */
+ switch (errno)
+ {
+ case EACCES:
+ case EIO:
+ case EPERM:
+ case EROFS:
+ return -1;
+ }
+ }
+# endif
+ return utimes (file, t);
+