1.\" Copyright (c) 1980, 1991, 1993 2.\" The Regents of the University of California. 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 8.2 (Berkeley) 11/16/93 33.\" $FreeBSD$ 34.\" 35.Dd November 16, 1993 36.Dt OPEN 2 37.Os 38.Sh NAME 39.Nm open 40.Nd open or create a file for reading or writing 41.Sh LIBRARY 42.Lb libc 43.Sh SYNOPSIS 44.In fcntl.h 45.Ft int 46.Fn open "const char *path" "int flags" "..." 47.Sh DESCRIPTION 48The file name specified by 49.Fa path 50is opened 51for reading and/or writing as specified by the 52argument 53.Fa flags 54and the file descriptor returned to the calling process. 55The 56.Fa flags 57argument may indicate the file is to be 58created if it does not exist (by specifying the 59.Dv O_CREAT 60flag). 61In this case 62.Fn open 63requires a third argument 64.Fa "mode_t mode" , 65and the file is created with mode 66.Fa mode 67as described in 68.Xr chmod 2 69and modified by the process' umask value (see 70.Xr umask 2 ) . 71.Pp 72The flags specified are formed by 73.Em or Ns 'ing 74the following values 75.Pp 76.Bd -literal -offset indent -compact 77O_RDONLY open for reading only 78O_WRONLY open for writing only 79O_RDWR open for reading and writing 80O_NONBLOCK do not block on open 81O_APPEND append on each write 82O_CREAT create file if it does not exist 83O_TRUNC truncate size to 0 84O_EXCL error if create and file exists 85O_SHLOCK atomically obtain a shared lock 86O_EXLOCK atomically obtain an exclusive lock 87O_DIRECT eliminate or reduce cache effects 88O_FSYNC synchronous writes 89O_SYNC synchronous writes 90O_NOFOLLOW do not follow symlinks 91O_NOCTTY don't assign controlling terminal 92.Ed 93.Pp 94Opening a file with 95.Dv O_APPEND 96set causes each write on the file 97to be appended to the end. 98If 99.Dv O_TRUNC 100is specified and the 101file exists, the file is truncated to zero length. 102If 103.Dv O_EXCL 104is set with 105.Dv O_CREAT 106and the file already 107exists, 108.Fn open 109returns an error. 110This may be used to 111implement a simple exclusive access locking mechanism. 112If 113.Dv O_EXCL 114is set and the last component of the pathname is 115a symbolic link, 116.Fn open 117will fail even if the symbolic 118link points to a non-existent name. 119If the 120.Dv O_NONBLOCK 121flag is specified and the 122.Fn open 123system call would result 124in the process being blocked for some reason (e.g., waiting for 125carrier on a dialup line), 126.Fn open 127returns immediately. 128The descriptor remains in non-blocking mode for subsequent operations. 129.Pp 130If 131.Dv O_FSYNC 132is used in the mask, all writes will 133immediately be written to disk, 134the kernel will not cache written data 135and all writes on the descriptor will not return until 136the data to be written completes. 137.Pp 138.Dv O_SYNC 139is a synonym for 140.Dv O_FSYNC 141required by 142.Tn POSIX . 143.Pp 144If 145.Dv O_NOFOLLOW 146is used in the mask and the target file passed to 147.Fn open 148is a symbolic link then the 149.Fn open 150will fail. 151.Pp 152When opening a file, a lock with 153.Xr flock 2 154semantics can be obtained by setting 155.Dv O_SHLOCK 156for a shared lock, or 157.Dv O_EXLOCK 158for an exclusive lock. 159If creating a file with 160.Dv O_CREAT , 161the request for the lock will never fail 162(provided that the underlying file system supports locking). 163.Pp 164.Dv O_DIRECT 165may be used to minimize or eliminate the cache effects of reading and writing. 166The system will attempt to avoid caching the data you read or write. 167If it cannot avoid caching the data, 168it will minimize the impact the data has on the cache. 169Use of this flag can drastically reduce performance if not used with care. 170.Pp 171.Dv O_NOCTTY 172may be used to ensure the OS does not assign this file as the 173controlling terminal when it opens a tty device. 174This is the default on 175.Fx , 176but is present for 177.Tn POSIX 178compatibility. 179The 180.Fn open 181system call will not assign controlling terminals on 182.Fx . 183.Pp 184If successful, 185.Fn open 186returns a non-negative integer, termed a file descriptor. 187It returns -1 on failure. 188The file pointer used to mark the current position within the 189file is set to the beginning of the file. 190.Pp 191When a new file is created it is given the group of the directory 192which contains it. 193.Pp 194The new descriptor is set to remain open across 195.Xr execve 2 196system calls; see 197.Xr close 2 198and 199.Xr fcntl 2 . 200.Pp 201The system imposes a limit on the number of file descriptors 202open simultaneously by one process. 203The 204.Xr getdtablesize 2 205system call returns the current system limit. 206.Sh RETURN VALUES 207If successful, 208.Fn open 209returns a non-negative integer, termed a file descriptor. 210It returns -1 on failure, and sets 211.Va errno 212to indicate the error. 213.Sh ERRORS 214The named file is opened unless: 215.Bl -tag -width Er 216.It Bq Er ENOTDIR 217A component of the path prefix is not a directory. 218.It Bq Er ENAMETOOLONG 219A component of a pathname exceeded 255 characters, 220or an entire path name exceeded 1023 characters. 221.It Bq Er ENOENT 222.Dv O_CREAT 223is not set and the named file does not exist. 224.It Bq Er ENOENT 225A component of the path name that must exist does not exist. 226.It Bq Er EACCES 227Search permission is denied for a component of the path prefix. 228.It Bq Er EACCES 229The required permissions (for reading and/or writing) 230are denied for the given flags. 231.It Bq Er EACCES 232.Dv O_CREAT 233is specified, 234the file does not exist, 235and the directory in which it is to be created 236does not permit writing. 237.It Bq Er ELOOP 238Too many symbolic links were encountered in translating the pathname. 239.It Bq Er EISDIR 240The named file is a directory, and the arguments specify 241it is to be opened for writing. 242.It Bq Er EROFS 243The named file resides on a read-only file system, 244and the file is to be modified. 245.It Bq Er EMFILE 246The process has already reached its limit for open file descriptors. 247.It Bq Er ENFILE 248The system file table is full. 249.It Bq Er EMLINK 250.Dv O_NOFOLLOW 251was specified and the target is a symbolic link. 252.It Bq Er ENXIO 253The named file is a character special or block 254special file, and the device associated with this special file 255does not exist. 256.It Bq Er ENXIO 257The named file is a fifo, no process has 258it open for reading, and the arguments specify it is 259to be opened for writing. 260.It Bq Er EINTR 261The 262.Fn open 263operation was interrupted by a signal. 264.It Bq Er EOPNOTSUPP 265.Dv O_SHLOCK 266or 267.Dv O_EXLOCK 268is specified but the underlying file system does not support locking. 269.It Bq Er EOPNOTSUPP 270The named file is a special file mounted through a file system that 271does not support access to it (e.g.\& NFS). 272.It Bq Er EWOULDBLOCK 273.Dv O_NONBLOCK 274and one of 275.Dv O_SHLOCK 276or 277.Dv O_EXLOCK 278is specified and the file is locked. 279.It Bq Er ENOSPC 280.Dv O_CREAT 281is specified, 282the file does not exist, 283and the directory in which the entry for the new file is being placed 284cannot be extended because there is no space left on the file 285system containing the directory. 286.It Bq Er ENOSPC 287.Dv O_CREAT 288is specified, 289the file does not exist, 290and there are no free inodes on the file system on which the 291file is being created. 292.It Bq Er EDQUOT 293.Dv O_CREAT 294is specified, 295the file does not exist, 296and the directory in which the entry for the new file 297is being placed cannot be extended because the 298user's quota of disk blocks on the file system 299containing the directory has been exhausted. 300.It Bq Er EDQUOT 301.Dv O_CREAT 302is specified, 303the file does not exist, 304and the user's quota of inodes on the file system on 305which the file is being created has been exhausted. 306.It Bq Er EIO 307An I/O error occurred while making the directory entry or 308allocating the inode for 309.Dv O_CREAT . 310.It Bq Er ETXTBSY 311The file is a pure procedure (shared text) file that is being 312executed and the 313.Fn open 314system call requests write access. 315.It Bq Er EFAULT 316The 317.Fa path 318argument 319points outside the process's allocated address space. 320.It Bq Er EEXIST 321.Dv O_CREAT 322and 323.Dv O_EXCL 324were specified and the file exists. 325.It Bq Er EOPNOTSUPP 326An attempt was made to open a socket (not currently implemented). 327.It Bq Er EINVAL 328An attempt was made to open a descriptor with an illegal combination 329of 330.Dv O_RDONLY , 331.Dv O_WRONLY , 332and 333.Dv O_RDWR . 334.El 335.Sh SEE ALSO 336.Xr chmod 2 , 337.Xr close 2 , 338.Xr dup 2 , 339.Xr fhopen 2 , 340.Xr getdtablesize 2 , 341.Xr getfh 2 , 342.Xr lgetfh 2 , 343.Xr lseek 2 , 344.Xr read 2 , 345.Xr umask 2 , 346.Xr write 2 , 347.Xr fopen 3 348.Sh HISTORY 349The 350.Fn open 351function appeared in 352.At v6 . 353