1OPEN(2) System Calls Manual OPEN(2)
2
3
4
6 open - open a file for reading or writing, or create a new file
7
9 #include <fcntl.h>
10
11 open(path, flags, mode)
12 char *path;
13 int flags, mode;
14
16 Open opens the file path for reading and/or writing, as specified by
17 the flags argument and returns a descriptor for that file. The flags
18 argument may indicate the file is to be created if it does not already
19 exist (by specifying the O_CREAT flag), in which case the file is cre‐
20 ated with mode mode as described in chmod(2) and modified by the
21 process' umask value (see umask(2)).
22
23 Path is the address of a string of ASCII characters representing a path
24 name, terminated by a null character. The flags specified are formed
25 by or'ing the following values
26
27 O_RDONLY open for reading only
28 O_WRONLY open for writing only
29 O_RDWR open for reading and writing
30 O_NONBLOCK do not block on open
31 O_APPEND append on each write
32 O_CREAT create file if it does not exist
33 O_TRUNC truncate size to 0
34 O_EXCL error if create and file exists
35 O_NOCTTY do not acquire as controlling terminal
36 O_SHLOCK atomically obtain a shared lock
37 O_EXLOCK atomically obtain an exclusive lock
38
39 Opening a file with O_APPEND set causes each write on the file to be
40 appended to the end. If O_TRUNC is specified and the file exists, the
41 file is truncated to zero length. If O_EXCL is set with O_CREAT, then
42 if the file already exists, the open returns an error. This can be
43 used to implement a simple exclusive access locking mechanism. If
44 O_EXCL is set and the last component of the pathname is a symbolic
45 link, the open will fail even if the symbolic link points to a non-
46 existent name. If the O_NONBLOCK flag is specified and the open call
47 would result in the process being blocked for some reason (e.g. waiting
48 for carrier on a dialup line), the open returns immediately. The first
49 time the process attempts to perform i/o on the open file it will
50 block.
51
52 The flag O_NOCTTY indicates that even if the file is a terminal device,
53 the call should not result in acquiring the terminal device as the con‐
54 trolling terminal of the caller. This flag is not the default and is
55 currently unimplemented (it will be Real Soon Now).
56
57 When opening a file, a lock with flock(2) semantics can be obtained by
58 setting O_SHLOCK for a shared lock, or O_EXLOCK for an exclusive lock.
59 If creating a file with O_CREAT, the request for the lock will never
60 fail.
61
62 Upon successful completion a non-negative integer termed a file
63 descriptor is returned. The file pointer used to mark the current
64 position within the file is set to the beginning of the file.
65
66 The new descriptor is set to remain open across execve system calls;
67 see close(2).
68
69 The system imposes a limit on the number of file descriptors open
70 simultaneously by one process. Getdtablesize(2) returns the current
71 system limit.
72
74 The named file is opened unless one or more of the following are true:
75
76 [ENOTDIR] A component of the path prefix is not a directory.
77
78 [EINVAL] The pathname contains a character with the high-order
79 bit set.
80
81 [ENAMETOOLONG] A component of a pathname exceeded 255 characters, or an
82 entire path name exceeded 1023 characters.
83
84 [ENOENT] O_CREAT is not set and the named file does not exist.
85
86 [ENOENT] A component of the path name that must exist does not
87 exist.
88
89 [EACCES] Search permission is denied for a component of the path
90 prefix.
91
92 [EACCES] The required permissions (for reading and/or writing)
93 are denied for the named flag.
94
95 [EACCES] O_CREAT is specified, the file does not exist, and the
96 directory in which it is to be created does not permit
97 writing.
98
99 [ELOOP] Too many symbolic links were encountered in translating
100 the pathname.
101
102 [EISDIR] The named file is a directory, and the arguments specify
103 it is to be opened for writting.
104
105 [EROFS] The named file resides on a read-only file system, and
106 the file is to be modified.
107
108 [EMFILE] The system limit for open file descriptors per process
109 has already been reached.
110
111 [ENFILE] The system file table is full.
112
113 [ENXIO] The named file is a character special or block special
114 file, and the device associated with this special file
115 does not exist.
116
117 [ENOSPC] O_CREAT is specified, the file does not exist, and the
118 directory in which the entry for the new file is being
119 placed cannot be extended because there is no space left
120 on the file system containing the directory.
121
122 [ENOSPC] O_CREAT is specified, the file does not exist, and there
123 are no free inodes on the file system on which the file
124 is being created.
125
126 [EDQUOT] O_CREAT is specified, the file does not exist, and the
127 directory in which the entry for the new fie is being
128 placed cannot be extended because the user's quota of
129 disk blocks on the file system containing the directory
130 has been exhausted.
131
132 [EDQUOT] O_CREAT is specified, the file does not exist, and the
133 user's quota of inodes on the file system on which the
134 file is being created has been exhausted.
135
136 [EIO] An I/O error occurred while making the directory entry
137 or allocating the inode for O_CREAT.
138
139 [ETXTBSY] The file is a pure procedure (shared text) file that is
140 being executed and the open call requests write access.
141
142 [EFAULT] Path points outside the process's allocated address
143 space.
144
145 [EEXIST] O_CREAT and O_EXCL were specified and the file exists.
146
147 [EOPNOTSUPP] An attempt was made to open a socket (not currently
148 implemented).
149
151 chmod(2), close(2), dup(2), getdtablesize(2), lseek(2), read(2),
152 write(2), umask(2)
153
154
155
1564th Berkeley Distribution Nov 30, 1994 OPEN(2)