UTIMES(2)

NAME
utimes, futimes, utimensat, futimens - set file access and modification times

SYNOPSIS
#include <sys/time.h>
int
utimes(const char *file, const struct timeval *times);
int
futimes(int fd, const struct timeval *times);
#include <sys/stat.h>
#include <fcntl.h>
int
utimensat(int fd, const char *file, const struct timespec times[2], int flag);
int
futimens(int fd, const struct timespec times[2]);

DESCRIPTION
The access and modification times of the file named by path or referenced by fd are changed as specified by the argument times.

If times is NULL, the access and modification times are set to the current time. The caller must be the owner of the file, have permission to write the file, or be the superuser.

If times is non-null, it is assumed to point to an array of two timeval structures. The access time is set to the value of the first element, and the modification time is set to the value of the second element. The caller must be the owner of the file or be the superuser.

In either case, the inode-change-time of the file is set to the current time.

The utimensat() and futimens() are equivalent to utimes() and futimes(), respectively, with the following differences.

Both utimensat() and futimens() take two timespec values instead of two timeval values. Further, either of the tv_nsec fields can be set to one of the following special values defined in <sys/stat.h>:

UTIME_NOW
    Set the respective timestamp to the greatest value supported that is not greater than the current time.
UTIME_OMIT
    Do not change the respective timestamp.

Additionally, if the path argument to utimensat() specifies a relative path, the file whose timestamps are changed is determined relative to the directory associated with file descriptor fd instead of the current working directory.

If utimensat() is passed the special value AT_FDCWD (defined in <fcntl.h>) in the fd parameter, the current working directory is used.

Values for flag are constructed by bitwise-inclusive ORing flags from the following list defined in <fcntl.h>:

AT_SYMLINK_NOFOLLOW
    If path names a symbolic link, then the timestamps of the symbolic link are changed.

RETURN VALUES
Upon successful completion, a value of 0 is returned. Otherwise, a value of -1 is returned and errno is set to indicate the error.

ERRORS
utimes() and utimensat() will fail if:

[EACCES]
    Search permission is denied for a component of the path prefix; or the times argument is NULL and the effective user ID of the process does not match the owner of the file, and is not the superuser, and write access is denied.

[EFAULT]
    File or times points outside the process's allocated address space.

[EIO]
    An I/O error occurred while reading or writing the affected inode.

[ELOOP]
    Too many symbolic links were encountered in translating the pathname.

[ENAMETOOLONG]
    A component of a pathname exceeded {NAME_MAX} characters, or an entire path name exceeded {PATH_MAX} characters.

[ENOENT]
    The named file does not exist.

[ENOTDIR]
    A component of the path prefix is not a directory.

[EPERM]
    The times argument is not NULL and the calling process's effective user ID does not match the owner of the file and is not the superuser.

[EROFS]
    The file system containing the file is mounted read-only.

Additionally, utimensat() will fail if:

[EBADF]
    The path argument does not specify an absolute path and the fd argument is neither AT_FDCWD nor a valid file descriptor open for reading.

futimes() and futimens() will fail if:

[EBADF]
    fd does not refer to a valid descriptor.

[EACCES]
    The times argument is NULL and the effective user ID of the process does not match the owner of the file, and is not the superuser, and write access is denied.

[EFAULT]
    times points outside the process's allocated address space.

[EIO]
    An I/O error occurred while reading or writing the affected inode.

[EPERM]
    The times argument is not NULL and the calling process's effective user ID does not match the owner of the file and is not the superuser.

[EROFS]
    The file system containing the file is mounted read-only.

SEE ALSO
stat(2), utime(3)

STANDARDS
The utimes(), utimensat(), and futimens() functions conform to p1003.1-2008.

HISTORY
The predecessors of utimes() were smdate() in AT&T UNIX, mdate() in AT&T UNIX, and utime() in AT&T UNIX; the latter first supported the concept of an access time in addition to the modification time.

The utimes() function call appeared in 4.2BSD. The futimes() function call appeared in NetBSD 1.2. The utimensat() and futimens() function calls appeared in OpenBSD 5.0.

CAVEATS
p1003.1-2008 specifies that utimensat() and futimens() shall mark the last file status change timestamp (i.e. st_ctim) for update upon successful completion. However, currently some filesystems (e.g. UFS) will not do so if UTIME_OMIT is specified for the modification timestamp argument.