1 .\" $NetBSD: open.2,v 1.8 1995/02/27 12:35:14 cgd Exp $
3 .\" Copyright (c) 1980, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. All advertising materials mentioning features or use of this software
15 .\" must display the following acknowledgement:
16 .\" This product includes software developed by the University of
17 .\" California, Berkeley and its contributors.
18 .\" 4. Neither the name of the University nor the names of its contributors
19 .\" may be used to endorse or promote products derived from this software
20 .\" without specific prior written permission.
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" @(#)open.2 8.2 (Berkeley) 11/16/93
41 .Nd open or create a file for reading or writing
43 .\" OH??? .Fd #include <sys/stat.h>
44 .Fd #include <fcntl.h>
47 .Fa "const char *path"
52 The file name specified by
55 for reading and/or writing,
56 as specified by the argument
58 the file descriptor is returned to the calling process.
62 argument may indicate that the file is to be
63 created if it does not exist (by specifying the
67 requires a third argument
69 the file is created with mode
73 and modified by the process' umask value (see
76 The flags specified are formed by
80 .Bd -literal -offset indent -compact
81 O_RDONLY open for reading only
82 O_WRONLY open for writing only
83 O_RDWR open for reading and writing
84 O_NONBLOCK do not block on open or for data to become available
85 O_APPEND append on each write
86 O_CREAT create file if it does not exist
87 O_TRUNC truncate size to 0
88 O_EXCL error if O_CREAT and the file exists
89 O_SHLOCK atomically obtain a shared lock
90 O_EXLOCK atomically obtain an exclusive lock
91 O_NOFOLLOW do not follow symlinks
92 O_SYMLINK allow open of symlinks
97 set causes each write on the file to be appended to the end. If
100 file exists, the file is truncated to zero length.
109 This may be used to implement a simple exclusive-access locking mechanism.
112 is set and the last component of the pathname is a symbolic link,
114 will fail even if the symbolic link points to a non-existent name.
118 flag is specified, do not wait for the device or file
119 to be ready or available. If the
122 in the process being blocked for some reason
123 (e.g., waiting for carrier on a dialup line),
126 This flag also has the effect of making all subsequent I/O
127 on the open file non-blocking.
129 When opening a file, a lock with
131 semantics can be obtained by setting
133 for a shared lock, or
135 for an exclusive lock.
136 If creating a file with
138 the request for the lock will never fail
139 (provided that the underlying filesystem supports locking).
143 is used in the mask and the target file passed to
145 is a symbolic link then the
151 is used in the mask and the target file passed to
153 is a symbolic link then the
155 will be for the symbolic link itself, not what it links to.
159 returns a non-negative integer, termed a file descriptor.
160 It returns -1 on failure.
161 The file pointer (used to mark the current position within the file)
162 is set to the beginning of the file.
164 When a new file is created,
165 it is given the group of the directory which contains it.
167 The new descriptor is set to remain open across
174 The system imposes a limit on the number of file descriptors
175 that can be held open simultaneously by one process.
177 returns the current system limit.
181 returns a non-negative integer, termed a file descriptor.
182 It returns -1 on failure, and sets
184 to indicate the error.
186 The named file is opened unless:
190 Search permission is denied for a component of the path prefix.
193 The required permissions (for reading and/or writing)
194 are denied for the given flags.
199 the file does not exist,
200 and the directory in which it is to be created
201 does not permit writing.
205 is specified and write permission is denied.
209 specifies the slave side of a locked pseudo-terminal device.
214 the file does not exist,
215 and the directory in which the entry for the new file
216 is being placed cannot be extended because the
217 user's quota of disk blocks on the file system
218 containing the directory has been exhausted.
223 the file does not exist,
224 and the user's quota of inodes on the file system
225 on which the file is being created has been exhausted.
231 are specified and the file exists.
235 points outside the process's allocated address space.
240 operation is interrupted by a signal.
248 An I/O error occurs while making the directory entry or
249 allocating the inode for
253 The named file is a directory, and the arguments specify
254 that it is to be opened for writing.
257 Too many symbolic links are encountered in translating the pathname.
258 This is taken to be indicative of a looping symbolic link.
261 The process has already reached its limit for open file descriptors.
263 .It Bq Er ENAMETOOLONG
264 A component of a pathname exceeds
266 characters, or an entire path name exceeded
271 The system file table is full.
275 was specified and the target is a symbolic link.
279 is not set and the named file does not exist.
282 A component of the path name that must exist does not exist.
287 the file does not exist,
288 and the directory in which the entry for the new file is being placed
289 cannot be extended because there is no space left on the file
290 system containing the directory.
295 the file does not exist,
296 and there are no free inodes on the file system on which the
297 file is being created.
300 A component of the path prefix is not a directory.
303 The named file is a character-special or block-special file
304 and the device associated with this special file does not exist.
307 O_NONBLOCK and O_WRONLY are set, the file is a FIFO,
308 and no process has it open for reading.
314 is specified, but the underlying filesystem does not support locking.
317 An attempt is made to open a socket (not currently implemented).
320 The named file is a regular file
321 and its size does not fit in an object of type off_t.
324 The named file resides on a read-only file system,
325 and the file is to be modified.
328 The file is a pure procedure (shared text) file that is being
331 call requests write access.
335 on a terminal device (i.e., /dev/console)
336 will now make that device a controlling terminal for the process.
337 Use the O_NOCTTY flag to open a terminal device
338 without changing your controlling terminal.
343 .Xr getdtablesize 2 ,
351 function call appeared in