xref: /csrg-svn/lib/libc/sys/open.2 (revision 64891)
161183Sbostic.\" Copyright (c) 1980, 1991, 1993
261183Sbostic.\"	The Regents of the University of California.  All rights reserved.
320038Smckusick.\"
447208Scael.\" %sccs.include.redist.man%
520038Smckusick.\"
6*64891Sbostic.\"     @(#)open.2	8.2 (Berkeley) 11/16/93
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
1553065Sbostic.Fd #include <fcntl.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
39*64891Sbostic.Em or Ns '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
4649903SmckusickO_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
5149903SmckusickO_SHLOCK	atomically obtain a shared lock
5249903SmckusickO_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
7949903Smckusick.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
9049903SmckusickWhen opening a file, a lock with
9149903Smckusick.Xr flock 2
9249903Smckusicksemantics can be obtained by setting
9349903Smckusick.Dv O_SHLOCK
9449903Smckusickfor a shared lock, or
9549903Smckusick.Dv O_EXLOCK
9649903Smckusickfor an exclusive lock.
9749903SmckusickIf creating a file with
9849903Smckusick.Dv O_CREAT ,
9949903Smckusickthe request for the lock will never fail
10049903Smckusick(provided that the underlying filesystem supports locking).
10149903Smckusick.Pp
10247208ScaelIf successful,
10347208Scael.Fn open
10451641Sbosticreturns a non-negative integer, termed a file descriptor.
10551641SbosticIt 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
10951641SbosticWhen a new file is created it is given the group of the directory
11051641Sbosticwhich contains it.
11151641Sbostic.Pp
11220039SmckusickThe new descriptor is set to remain open across
11347208Scael.Xr execve
11420039Smckusicksystem calls; see
11547208Scael.Xr close 2
11647208Scaeland
11747208Scael.Xr fcntl 2 .
11847208Scael.Pp
11928109SkarelsThe system imposes a limit on the number of file descriptors
12028109Skarelsopen simultaneously by one process.
12147208Scael.Xr Getdtablesize 2
12228109Skarelsreturns the current system limit.
12347208Scael.Sh ERRORS
12447208ScaelThe named file is opened unless:
12547208Scael.Bl -tag -width Er
12647208Scael.It Bq Er ENOTDIR
12720039SmckusickA component of the path prefix is not a directory.
12847208Scael.It Bq Er ENAMETOOLONG
12921003SmckusickA component of a pathname exceeded 255 characters,
13021003Smckusickor an entire path name exceeded 1023 characters.
13147208Scael.It Bq Er ENOENT
13247208Scael.Dv O_CREAT
13347208Scaelis not set and the named file does not exist.
13447208Scael.It Bq Er ENOENT
13524439SmckusickA component of the path name that must exist does not exist.
13647208Scael.It Bq Er EACCES
13721003SmckusickSearch permission is denied for a component of the path prefix.
13847208Scael.It Bq Er EACCES
13920039SmckusickThe required permissions (for reading and/or writing)
14047208Scaelare denied for the given flags.
14147208Scael.It Bq Er EACCES
14247208Scael.Dv O_CREAT
14347208Scaelis specified,
14424439Smckusickthe file does not exist,
14524439Smckusickand the directory in which it is to be created
14624439Smckusickdoes not permit writing.
14747208Scael.It Bq Er ELOOP
14821003SmckusickToo many symbolic links were encountered in translating the pathname.
14947208Scael.It Bq Er EISDIR
15020039SmckusickThe named file is a directory, and the arguments specify
15147208Scaelit is to be opened for writing.
15247208Scael.It Bq Er EROFS
15320039SmckusickThe named file resides on a read-only file system,
15420039Smckusickand the file is to be modified.
15547208Scael.It Bq Er EMFILE
15647208ScaelThe process has already reached its limit for open file descriptors.
15747208Scael.It Bq Er ENFILE
15824439SmckusickThe system file table is full.
15947208Scael.It Bq Er ENXIO
16020039SmckusickThe named file is a character special or block
16120039Smckusickspecial file, and the device associated with this special file
16220039Smckusickdoes not exist.
16349903Smckusick.It Bq Er EINTR
16449903SmckusickThe
16549903Smckusick.Nm
16649903Smckusickoperation was interrupted by a signal.
16749903Smckusick.It Bq Er EOPNOTSUPP
16849903Smckusick.Dv O_SHLOCK
16949903Smckusickor
17049903Smckusick.Dv O_EXLOCK
17149903Smckusickis specified but the underlying filesystem does not support locking.
17247208Scael.It Bq Er ENOSPC
17347208Scael.Dv O_CREAT
17447208Scaelis specified,
17524439Smckusickthe file does not exist,
17624439Smckusickand the directory in which the entry for the new file is being placed
17724439Smckusickcannot be extended because there is no space left on the file
17824439Smckusicksystem containing the directory.
17947208Scael.It Bq Er ENOSPC
18047208Scael.Dv O_CREAT
18147208Scaelis specified,
18224439Smckusickthe file does not exist,
18324439Smckusickand there are no free inodes on the file system on which the
18424439Smckusickfile is being created.
18547208Scael.It Bq Er EDQUOT
18647208Scael.Dv O_CREAT
18747208Scaelis specified,
18824439Smckusickthe file does not exist,
18949903Smckusickand the directory in which the entry for the new file
19024439Smckusickis being placed cannot be extended because the
19124439Smckusickuser's quota of disk blocks on the file system
19224439Smckusickcontaining the directory has been exhausted.
19347208Scael.It Bq Er EDQUOT
19447208Scael.Dv O_CREAT
19547208Scaelis specified,
19624439Smckusickthe file does not exist,
19724439Smckusickand the user's quota of inodes on the file system on
19824439Smckusickwhich the file is being created has been exhausted.
19947208Scael.It Bq Er EIO
20021003SmckusickAn I/O error occurred while making the directory entry or
20147208Scaelallocating the inode for
20247208Scael.Dv O_CREAT .
20347208Scael.It Bq Er ETXTBSY
20420039SmckusickThe file is a pure procedure (shared text) file that is being
20547208Scaelexecuted and the
20647208Scael.Fn open
20747208Scaelcall requests write access.
20847208Scael.It Bq Er EFAULT
20947208Scael.Fa Path
21020039Smckusickpoints outside the process's allocated address space.
21147208Scael.It Bq Er EEXIST
21247208Scael.Dv O_CREAT
21347208Scaeland
21447208Scael.Dv O_EXCL
21547208Scaelwere specified and the file exists.
21647208Scael.It Bq Er EOPNOTSUPP
21720039SmckusickAn attempt was made to open a socket (not currently implemented).
21847208Scael.El
21947208Scael.Sh SEE ALSO
22047208Scael.Xr chmod 2 ,
22147208Scael.Xr close 2 ,
22247208Scael.Xr dup 2 ,
22347208Scael.Xr getdtablesize 2 ,
22447208Scael.Xr lseek 2 ,
22547208Scael.Xr read 2 ,
22647208Scael.Xr write 2 ,
22747208Scael.Xr umask 2
22847208Scael.Sh HISTORY
22947208ScaelAn
23047208Scael.Nm
23147208Scaelfunction call appeared in Version 6 AT&T UNIX.
232