1.\" Copyright (c) 1980, 1991 Regents of the University of California. 2.\" All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 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. 19.\" 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 30.\" SUCH DAMAGE. 31.\" 32.\" @(#)open.2 6.7 (Berkeley) 5/27/91 33.\" 34.Dd May 27, 1991 35.Dt OPEN 2 36.Os BSD 4 37.Sh NAME 38.Nm open 39.Nd open or create a file for reading or writing 40.Sh SYNOPSIS 41.Fd #include <sys/file.h> 42.Ft int 43.Fn open "const char *path" "int flags" "mode_t mode" 44.Sh DESCRIPTION 45The file name specified by 46.Fa path 47is opened 48for reading and/or writing as specified by the 49argument 50.Fa flags 51and the file descriptor returned to the calling process. 52The 53.Fa flags 54argument may indicate the file is to be 55created if it does not exist (by specifying the 56.Dv O_CREAT 57flag), in which case the file is created with mode 58.Fa mode 59as described in 60.Xr chmod 2 61and modified by the process' umask value (see 62.Xr umask 2 ) . 63.Pp 64The flags specified are formed by 65.Em or Ap ing 66the following values 67.Pp 68.Bd -literal -offset indent -compact 69O_RDONLY open for reading only 70O_WRONLY open for writing only 71O_RDWR open for reading and writing 72O_NONBLOCK do not block on open 73O_APPEND append on each write 74O_CREAT create file if it does not exist 75O_TRUNC truncate size to 0 76O_EXCL error if create and file exists 77O_SHLOCK atomically obtain a shared lock 78O_EXLOCK atomically obtain an exclusive lock 79.Ed 80.Pp 81Opening a file with 82.Dv O_APPEND 83set causes each write on the file 84to be appended to the end. If 85.Dv O_TRUNC 86is specified and the 87file exists, the file is truncated to zero length. 88If 89.Dv O_EXCL 90is set with 91.Dv O_CREAT 92and the file already 93exists, 94.Fn open 95returns an error. This may be used to 96implement a simple exclusive access locking mechanism. 97If 98.Dv O_EXCL 99is set and the last component of the pathname is 100a symbolic link, 101.Fn open 102will fail even if the symbolic 103link points to a non-existent name. 104If the 105.Dv O_NONBLOCK 106flag is specified and the 107.Fn open 108call would result 109in the process being blocked for some reason (e.g., waiting for 110carrier on a dialup line), 111.Fn open 112returns immediately. 113The first time the process attempts to perform I/O on the open 114file it will block (not currently implemented). 115.Pp 116When opening a file, a lock with 117.Xr flock 2 118semantics can be obtained by setting 119.Dv O_SHLOCK 120for a shared lock, or 121.Dv O_EXLOCK 122for an exclusive lock. 123If creating a file with 124.Dv O_CREAT , 125the request for the lock will never fail 126(provided that the underlying filesystem supports locking). 127.Pp 128If successful, 129.Fn open 130returns a non-negative integer, termed a 131file descriptor. It returns -1 on failure. 132The file pointer used to mark the current position within the 133file is set to the beginning of the file. 134.Pp 135The new descriptor is set to remain open across 136.Xr execve 137system calls; see 138.Xr close 2 139and 140.Xr fcntl 2 . 141.Pp 142The system imposes a limit on the number of file descriptors 143open simultaneously by one process. 144.Xr Getdtablesize 2 145returns the current system limit. 146.Sh ERRORS 147The named file is opened unless: 148.Bl -tag -width Er 149.It Bq Er ENOTDIR 150A component of the path prefix is not a directory. 151.It Bq Er ENAMETOOLONG 152A component of a pathname exceeded 255 characters, 153or an entire path name exceeded 1023 characters. 154.It Bq Er ENOENT 155.Dv O_CREAT 156is not set and the named file does not exist. 157.It Bq Er ENOENT 158A component of the path name that must exist does not exist. 159.It Bq Er EACCES 160Search permission is denied for a component of the path prefix. 161.It Bq Er EACCES 162The required permissions (for reading and/or writing) 163are denied for the given flags. 164.It Bq Er EACCES 165.Dv O_CREAT 166is specified, 167the file does not exist, 168and the directory in which it is to be created 169does not permit writing. 170.It Bq Er ELOOP 171Too many symbolic links were encountered in translating the pathname. 172.It Bq Er EISDIR 173The named file is a directory, and the arguments specify 174it is to be opened for writing. 175.It Bq Er EROFS 176The named file resides on a read-only file system, 177and the file is to be modified. 178.It Bq Er EMFILE 179The process has already reached its limit for open file descriptors. 180.It Bq Er ENFILE 181The system file table is full. 182.It Bq Er ENXIO 183The named file is a character special or block 184special file, and the device associated with this special file 185does not exist. 186.It Bq Er EINTR 187The 188.Nm 189operation was interrupted by a signal. 190.It Bq Er EOPNOTSUPP 191.Dv O_SHLOCK 192or 193.Dv O_EXLOCK 194is specified but the underlying filesystem does not support locking. 195.It Bq Er ENOSPC 196.Dv O_CREAT 197is specified, 198the file does not exist, 199and the directory in which the entry for the new file is being placed 200cannot be extended because there is no space left on the file 201system containing the directory. 202.It Bq Er ENOSPC 203.Dv O_CREAT 204is specified, 205the file does not exist, 206and there are no free inodes on the file system on which the 207file is being created. 208.It Bq Er EDQUOT 209.Dv O_CREAT 210is specified, 211the file does not exist, 212and the directory in which the entry for the new file 213is being placed cannot be extended because the 214user's quota of disk blocks on the file system 215containing the directory has been exhausted. 216.It Bq Er EDQUOT 217.Dv O_CREAT 218is specified, 219the file does not exist, 220and the user's quota of inodes on the file system on 221which the file is being created has been exhausted. 222.It Bq Er EIO 223An I/O error occurred while making the directory entry or 224allocating the inode for 225.Dv O_CREAT . 226.It Bq Er ETXTBSY 227The file is a pure procedure (shared text) file that is being 228executed and the 229.Fn open 230call requests write access. 231.It Bq Er EFAULT 232.Fa Path 233points outside the process's allocated address space. 234.It Bq Er EEXIST 235.Dv O_CREAT 236and 237.Dv O_EXCL 238were specified and the file exists. 239.It Bq Er EOPNOTSUPP 240An attempt was made to open a socket (not currently implemented). 241.El 242.Sh SEE ALSO 243.Xr chmod 2 , 244.Xr close 2 , 245.Xr dup 2 , 246.Xr getdtablesize 2 , 247.Xr lseek 2 , 248.Xr read 2 , 249.Xr write 2 , 250.Xr umask 2 251.Sh HISTORY 252An 253.Nm 254function call appeared in Version 6 AT&T UNIX. 255