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