1OPEN(2)                    Linux Programmer's Manual                   OPEN(2)
2
3
4

NAME

6       open, openat, creat - open and possibly create a file
7

SYNOPSIS

9       #include <fcntl.h>
10
11       int open(const char *pathname, int flags);
12       int open(const char *pathname, int flags, mode_t mode);
13
14       int creat(const char *pathname, mode_t mode);
15
16       int openat(int dirfd, const char *pathname, int flags);
17       int openat(int dirfd, const char *pathname, int flags, mode_t mode);
18
19       /* Documented separately, in openat2(2): */
20       int openat2(int dirfd, const char *pathname,
21                   const struct open_how *how, size_t size);
22
23   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
24
25       openat():
26           Since glibc 2.10:
27               _POSIX_C_SOURCE >= 200809L
28           Before glibc 2.10:
29               _ATFILE_SOURCE
30

DESCRIPTION

32       The  open()  system  call opens the file specified by pathname.  If the
33       specified file does not exist, it may optionally (if O_CREAT is  speci‐
34       fied in flags) be created by open().
35
36       The  return  value of open() is a file descriptor, a small, nonnegative
37       integer that is an index to an entry in the  process's  table  of  open
38       file  descriptors.   The  file  descriptor is used in subsequent system
39       calls (read(2), write(2), lseek(2), fcntl(2), etc.)  to  refer  to  the
40       open  file.   The file descriptor returned by a successful call will be
41       the lowest-numbered file descriptor not currently open for the process.
42
43       By default, the new file descriptor is set to remain open across an ex‐
44       ecve(2)  (i.e.,  the  FD_CLOEXEC  file descriptor flag described in fc‐
45       ntl(2) is initially disabled); the O_CLOEXEC flag, described below, can
46       be  used  to change this default.  The file offset is set to the begin‐
47       ning of the file (see lseek(2)).
48
49       A call to open() creates a new open file description, an entry  in  the
50       system-wide table of open files.  The open file description records the
51       file offset and the file status flags (see below).  A  file  descriptor
52       is  a  reference  to  an open file description; this reference is unaf‐
53       fected if pathname is subsequently removed or modified to  refer  to  a
54       different  file.   For  further  details on open file descriptions, see
55       NOTES.
56
57       The argument flags must include one  of  the  following  access  modes:
58       O_RDONLY,  O_WRONLY,  or  O_RDWR.  These request opening the file read-
59       only, write-only, or read/write, respectively.
60
61       In addition, zero or more file creation flags and file status flags can
62       be  bitwise-or'd  in  flags.   The  file  creation flags are O_CLOEXEC,
63       O_CREAT, O_DIRECTORY,  O_EXCL,  O_NOCTTY,  O_NOFOLLOW,  O_TMPFILE,  an