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 .\" 3. All advertising materials mentioning features or use of this software
13 .\" must display the following acknowledgement:
14 .\" This product includes software developed by the University of
15 .\" California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" @(#)open.2 8.2 (Berkeley) 11/16/93
33 .\" $FreeBSD: src/lib/libc/sys/open.2,v 1.11.2.9 2001/12/14 18:34:01 ru Exp $
34 .\" $DragonFly: src/lib/libc/sys/open.2,v 1.2 2003/06/17 04:26:47 dillon Exp $
41 .Nd open or create a file for reading or writing
47 .Fn open "const char *path" "int flags" "..."
49 The file name specified by
52 for reading and/or writing as specified by the
55 and the file descriptor returned to the calling process.
58 argument may indicate the file is to be
59 created if it does not exist (by specifying the
64 requires a third argument
66 and the file is created with mode
70 and modified by the process' umask value (see
73 The flags specified are formed by
77 .Bd -literal -offset indent -compact
78 O_RDONLY open for reading only
79 O_WRONLY open for writing only
80 O_RDWR open for reading and writing
81 O_NONBLOCK do not block on open
82 O_APPEND append on each write
83 O_CREAT create file if it does not exist
84 O_TRUNC truncate size to 0
85 O_EXCL error if create and file exists
86 O_SHLOCK atomically obtain a shared lock
87 O_EXLOCK atomically obtain an exclusive lock
88 O_DIRECT eliminate or reduce cache effects
89 O_FSYNC synchronous writes
90 O_NOFOLLOW do not follow symlinks
95 set causes each write on the file
96 to be appended to the end.
100 file exists, the file is truncated to zero length.
110 implement a simple exclusive access locking mechanism.
113 is set and the last component of the pathname is
116 will fail even if the symbolic
117 link points to a non-existent name.
120 flag is specified and the
123 in the process being blocked for some reason (e.g., waiting for
124 carrier on a dialup line),
127 The first time the process attempts to perform I/O on the open
128 file it will block (not currently implemented).
132 is used in the mask, all writes will
133 immediately be written to disk,
134 the kernel will not cache written data
135 and all writes on the descriptor will not return until
136 the data to be written completes.
140 is used in the mask and the target file passed to
142 is a symbolic link then the
146 When opening a file, a lock with
148 semantics can be obtained by setting
150 for a shared lock, or
152 for an exclusive lock.
153 If creating a file with
155 the request for the lock will never fail
156 (provided that the underlying filesystem supports locking).
159 may be used to minimize or eliminate the cache effects of reading and writing.
160 The system will attempt to avoid caching the data you read or write.
161 If it cannot avoid caching the data,
162 it will minimize the impact the data has on the cache.
163 Use of this flag can drastically reduce performance if not used with care.
167 returns a non-negative integer, termed a file descriptor.
168 It returns -1 on failure.
169 The file pointer used to mark the current position within the
170 file is set to the beginning of the file.
172 When a new file is created it is given the group of the directory
175 The new descriptor is set to remain open across
182 The system imposes a limit on the number of file descriptors
183 open simultaneously by one process.
185 returns the current system limit.
189 returns a non-negative integer, termed a file descriptor.
190 It returns -1 on failure, and sets
192 to indicate the error.
194 The named file is opened unless:
197 A component of the path prefix is not a directory.
198 .It Bq Er ENAMETOOLONG
199 A component of a pathname exceeded 255 characters,
200 or an entire path name exceeded 1023 characters.
203 is not set and the named file does not exist.
205 A component of the path name that must exist does not exist.
207 Search permission is denied for a component of the path prefix.
209 The required permissions (for reading and/or writing)
210 are denied for the given flags.
214 the file does not exist,
215 and the directory in which it is to be created
216 does not permit writing.
218 Too many symbolic links were encountered in translating the pathname.
220 The named file is a directory, and the arguments specify
221 it is to be opened for writing.
223 The named file resides on a read-only file system,
224 and the file is to be modified.
226 The process has already reached its limit for open file descriptors.
228 The system file table is full.
231 was specified and the target is a symbolic link.
233 The named file is a character special or block
234 special file, and the device associated with this special file
237 The named file is a fifo, no process has
238 it open for reading, and the arguments specify it is
239 to be opened for writing.
243 operation was interrupted by a signal.
248 is specified but the underlying filesystem does not support locking.
249 .It Bq Er EWOULDBLOCK
255 is specified and the file is locked.
259 the file does not exist,
260 and the directory in which the entry for the new file is being placed
261 cannot be extended because there is no space left on the file
262 system containing the directory.
266 the file does not exist,
267 and there are no free inodes on the file system on which the
268 file is being created.
272 the file does not exist,
273 and the directory in which the entry for the new file
274 is being placed cannot be extended because the
275 user's quota of disk blocks on the file system
276 containing the directory has been exhausted.
280 the file does not exist,
281 and the user's quota of inodes on the file system on
282 which the file is being created has been exhausted.
284 An I/O error occurred while making the directory entry or
285 allocating the inode for
288 The file is a pure procedure (shared text) file that is being
291 call requests write access.
294 points outside the process's allocated address space.
299 were specified and the file exists.
301 An attempt was made to open a socket (not currently implemented).
303 An attempt was made to open a descriptor with an illegal combination
314 .Xr getdtablesize 2 ,
322 function call appeared in