内容简介
#include <fcntl.h>
int openat(int dirfd, const char *pathname, int flags)
int openat(int dirfd, const char *pathname, int flags ", mode_t " mode )
The%uA0openat() system call operates in exactly the same way as%uA0open(2), except for the differences described in this manual page.描述
If the pathname given in%uA0pathname%uA0is relative, then it is interpreted relative to the directory referred to by the file descriptor%uA0dirfd%uA0(rather than relative to the current working directory of the calling process, as is done by%uA0open(2) for a relative pathname).
If the pathname given in%uA0pathname%uA0is relative and%uA0dirfd%uA0is the special value%uA0AT_FDCWD, then%uA0pathname%uA0is interpreted relative to the current working directory of the calling process (like%uA0open(2)).
If the pathname given in%uA0pathname%uA0is absolute, then%uA0dirfd%uA0is ignored.
返回值
On success,%uA0openat() returns a new file descriptor. On error, -1 is returned and%uA0errno%uA0is set to indicate the error.错误
The same errors that occur for%uA0open(2) can also occur for%uA0openat(). The following additional errors can occur for%uA0openat():标签 | 描述 |
---|---|
EBADF | dirfd%uA0is not a valid file descriptor. |
ENOTDIR | |
%uA0 | pathname%uA0is a relative path and%uA0dirfd%uA0is a file descriptor referring to a file other than a directory. |
注意
openat() and other similar system calls suffixed "at" are supported for two reasons.First,%uA0openat() allows an application to avoid race conditions that could occur when using%uA0open(2) to open files in directories other than the current working directory. These race conditions result from the fact that some component of the directory prefix given to%uA0open() could be changed in parallel with the call to%uA0open(). Such races can be avoided by opening a file descriptor for the target directory, and then specifying that file descriptor as the%uA0dirfd%uA0argument of%uA0openat().
Second,%uA0openat() allows the implementation of a per-thread "current working directory", via file descriptor(s) maintained by the application. (This functionality can also be obtained by tricks based on the use of%uA0/proc/self/fd/dirfd, but less efficiently.)