1OPEN(2) Linux Programmer's Manual OPEN(2)
2
3
4
6 open, openat, creat - open and possibly create a file
7
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12
13 int open(const char *pathname, int flags);
14 int open(const char *pathname, int flags, mode_t mode);
15
16 int creat(const char *pathname, mode_t mode);
17
18 int openat(int dirfd, const char *pathname, int flags);
19 int openat(int dirfd, const char *pathname, int flags, mode_t mode);
20
21 /* Documented separately, in openat2(2): */
22 int openat2(int dirfd, const char *pathname,
23 const struct open_how *how, size_t size);
24
25 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
26
27 openat():
28 Since glibc 2.10:
29 _POSIX_C_SOURCE >= 200809L
30 Before glibc 2.10:
31 _ATFILE_SOURCE
32
34 The open() system call opens the file specified by pathname. If the
35 specified file does not exist, it may optionally (if O_CREAT is speci‐
36 fied in flags) be created by open().
37
38 The return value of open() is a file descriptor, a small, nonnegative
39 integer that is used in subsequent system calls (read(2), write(2),
40 lseek(2), fcntl(2), etc.) to refer to the open file. The file descrip‐
41 tor returned by a successful call will be the lowest-numbered file
42 descriptor not currently open for the process.
43
44 By default, the new file descriptor is set to remain open across an
45 execve(2) (i.e., the FD_CLOEXEC file descriptor flag described in
46 fcntl(2) is initially disabled); the O_CLOEXEC flag, described below,
47 can be used to change this default. The file offset is set to the
48 beginning of the file (see lseek(2)).
49
50 A call to open() creates a new open file description, an entry in the
51 system-wide table of open files. The open file description records the
52 file offset and the file status flags (see below). A file descriptor
53 is a reference to an open file description; this reference is unaf‐
54 fected if pathname is subsequently removed or modified to refer to a
55 different file. For further details on open file descriptions, see
56 NOTES.
57
58 The argument flags must include one of the following access modes:
59 O_RDONLY, O_WRONLY, or O_RDWR. These request opening the file read-
60 only, write-only, or read/write, respectively.
61
62 In addition, zero or more file creation flags and file status flags can
63 be bitwise-or'd in flags. The file creation flags are O_CLOEXEC,
64 O_CREAT, O_DIRECTORY, O_EXCL, O_NOCTTY, O_NOFOLLOW, O_TMPFILE, and