---
name: "utimensat"
section: "2"
platform: "macos"
package: "xnu"
version: "12377.121.6"
release: "macOS 26.5"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/utimensat"
---

# utimensat(2)

set file access and modification times

## NAME

**futimens**, **utimensat** – set file access and modification times

## SYNOPSIS

**#include \<sys/stat.h\>**

*int*\

**futimens**(*int fd*, *const struct timespec times\[2\]*);

*int*\

**utimensat**(*int fd*, *const char \*path*, *const struct timespec times\[2\]*, *int flag*);

## DESCRIPTION

The access and modification times of the file named by *path* or referenced by *fd* are changed as specified by the argument *times*. The inode-change-time of the file is set to the current time.

If *path* specifies a relative path, it is relative to the current working directory if *fd* is AT\_FDCWD and otherwise relative to the directory associated with the file descriptor *fd*.

The *tv\_nsec* field of a *timespec* structure can be set to the special value UTIME\_NOW to set the current time, or to UTIME\_OMIT to leave the time unchanged. In either case, the *tv\_sec* field is ignored.

If *times* is non-NULL, it is assumed to point to an array of two timespec 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. If *times* is NULL, this is equivalent to passing a pointer to an array of two timespec structures with both *tv\_nsec* fields set to UTIME\_NOW.

If both *tv\_nsec* fields are UTIME\_OMIT, the timestamps remain unchanged and no permissions are needed for the file itself, although search permissions may be required for the path prefix. The call may or may not succeed if the named file does not exist.

If both *tv\_nsec* fields are UTIME\_NOW, the caller must be the owner of the file, have permission to write the file, or be the super-user.

For all other values of the timestamps, the caller must be the owner of the file or be the super-user.

The values for the *flag* argument of the **utimensat**() system call are constructed by a bitwise-inclusive OR of flags from the following list, defined in ⟨fcntl.h⟩:

- AT\_SYMLINK\_NOFOLLOW

  If *path* names a symbolic link, the symbolic link's times are changed. By default, **utimensat**() changes the times of the file referenced by the symbolic link.

- AT\_SYMLINK\_NOFOLLOW\_ANY

  If *path* names a symbolic link, the symbolic link's times are changed. In addition, if any symbolic link is enountered during pathname resolution, an error is returned. By default, **utimensat**() changes the times of the file referenced by the symbolic link.

- AT\_RESOLVE\_BENEATH

  If *path* does not reside in the hierarchy beneath the starting directory, an error is returned.

- AT\_UNIQUE

  If *path* has multiple hard links, an error is returned.

## RETURN VALUES

The **futimens()** function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

## ERRORS

These system calls will fail if:

- \[EACCES\]

  The *times* argument is NULL, or both *tv\_nsec* values are UTIME\_NOW, and the effective user ID of the process does not match the owner of the file, and is not the super-user, and write access is denied.

- \[EINVAL\]

  The *tv\_nsec* component of at least one of the values specified by the *times* argument has a value less than 0 or greater than 999999999 and is not equal to UTIME\_NOW or UTIME\_OMIT.

- \[EIO\]

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

- \[EPERM\]

  The *times* argument is not NULL nor are both *tv\_nsec* values UTIME\_NOW, nor are both *tv\_nsec* values UTIME\_OMIT and the calling process's effective user ID does not match the owner of the file and is not the super-user.

- \[EPERM\]

  The named file has its immutable or append-only flag set, see the [chflags(2)](/chflags/2.md) manual page for more information.

- \[EROFS\]

  The file system containing the file is mounted read-only.

The **futimens**() system call will fail if:

- \[EBADF\]

  The *fd* argument does not refer to a valid descriptor.

The **utimensat**() system call will fail if:

- \[EACCES\]

  Search permission is denied for a component of the path prefix.

- \[EBADF\]

  The *path* argument does not specify an absolute path and the *fd* argument is neither AT\_FDCWD nor a valid file descriptor.

- \[EFAULT\]

  The *path* argument points outside the process's allocated address space.

- \[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.

- \[ENOTDIR\]

  The *path* argument is not an absolute path and *fd* is neither AT\_FDCWD nor a file descriptor associated with a directory.

- \[ENOTCAPABLE\]

  if AT\_RESOLVE\_BENEATH is passed and *path* does not reside in the directory hierarchy beneath the starting directory.

- \[ENOTCAPABLE\]

  if AT\_UNIQUE is passed and *path* has multiple hard links.

## SEE ALSO

[chflags(2)](/chflags/2.md), [stat(2)](/stat/2.md), [symlink(2)](/symlink/2.md), [utimes(2)](/utimes/2.md), [utime(3)](/utime/3.md), [symlink(7)](/symlink/7.md)

## STANDARDS

The **futimens**() and **utimensat**() system calls are expected to conform to IEEE Std 1003.1-2008 (“POSIX.1”).
