spelling
[unix-history] / usr / src / lib / libc / sys / open.2
CommitLineData
7c9fa549
KM
1.\" Copyright (c) 1980 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
8f02ec0d 5.\" @(#)open.2 6.4 (Berkeley) %G%
7c9fa549 6.\"
3e716fce 7.TH OPEN 2 ""
7c9fa549
KM
8.UC 4
9.SH NAME
87d6e244 10open \- open a file for reading or writing, or create a new file
7c9fa549
KM
11.SH SYNOPSIS
12.nf
87d6e244
KM
13.ft B
14#include <sys/file.h>
15.PP
16.ft B
17open(path, flags, mode)
18char *path;
19int flags, mode;
7c9fa549
KM
20.fi
21.SH DESCRIPTION
22.I Open
23opens the file
87d6e244
KM
24.I path
25for reading and/or writing, as specified by the
26.I flags
27argument and returns a descriptor for that file.
28The
29.I flags
30argument may indicate the file is to be
31created if it does not already exist (by specifying the
32O_CREAT flag), in which case the file is created with mode
7c9fa549 33.I mode
87d6e244
KM
34as described in
35.IR chmod (2)
36and modified by the process' umask value (see
37.IR umask (2)).
38.PP
39.I Path
7c9fa549
KM
40is the address of a string of ASCII characters representing
41a path name, terminated by a null character.
87d6e244
KM
42The flags specified are formed by
43.IR or 'ing
44the following values
45.PP
46.RS
47 O_RDONLY open for reading only
48 O_WRONLY open for writing only
49 O_RDWR open for reading and writing
50 O_NDELAY do not block on open
51 O_APPEND append on each write
52 O_CREAT create file if it does not exist
53 O_TRUNC truncate size to 0
54 O_EXCL error if create and file exists
55.RE
56.PP
57Opening a file with O_APPEND set causes each write on the file
58to be appended to the end. If O_TRUNC is specified and the
59file exists, the file is truncated to zero length.
60If O_EXCL is set with O_CREAT, then if the file already
61exists, the open returns an error. This can be used to
62implement a simple exclusive access locking mechanism.
3e716fce
KM
63If O_EXCL is set and the last component of the pathname is
64a symbolic link, the open will fail even if the symbolic
65link points to a non-existent name.
87d6e244
KM
66If the O_NDELAY flag is specified and the open call would result
67in the process being blocked for some reason (e.g. waiting for
68carrier on a dialup line), the open returns immediately.
69The first time the process attempts to perform i/o on the open
70file it will block (not currently implemented).
71.PP
72Upon successful completion a non-negative integer termed a
73file descriptor is returned.
74The file pointer used to mark the current position within the
75file is set to the beginning of the file.
76.PP
77The new descriptor is set to remain open across
78.IR execve
79system calls; see
80.IR close (2).
7c9fa549 81.PP
8f02ec0d
MK
82The system imposes a limit on the number of file descriptors
83open simultaneously by one process.
84.IR Getdtablesize (2)
85returns the current system limit.
87d6e244
KM
86.SH "ERRORS
87The named file is opened unless one or more of the
88following are true:
89.TP 15
87d6e244
KM
90[ENOTDIR]
91A component of the path prefix is not a directory.
92.TP 15
b5984ffe
KM
93[EINVAL]
94The pathname contains a character with the high-order bit set.
95.TP 15
96[ENAMETOOLONG]
97A component of a pathname exceeded 255 characters,
98or an entire path name exceeded 1023 characters.
99.TP 15
87d6e244
KM
100[ENOENT]
101O_CREAT is not set and the named file does not exist.
102.TP 15
fd690c8b
KM
103[ENOENT]
104A component of the path name that must exist does not exist.
105.TP 15
87d6e244 106[EACCES]
b5984ffe 107Search permission is denied for a component of the path prefix.
87d6e244
KM
108.TP 15
109[EACCES]
110The required permissions (for reading and/or writing)
111are denied for the named flag.
112.TP 15
fd690c8b
KM
113[EACCES]
114O_CREAT is specified,
115the file does not exist,
116and the directory in which it is to be created
117does not permit writing.
118.TP 15
b5984ffe
KM
119[ELOOP]
120Too many symbolic links were encountered in translating the pathname.
121.TP 15
87d6e244
KM
122[EISDIR]
123The named file is a directory, and the arguments specify
124it is to be opened for writting.
125.TP 15
126[EROFS]
127The named file resides on a read-only file system,
128and the file is to be modified.
129.TP 15
130[EMFILE]
8f02ec0d 131The system limit for open file descriptors per process has already been reached.
87d6e244 132.TP 15
fd690c8b
KM
133[ENFILE]
134The system file table is full.
135.TP 15
87d6e244
KM
136[ENXIO]
137The named file is a character special or block
138special file, and the device associated with this special file
139does not exist.
140.TP 15
fd690c8b
KM
141[ENOSPC]
142O_CREAT is specified,
143the file does not exist,
144and the directory in which the entry for the new file is being placed
145cannot be extended because there is no space left on the file
146system containing the directory.
147.TP 15
148[ENOSPC]
149O_CREAT is specified,
150the file does not exist,
151and there are no free inodes on the file system on which the
152file is being created.
153.TP 15
154[EDQUOT]
155O_CREAT is specified,
156the file does not exist,
157and the directory in which the entry for the new fie
158is being placed cannot be extended because the
159user's quota of disk blocks on the file system
160containing the directory has been exhausted.
161.TP 15
162[EDQUOT]
163O_CREAT is specified,
164the file does not exist,
165and the user's quota of inodes on the file system on
166which the file is being created has been exhausted.
167.TP 15
b5984ffe
KM
168[EIO]
169An I/O error occurred while making the directory entry or
170allocating the inode for O_CREAT.
171.TP 15
87d6e244
KM
172[ETXTBSY]
173The file is a pure procedure (shared text) file that is being
174executed and the \fIopen\fP call requests write access.
175.TP 15
176[EFAULT]
177.I Path
178points outside the process's allocated address space.
179.TP 15
87d6e244 180[EEXIST]
fd690c8b 181O_CREAT and O_EXCL were specified and the file exists.
87d6e244 182.TP 15
87d6e244
KM
183[EOPNOTSUPP]
184An attempt was made to open a socket (not currently implemented).
7c9fa549 185.SH "SEE ALSO"
8f02ec0d
MK
186chmod(2), close(2), dup(2), getdtablesize(2),
187lseek(2), read(2), write(2), umask(2)