1 .\" Copyright (c) 1980, 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 4. Neither the name of the University nor the names of its contributors
13 .\" may be used to endorse or promote products derived from this software
14 .\" without specific prior written permission.
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" @(#)open.2 8.2 (Berkeley) 11/16/93
36 .Nd open or create a file for reading or writing
42 .Fn open "const char *path" "int flags" "..."
44 The file name specified by
47 for reading and/or writing as specified by the
50 and the file descriptor returned to the calling process.
53 argument may indicate the file is to be
54 created if it does not exist (by specifying the
59 requires a third argument
61 and the file is created with mode
65 and modified by the process' umask value (see
68 The flags specified are formed by
72 .Bd -literal -offset indent -compact
73 O_RDONLY open for reading only
74 O_WRONLY open for writing only
75 O_RDWR open for reading and writing
76 O_NONBLOCK do not block on open
77 O_APPEND append on each write
78 O_CREAT create file if it does not exist
79 O_TRUNC truncate size to 0
80 O_EXCL error if create and file exists
81 O_SHLOCK atomically obtain a shared lock
82 O_EXLOCK atomically obtain an exclusive lock
83 O_DIRECT eliminate or reduce cache effects
84 O_FSYNC synchronous writes
85 O_SYNC synchronous writes
86 O_NOFOLLOW do not follow symlinks
87 O_NOCTTY don't assign controlling terminal
92 set causes each write on the file
93 to be appended to the end.
97 file exists, the file is truncated to zero length.
107 implement a simple exclusive access locking mechanism.
110 is set and the last component of the pathname is
113 will fail even if the symbolic
114 link points to a non-existent name.
117 flag is specified and the
119 system call would result
120 in the process being blocked for some reason (e.g., waiting for
121 carrier on a dialup line),
124 The descriptor remains in non-blocking mode for subsequent operations.
128 is used in the mask, all writes will
129 immediately be written to disk,
130 the kernel will not cache written data
131 and all writes on the descriptor will not return until
132 the data to be written completes.
142 is used in the mask and the target file passed to
144 is a symbolic link then the
148 When opening a file, a lock with
150 semantics can be obtained by setting
152 for a shared lock, or
154 for an exclusive lock.
155 If creating a file with
157 the request for the lock will never fail
158 (provided that the underlying file system supports locking).
161 may be used to minimize or eliminate the cache effects of reading and writing.
162 The system will attempt to avoid caching the data you read or write.
163 If it cannot avoid caching the data,
164 it will minimize the impact the data has on the cache.
165 Use of this flag can drastically reduce performance if not used with care.
168 may be used to ensure the OS does not assign this file as the
169 controlling terminal when it opens a tty device.
170 This is the default on
177 system call will not assign controlling terminals on
182 returns a non-negative integer, termed a file descriptor.
183 It returns -1 on failure.
184 The file pointer used to mark the current position within the
185 file is set to the beginning of the file.
187 When a new file is created it is given the group of the directory
190 The new descriptor is set to remain open across
197 The system imposes a limit on the number of file descriptors
198 open simultaneously by one process.
201 system call returns the current system limit.
205 returns a non-negative integer, termed a file descriptor.
206 It returns -1 on failure, and sets
208 to indicate the error.
210 The named file is opened unless:
213 A component of the path prefix is not a directory.
214 .It Bq Er ENAMETOOLONG
215 A component of a pathname exceeded 255 characters,
216 or an entire path name exceeded 1023 characters.
219 is not set and the named file does not exist.
221 A component of the path name that must exist does not exist.
223 Search permission is denied for a component of the path prefix.
225 The required permissions (for reading and/or writing)
226 are denied for the given flags.
229 is specified and write permission is denied.
233 the file does not exist,
234 and the directory in which it is to be created
235 does not permit writing.
238 is specified, the file does not exist, and the directory in which it is to be
239 created has its immutable flag set, see the
241 manual page for more information.
243 .Dv The named file has its immutable flag set and the file is to be modified.
245 .Dv The named file has its append-only flag set, the file is to be modified, and
251 Too many symbolic links were encountered in translating the pathname.
253 The named file is a directory, and the arguments specify
254 it is to be modified.
256 The named file resides on a read-only file system,
257 and the file is to be modified.
260 is specified and the named file would reside on a read-only file system.
262 The process has already reached its limit for open file descriptors.
264 The system file table is full.
267 was specified and the target is a symbolic link.
269 The named file is a character special or block
270 special file, and the device associated with this special file
274 is set, the named file is a fifo,
276 is set, and no process has the file open for reading.
280 operation was interrupted by a signal.
285 is specified but the underlying file system does not support locking.
287 The named file is a special file mounted through a file system that
288 does not support access to it (e.g.\& NFS).
289 .It Bq Er EWOULDBLOCK
295 is specified and the file is locked.
299 the file does not exist,
300 and the directory in which the entry for the new file is being placed
301 cannot be extended because there is no space left on the file
302 system containing the directory.
306 the file does not exist,
307 and there are no free inodes on the file system on which the
308 file is being created.
312 the file does not exist,
313 and the directory in which the entry for the new file
314 is being placed cannot be extended because the
315 user's quota of disk blocks on the file system
316 containing the directory has been exhausted.
320 the file does not exist,
321 and the user's quota of inodes on the file system on
322 which the file is being created has been exhausted.
324 An I/O error occurred while making the directory entry or
325 allocating the inode for
328 The file is a pure procedure (shared text) file that is being
331 system call requests write access.
336 points outside the process's allocated address space.
341 were specified and the file exists.
343 An attempt was made to open a socket (not currently implemented).
345 An attempt was made to open a descriptor with an illegal combination
357 .Xr getdtablesize 2 ,