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 $
40 .Nd open or create a file for reading or writing
46 .Fn open "const char *path" "int flags" "..."
48 The file name specified by
51 for reading and/or writing as specified by the
54 and the file descriptor returned to the calling process.
57 argument may indicate the file is to be
58 created if it does not exist (by specifying the
63 requires a third argument
65 and the file is created with mode
69 and modified by the process' umask value (see
72 The flags specified are formed by
76 .Bd -literal -offset indent -compact
77 O_RDONLY open for reading only
78 O_WRONLY open for writing only
79 O_RDWR open for reading and writing
80 O_NONBLOCK do not block on open
81 O_APPEND append on each write
82 O_CREAT create file if it does not exist
83 O_TRUNC truncate size to 0
84 O_EXCL error if create and file exists
85 O_SHLOCK atomically obtain a shared lock
86 O_EXLOCK atomically obtain an exclusive lock
87 O_DIRECT eliminate or reduce cache effects
88 O_FSYNC synchronous writes
89 O_NOFOLLOW do not follow symlinks
94 set causes each write on the file
95 to be appended to the end.
99 file exists, the file is truncated to zero length.
109 implement a simple exclusive access locking mechanism.
112 is set and the last component of the pathname is
115 will fail even if the symbolic
116 link points to a non-existent name.
119 flag is specified and the
122 in the process being blocked for some reason (e.g., waiting for
123 carrier on a dialup line),
126 The first time the process attempts to perform I/O on the open
127 file it will block (not currently implemented).
131 is used in the mask, all writes will
132 immediately be written to disk,
133 the kernel will not cache written data
134 and all writes on the descriptor will not return until
135 the data to be written completes.
139 is used in the mask and the target file passed to
141 is a symbolic link then the
145 When opening a file, a lock with
147 semantics can be obtained by setting
149 for a shared lock, or
151 for an exclusive lock.
152 If creating a file with
154 the request for the lock will never fail
155 (provided that the underlying filesystem supports locking).
158 may be used to minimize or eliminate the cache effects of reading and writing.
159 The system will attempt to avoid caching the data you read or write.
160 If it cannot avoid caching the data,
161 it will minimize the impact the data has on the cache.
162 Use of this flag can drastically reduce performance if not used with care.
166 returns a non-negative integer, termed a file descriptor.
167 It returns -1 on failure.
168 The file pointer used to mark the current position within the
169 file is set to the beginning of the file.
171 When a new file is created it is given the group of the directory
174 The new descriptor is set to remain open across
181 The system imposes a limit on the number of file descriptors
182 open simultaneously by one process.
184 returns the current system limit.
188 returns a non-negative integer, termed a file descriptor.
189 It returns -1 on failure, and sets
191 to indicate the error.
193 The named file is opened unless:
196 A component of the path prefix is not a directory.
197 .It Bq Er ENAMETOOLONG
198 A component of a pathname exceeded 255 characters,
199 or an entire path name exceeded 1023 characters.
202 is not set and the named file does not exist.
204 A component of the path name that must exist does not exist.
206 Search permission is denied for a component of the path prefix.
208 The required permissions (for reading and/or writing)
209 are denied for the given flags.
213 the file does not exist,
214 and the directory in which it is to be created
215 does not permit writing.
217 Too many symbolic links were encountered in translating the pathname.
219 The named file is a directory, and the arguments specify
220 it is to be opened for writing.
222 The named file resides on a read-only file system,
223 and the file is to be modified.
225 The process has already reached its limit for open file descriptors.
227 The system file table is full.
230 was specified and the target is a symbolic link.
232 The named file is a character special or block
233 special file, and the device associated with this special file
236 The named file is a fifo, no process has
237 it open for reading, and the arguments specify it is
238 to be opened for writing.
242 operation was interrupted by a signal.
247 is specified but the underlying filesystem does not support locking.
248 .It Bq Er EWOULDBLOCK
254 is specified and the file is locked.
258 the file does not exist,
259 and the directory in which the entry for the new file is being placed
260 cannot be extended because there is no space left on the file
261 system containing the directory.
265 the file does not exist,
266 and there are no free inodes on the file system on which the
267 file is being created.
271 the file does not exist,
272 and the directory in which the entry for the new file
273 is being placed cannot be extended because the
274 user's quota of disk blocks on the file system
275 containing the directory has been exhausted.
279 the file does not exist,
280 and the user's quota of inodes on the file system on
281 which the file is being created has been exhausted.
283 An I/O error occurred while making the directory entry or
284 allocating the inode for
287 The file is a pure procedure (shared text) file that is being
290 call requests write access.
293 points outside the process's allocated address space.
298 were specified and the file exists.
300 An attempt was made to open a socket (not currently implemented).
302 An attempt was made to open a descriptor with an illegal combination
313 .Xr getdtablesize 2 ,
321 function call appeared in