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.3 2005/07/29 23:16:04 hsu 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 lowest unused file descriptor in the process' file descriptor table
59 argument may indicate the file is to be
60 created if it does not exist (by specifying the
65 requires a third argument
67 and the file is created with mode
71 and modified by the process' umask value (see
74 The flags specified are formed by
78 .Bd -literal -offset indent -compact
79 O_RDONLY open for reading only
80 O_WRONLY open for writing only
81 O_RDWR open for reading and writing
82 O_NONBLOCK do not block on open
83 O_APPEND append on each write
84 O_CREAT create file if it does not exist
85 O_TRUNC truncate size to 0
86 O_EXCL error if create and file exists
87 O_SHLOCK atomically obtain a shared lock
88 O_EXLOCK atomically obtain an exclusive lock
89 O_DIRECT eliminate or reduce cache effects
90 O_FSYNC synchronous writes
91 O_NOFOLLOW do not follow symlinks
96 set causes each write on the file
97 to be appended to the end.
101 file exists, the file is truncated to zero length.
111 implement a simple exclusive access locking mechanism.
114 is set and the last component of the pathname is
117 will fail even if the symbolic
118 link points to a non-existent name.
121 flag is specified and the
124 in the process being blocked for some reason (e.g., waiting for
125 carrier on a dialup line),
128 The first time the process attempts to perform I/O on the open
129 file it will block (not currently implemented).
133 is used in the mask, all writes will
134 immediately be written to disk,
135 the kernel will not cache written data
136 and all writes on the descriptor will not return until
137 the data to be written completes.
141 is used in the mask and the target file passed to
143 is a symbolic link then the
147 When opening a file, a lock with
149 semantics can be obtained by setting
151 for a shared lock, or
153 for an exclusive lock.
154 If creating a file with
156 the request for the lock will never fail
157 (provided that the underlying filesystem supports locking).
160 may be used to minimize or eliminate the cache effects of reading and writing.
161 The system will attempt to avoid caching the data you read or write.
162 If it cannot avoid caching the data,
163 it will minimize the impact the data has on the cache.
164 Use of this flag can drastically reduce performance if not used with care.
168 returns a non-negative integer, termed a file descriptor.
169 It returns -1 on failure.
170 The file pointer used to mark the current position within the
171 file is set to the beginning of the file.
173 When a new file is created it is given the group of the directory
176 The new descriptor is set to remain open across
183 The system imposes a limit on the number of file descriptors
184 open simultaneously by one process.
186 returns the current system limit.
190 returns a non-negative integer, termed a file descriptor.
191 It returns -1 on failure, and sets
193 to indicate the error.
195 The named file is opened unless:
198 A component of the path prefix is not a directory.
199 .It Bq Er ENAMETOOLONG
200 A component of a pathname exceeded 255 characters,
201 or an entire path name exceeded 1023 characters.
204 is not set and the named file does not exist.
206 A component of the path name that must exist does not exist.
208 Search permission is denied for a component of the path prefix.
210 The required permissions (for reading and/or writing)
211 are denied for the given flags.
215 the file does not exist,
216 and the directory in which it is to be created
217 does not permit writing.
219 Too many symbolic links were encountered in translating the pathname.
221 The named file is a directory, and the arguments specify
222 it is to be opened for writing.
224 The named file resides on a read-only file system,
225 and the file is to be modified.
227 The process has already reached its limit for open file descriptors.
229 The system file table is full.
232 was specified and the target is a symbolic link.
234 The named file is a character special or block
235 special file, and the device associated with this special file
238 The named file is a fifo, no process has
239 it open for reading, and the arguments specify it is
240 to be opened for writing.
244 operation was interrupted by a signal.
249 is specified but the underlying filesystem does not support locking.
250 .It Bq Er EWOULDBLOCK
256 is specified and the file is locked.
260 the file does not exist,
261 and the directory in which the entry for the new file is being placed
262 cannot be extended because there is no space left on the file
263 system containing the directory.
267 the file does not exist,
268 and there are no free inodes on the file system on which the
269 file is being created.
273 the file does not exist,
274 and the directory in which the entry for the new file
275 is being placed cannot be extended because the
276 user's quota of disk blocks on the file system
277 containing the directory has been exhausted.
281 the file does not exist,
282 and the user's quota of inodes on the file system on
283 which the file is being created has been exhausted.
285 An I/O error occurred while making the directory entry or
286 allocating the inode for
289 The file is a pure procedure (shared text) file that is being
292 call requests write access.
295 points outside the process's allocated address space.
300 were specified and the file exists.
302 An attempt was made to open a socket (not currently implemented).
304 An attempt was made to open a descriptor with an illegal combination
315 .Xr getdtablesize 2 ,
323 function call appeared in