xref: /csrg-svn/lib/libc/sys/open.2 (revision 47208)
1*47208Scael.\" Copyright (c) 1980, 1991 Regents of the University of California.
2*47208Scael.\" All rights reserved.
320038Smckusick.\"
4*47208Scael.\" %sccs.include.redist.man%
520038Smckusick.\"
6*47208Scael.\"     @(#)open.2	6.6 (Berkeley) 03/10/91
7*47208Scael.\"
8*47208Scael.Dd
9*47208Scael.Dt OPEN 2
10*47208Scael.Os BSD 4
11*47208Scael.Sh NAME
12*47208Scael.Nm open
13*47208Scael.Nd open or create a file for reading or writing
14*47208Scael.Sh SYNOPSIS
15*47208Scael.Fd #include <sys/file.h>
16*47208Scael.Ft int
17*47208Scael.Fn open "const char *path" "int flags" "mode_t mode"
18*47208Scael.Sh DESCRIPTION
19*47208ScaelThe file name specified by
20*47208Scael.Fa path
21*47208Scaelis opened
22*47208Scaelfor reading and/or writing as specified by the
23*47208Scaelargument
24*47208Scael.Fa flags
25*47208Scaeland the file descriptor returned to the calling process.
2620039SmckusickThe
27*47208Scael.Fa flags
2820039Smckusickargument may indicate the file is to be
29*47208Scaelcreated if it does not exist (by specifying the
30*47208Scael.Dv O_CREAT
31*47208Scaelflag), in which case the file is created with mode
32*47208Scael.Fa mode
3320039Smckusickas described in
34*47208Scael.Xr chmod 2
3520039Smckusickand modified by the process' umask value (see
36*47208Scael.Xr umask 2 ) .
37*47208Scael.Pp
3820039SmckusickThe flags specified are formed by
39*47208Scael.Em or Ap ing
4020039Smckusickthe following values
41*47208Scael.Pp
42*47208Scael.Bd -literal -offset indent -compact
43*47208ScaelO_RDONLY	open for reading only
44*47208ScaelO_WRONLY	open for writing only
45*47208ScaelO_RDWR		open for reading and writing
46*47208ScaelO_NDELAY	do not block on open
47*47208ScaelO_APPEND	append on each write
48*47208ScaelO_CREAT		create file if it does not exist
49*47208ScaelO_TRUNC		truncate size to 0
50*47208ScaelO_EXCL		error if create and file exists
51*47208Scael.Ed
52*47208Scael.Pp
53*47208ScaelOpening a file with
54*47208Scael.Dv O_APPEND
55*47208Scaelset causes each write on the file
56*47208Scaelto be appended to the end.  If
57*47208Scael.Dv O_TRUNC
58*47208Scaelis specified and the
5920039Smckusickfile exists, the file is truncated to zero length.
60*47208ScaelIf
61*47208Scael.Dv O_EXCL
62*47208Scaelis set with
63*47208Scael.Dv O_CREAT
64*47208Scaeland the file already
65*47208Scaelexists,
66*47208Scael.Fn open
67*47208Scaelreturns an error.  This may be used to
6820039Smckusickimplement a simple exclusive access locking mechanism.
69*47208ScaelIf
70*47208Scael.Dv O_EXCL
71*47208Scaelis set and the last component of the pathname is
72*47208Scaela symbolic link,
73*47208Scael.Fn open
74*47208Scaelwill fail even if the symbolic
7520040Smckusicklink points to a non-existent name.
76*47208ScaelIf the
77*47208Scael.Dv O_NDELAY
78*47208Scaelflag is specified and the
79*47208Scael.Fn open
80*47208Scaelcall would result
81*47208Scaelin the process being blocked for some reason (e.g., waiting for
82*47208Scaelcarrier on a dialup line),
83*47208Scael.Fn open
84*47208Scaelreturns immediately.
85*47208ScaelThe first time the process attempts to perform I/O on the open
8620039Smckusickfile it will block (not currently implemented).
87*47208Scael.Pp
88*47208ScaelIf successful,
89*47208Scael.Fn open
90*47208Scaelreturns a non-negative integer, termed a
9135356Sbosticfile descriptor.  It returns -1 on failure.
9220039SmckusickThe file pointer used to mark the current position within the
9320039Smckusickfile is set to the beginning of the file.
94*47208Scael.Pp
9520039SmckusickThe new descriptor is set to remain open across
96*47208Scael.Xr execve
9720039Smckusicksystem calls; see
98*47208Scael.Xr close 2
99*47208Scaeland
100*47208Scael.Xr fcntl 2 .
101*47208Scael.Pp
10228109SkarelsThe system imposes a limit on the number of file descriptors
10328109Skarelsopen simultaneously by one process.
104*47208Scael.Xr Getdtablesize 2
10528109Skarelsreturns the current system limit.
106*47208Scael.Sh ERRORS
107*47208ScaelThe named file is opened unless:
108*47208Scael.Bl -tag -width Er
109*47208Scael.It Bq Er ENOTDIR
11020039SmckusickA component of the path prefix is not a directory.
111*47208Scael.It Bq Er EINVAL
11221003SmckusickThe pathname contains a character with the high-order bit set.
113*47208Scael.It Bq Er ENAMETOOLONG
11421003SmckusickA component of a pathname exceeded 255 characters,
11521003Smckusickor an entire path name exceeded 1023 characters.
116*47208Scael.It Bq Er ENOENT
117*47208Scael.Dv O_CREAT
118*47208Scaelis not set and the named file does not exist.
119*47208Scael.It Bq Er ENOENT
12024439SmckusickA component of the path name that must exist does not exist.
121*47208Scael.It Bq Er EACCES
12221003SmckusickSearch permission is denied for a component of the path prefix.
123*47208Scael.It Bq Er EACCES
12420039SmckusickThe required permissions (for reading and/or writing)
125*47208Scaelare denied for the given flags.
126*47208Scael.It Bq Er EACCES
127*47208Scael.Dv O_CREAT
128*47208Scaelis specified,
12924439Smckusickthe file does not exist,
13024439Smckusickand the directory in which it is to be created
13124439Smckusickdoes not permit writing.
132*47208Scael.It Bq Er ELOOP
13321003SmckusickToo many symbolic links were encountered in translating the pathname.
134*47208Scael.It Bq Er EISDIR
13520039SmckusickThe named file is a directory, and the arguments specify
136*47208Scaelit is to be opened for writing.
137*47208Scael.It Bq Er EROFS
13820039SmckusickThe named file resides on a read-only file system,
13920039Smckusickand the file is to be modified.
140*47208Scael.It Bq Er EMFILE
141*47208ScaelThe process has already reached its limit for open file descriptors.
142*47208Scael.It Bq Er ENFILE
14324439SmckusickThe system file table is full.
144*47208Scael.It Bq Er ENXIO
14520039SmckusickThe named file is a character special or block
14620039Smckusickspecial file, and the device associated with this special file
14720039Smckusickdoes not exist.
148*47208Scael.It Bq Er ENOSPC
149*47208Scael.Dv O_CREAT
150*47208Scaelis specified,
15124439Smckusickthe file does not exist,
15224439Smckusickand the directory in which the entry for the new file is being placed
15324439Smckusickcannot be extended because there is no space left on the file
15424439Smckusicksystem containing the directory.
155*47208Scael.It Bq Er ENOSPC
156*47208Scael.Dv O_CREAT
157*47208Scaelis specified,
15824439Smckusickthe file does not exist,
15924439Smckusickand there are no free inodes on the file system on which the
16024439Smckusickfile is being created.
161*47208Scael.It Bq Er EDQUOT
162*47208Scael.Dv O_CREAT
163*47208Scaelis specified,
16424439Smckusickthe file does not exist,
16524439Smckusickand the directory in which the entry for the new fie
16624439Smckusickis being placed cannot be extended because the
16724439Smckusickuser's quota of disk blocks on the file system
16824439Smckusickcontaining the directory has been exhausted.
169*47208Scael.It Bq Er EDQUOT
170*47208Scael.Dv O_CREAT
171*47208Scaelis specified,
17224439Smckusickthe file does not exist,
17324439Smckusickand the user's quota of inodes on the file system on
17424439Smckusickwhich the file is being created has been exhausted.
175*47208Scael.It Bq Er EIO
17621003SmckusickAn I/O error occurred while making the directory entry or
177*47208Scaelallocating the inode for
178*47208Scael.Dv O_CREAT .
179*47208Scael.It Bq Er ETXTBSY
18020039SmckusickThe file is a pure procedure (shared text) file that is being
181*47208Scaelexecuted and the
182*47208Scael.Fn open
183*47208Scaelcall requests write access.
184*47208Scael.It Bq Er EFAULT
185*47208Scael.Fa Path
18620039Smckusickpoints outside the process's allocated address space.
187*47208Scael.It Bq Er EEXIST
188*47208Scael.Dv O_CREAT
189*47208Scaeland
190*47208Scael.Dv O_EXCL
191*47208Scaelwere specified and the file exists.
192*47208Scael.It Bq Er EOPNOTSUPP
19320039SmckusickAn attempt was made to open a socket (not currently implemented).
194*47208Scael.El
195*47208Scael.Sh SEE ALSO
196*47208Scael.Xr chmod 2 ,
197*47208Scael.Xr close 2 ,
198*47208Scael.Xr dup 2 ,
199*47208Scael.Xr getdtablesize 2 ,
200*47208Scael.Xr lseek 2 ,
201*47208Scael.Xr read 2 ,
202*47208Scael.Xr write 2 ,
203*47208Scael.Xr umask 2
204*47208Scael.Sh HISTORY
205*47208ScaelAn
206*47208Scael.Nm
207*47208Scaelfunction call appeared in Version 6 AT&T UNIX.
208