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 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
22
23 openat():
24 Since glibc 2.10:
25 _POSIX_C_SOURCE >= 200809L
26 Before glibc 2.10:
27 _ATFILE_SOURCE
28
30 The open() system call opens the file specified by pathname. If the
31 specified file does not exist, it may optionally (if O_CREAT is speci‐
32 fied in flags) be created by open().
33
34 The return value of open() is a file descriptor, a small, nonnegative
35 integer that is used in subsequent system calls (read(2), write(2),
36 lseek(2), fcntl(2), etc.) to refer to the open file. The file descrip‐
37 tor returned by a successful call will be the lowest-numbered file
38 descriptor not currently open for the process.
39
40 By default, the new file descriptor is set to remain open across an
41 execve(2) (i.e., the FD_CLOEXEC file descriptor flag described in
42 fcntl(2) is initially disabled); the O_CLOEXEC flag, described below,
43 can be used to change this default. The file offset is set to the
44 beginning of the file (see lseek(2)).
45
46 A call to open() creates a new open file description, an entry in the
47 system-wide table of open files. The open file description records the
48 file offset and the file status flags (see below). A file descriptor
49 is a reference to an open file description; this reference is unaf‐
50 fected if pathname is subsequently removed or modified to refer to a
51 different file. For further details on open file descriptions, see
52 NOTES.
53
54 The argument flags must include one of the following access modes:
55 O_RDONLY, O_WRONLY, or O_RDWR. These request opening the file read-
56 only, write-only, or read/write, respectively.
57
58 In addition, zero or more file creation flags and file status flags can
59 be bitwise-or'd in flags. The file creation flags are O_CLOEXEC,
60 O_CREAT, O_DIRECTORY, O_EXCL, O_NOCTTY, O_NOFOLLOW, O_TMPFILE, and
61 O_TRUNC. The file status flags are all of the remaining flags listed
62 below. The distinction between these two groups of flags is that the
63 file creation flags affect the seman