147208Scael.\" Copyright (c) 1980, 1991 Regents of the University of California. 247208Scael.\" All rights reserved. 320038Smckusick.\" 447208Scael.\" %sccs.include.redist.man% 520038Smckusick.\" 6*49903Smckusick.\" @(#)open.2 6.7 (Berkeley) 05/27/91 747208Scael.\" 847208Scael.Dd 947208Scael.Dt OPEN 2 1047208Scael.Os BSD 4 1147208Scael.Sh NAME 1247208Scael.Nm open 1347208Scael.Nd open or create a file for reading or writing 1447208Scael.Sh SYNOPSIS 1547208Scael.Fd #include <sys/file.h> 1647208Scael.Ft int 1747208Scael.Fn open "const char *path" "int flags" "mode_t mode" 1847208Scael.Sh DESCRIPTION 1947208ScaelThe file name specified by 2047208Scael.Fa path 2147208Scaelis opened 2247208Scaelfor reading and/or writing as specified by the 2347208Scaelargument 2447208Scael.Fa flags 2547208Scaeland the file descriptor returned to the calling process. 2620039SmckusickThe 2747208Scael.Fa flags 2820039Smckusickargument may indicate the file is to be 2947208Scaelcreated if it does not exist (by specifying the 3047208Scael.Dv O_CREAT 3147208Scaelflag), in which case the file is created with mode 3247208Scael.Fa mode 3320039Smckusickas described in 3447208Scael.Xr chmod 2 3520039Smckusickand modified by the process' umask value (see 3647208Scael.Xr umask 2 ) . 3747208Scael.Pp 3820039SmckusickThe flags specified are formed by 3947208Scael.Em or Ap ing 4020039Smckusickthe following values 4147208Scael.Pp 4247208Scael.Bd -literal -offset indent -compact 4347208ScaelO_RDONLY open for reading only 4447208ScaelO_WRONLY open for writing only 4547208ScaelO_RDWR open for reading and writing 46*49903SmckusickO_NONBLOCK do not block on open 4747208ScaelO_APPEND append on each write 4847208ScaelO_CREAT create file if it does not exist 4947208ScaelO_TRUNC truncate size to 0 5047208ScaelO_EXCL error if create and file exists 51*49903SmckusickO_SHLOCK atomically obtain a shared lock 52*49903SmckusickO_EXLOCK atomically obtain an exclusive lock 5347208Scael.Ed 5447208Scael.Pp 5547208ScaelOpening a file with 5647208Scael.Dv O_APPEND 5747208Scaelset causes each write on the file 5847208Scaelto be appended to the end. If 5947208Scael.Dv O_TRUNC 6047208Scaelis specified and the 6120039Smckusickfile exists, the file is truncated to zero length. 6247208ScaelIf 6347208Scael.Dv O_EXCL 6447208Scaelis set with 6547208Scael.Dv O_CREAT 6647208Scaeland the file already 6747208Scaelexists, 6847208Scael.Fn open 6947208Scaelreturns an error. This may be used to 7020039Smckusickimplement a simple exclusive access locking mechanism. 7147208ScaelIf 7247208Scael.Dv O_EXCL 7347208Scaelis set and the last component of the pathname is 7447208Scaela symbolic link, 7547208Scael.Fn open 7647208Scaelwill fail even if the symbolic 7720040Smckusicklink points to a non-existent name. 7847208ScaelIf the 79*49903Smckusick.Dv O_NONBLOCK 8047208Scaelflag is specified and the 8147208Scael.Fn open 8247208Scaelcall would result 8347208Scaelin the process being blocked for some reason (e.g., waiting for 8447208Scaelcarrier on a dialup line), 8547208Scael.Fn open 8647208Scaelreturns immediately. 8747208ScaelThe first time the process attempts to perform I/O on the open 8820039Smckusickfile it will block (not currently implemented). 8947208Scael.Pp 90*49903SmckusickWhen opening a file, a lock with 91*49903Smckusick.Xr flock 2 92*49903Smckusicksemantics can be obtained by setting 93*49903Smckusick.Dv O_SHLOCK 94*49903Smckusickfor a shared lock, or 95*49903Smckusick.Dv O_EXLOCK 96*49903Smckusickfor an exclusive lock. 97*49903SmckusickIf creating a file with 98*49903Smckusick.Dv O_CREAT , 99*49903Smckusickthe request for the lock will never fail 100*49903Smckusick(provided that the underlying filesystem supports locking). 101*49903Smckusick.Pp 10247208ScaelIf successful, 10347208Scael.Fn open 10447208Scaelreturns a non-negative integer, termed a 10535356Sbosticfile descriptor. It returns -1 on failure. 10620039SmckusickThe file pointer used to mark the current position within the 10720039Smckusickfile is set to the beginning of the file. 10847208Scael.Pp 10920039SmckusickThe new descriptor is set to remain open across 11047208Scael.Xr execve 11120039Smckusicksystem calls; see 11247208Scael.Xr close 2 11347208Scaeland 11447208Scael.Xr fcntl 2 . 11547208Scael.Pp 11628109SkarelsThe system imposes a limit on the number of file descriptors 11728109Skarelsopen simultaneously by one process. 11847208Scael.Xr Getdtablesize 2 11928109Skarelsreturns the current system limit. 12047208Scael.Sh ERRORS 12147208ScaelThe named file is opened unless: 12247208Scael.Bl -tag -width Er 12347208Scael.It Bq Er ENOTDIR 12420039SmckusickA component of the path prefix is not a directory. 12547208Scael.It Bq Er ENAMETOOLONG 12621003SmckusickA component of a pathname exceeded 255 characters, 12721003Smckusickor an entire path name exceeded 1023 characters. 12847208Scael.It Bq Er ENOENT 12947208Scael.Dv O_CREAT 13047208Scaelis not set and the named file does not exist. 13147208Scael.It Bq Er ENOENT 13224439SmckusickA component of the path name that must exist does not exist. 13347208Scael.It Bq Er EACCES 13421003SmckusickSearch permission is denied for a component of the path prefix. 13547208Scael.It Bq Er EACCES 13620039SmckusickThe required permissions (for reading and/or writing) 13747208Scaelare denied for the given flags. 13847208Scael.It Bq Er EACCES 13947208Scael.Dv O_CREAT 14047208Scaelis specified, 14124439Smckusickthe file does not exist, 14224439Smckusickand the directory in which it is to be created 14324439Smckusickdoes not permit writing. 14447208Scael.It Bq Er ELOOP 14521003SmckusickToo many symbolic links were encountered in translating the pathname. 14647208Scael.It Bq Er EISDIR 14720039SmckusickThe named file is a directory, and the arguments specify 14847208Scaelit is to be opened for writing. 14947208Scael.It Bq Er EROFS 15020039SmckusickThe named file resides on a read-only file system, 15120039Smckusickand the file is to be modified. 15247208Scael.It Bq Er EMFILE 15347208ScaelThe process has already reached its limit for open file descriptors. 15447208Scael.It Bq Er ENFILE 15524439SmckusickThe system file table is full. 15647208Scael.It Bq Er ENXIO 15720039SmckusickThe named file is a character special or block 15820039Smckusickspecial file, and the device associated with this special file 15920039Smckusickdoes not exist. 160*49903Smckusick.It Bq Er EINTR 161*49903SmckusickThe 162*49903Smckusick.Nm 163*49903Smckusickoperation was interrupted by a signal. 164*49903Smckusick.It Bq Er EOPNOTSUPP 165*49903Smckusick.Dv O_SHLOCK 166*49903Smckusickor 167*49903Smckusick.Dv O_EXLOCK 168*49903Smckusickis specified but the underlying filesystem does not support locking. 16947208Scael.It Bq Er ENOSPC 17047208Scael.Dv O_CREAT 17147208Scaelis specified, 17224439Smckusickthe file does not exist, 17324439Smckusickand the directory in which the entry for the new file is being placed 17424439Smckusickcannot be extended because there is no space left on the file 17524439Smckusicksystem containing the directory. 17647208Scael.It Bq Er ENOSPC 17747208Scael.Dv O_CREAT 17847208Scaelis specified, 17924439Smckusickthe file does not exist, 18024439Smckusickand there are no free inodes on the file system on which the 18124439Smckusickfile is being created. 18247208Scael.It Bq Er EDQUOT 18347208Scael.Dv O_CREAT 18447208Scaelis specified, 18524439Smckusickthe file does not exist, 186*49903Smckusickand the directory in which the entry for the new file 18724439Smckusickis being placed cannot be extended because the 18824439Smckusickuser's quota of disk blocks on the file system 18924439Smckusickcontaining the directory has been exhausted. 19047208Scael.It Bq Er EDQUOT 19147208Scael.Dv O_CREAT 19247208Scaelis specified, 19324439Smckusickthe file does not exist, 19424439Smckusickand the user's quota of inodes on the file system on 19524439Smckusickwhich the file is being created has been exhausted. 19647208Scael.It Bq Er EIO 19721003SmckusickAn I/O error occurred while making the directory entry or 19847208Scaelallocating the inode for 19947208Scael.Dv O_CREAT . 20047208Scael.It Bq Er ETXTBSY 20120039SmckusickThe file is a pure procedure (shared text) file that is being 20247208Scaelexecuted and the 20347208Scael.Fn open 20447208Scaelcall requests write access. 20547208Scael.It Bq Er EFAULT 20647208Scael.Fa Path 20720039Smckusickpoints outside the process's allocated address space. 20847208Scael.It Bq Er EEXIST 20947208Scael.Dv O_CREAT 21047208Scaeland 21147208Scael.Dv O_EXCL 21247208Scaelwere specified and the file exists. 21347208Scael.It Bq Er EOPNOTSUPP 21420039SmckusickAn attempt was made to open a socket (not currently implemented). 21547208Scael.El 21647208Scael.Sh SEE ALSO 21747208Scael.Xr chmod 2 , 21847208Scael.Xr close 2 , 21947208Scael.Xr dup 2 , 22047208Scael.Xr getdtablesize 2 , 22147208Scael.Xr lseek 2 , 22247208Scael.Xr read 2 , 22347208Scael.Xr write 2 , 22447208Scael.Xr umask 2 22547208Scael.Sh HISTORY 22647208ScaelAn 22747208Scael.Nm 22847208Scaelfunction call appeared in Version 6 AT&T UNIX. 229