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