Improved English
[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.\"
b5984ffe 5.\" @(#)open.2 6.2 (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
87d6e244
KM
82No process may have more than {OPEN_MAX} file descriptors open
83simultaneously.
84.SH "ERRORS
85The named file is opened unless one or more of the
86following are true:
87.TP 15
87d6e244
KM
88[ENOTDIR]
89A component of the path prefix is not a directory.
90.TP 15
b5984ffe
KM
91[EINVAL]
92The pathname contains a character with the high-order bit set.
93.TP 15
94[ENAMETOOLONG]
95A component of a pathname exceeded 255 characters,
96or an entire path name exceeded 1023 characters.
97.TP 15
87d6e244
KM
98[ENOENT]
99O_CREAT is not set and the named file does not exist.
100.TP 15
101[EACCES]
b5984ffe 102Search permission is denied for a component of the path prefix.
87d6e244
KM
103.TP 15
104[EACCES]
105The required permissions (for reading and/or writing)
106are denied for the named flag.
107.TP 15
b5984ffe
KM
108[ELOOP]
109Too many symbolic links were encountered in translating the pathname.
110.TP 15
87d6e244
KM
111[EISDIR]
112The named file is a directory, and the arguments specify
113it is to be opened for writting.
114.TP 15
115[EROFS]
116The named file resides on a read-only file system,
117and the file is to be modified.
118.TP 15
119[EMFILE]
120{OPEN_MAX} file descriptors are currently open.
121.TP 15
122[ENXIO]
123The named file is a character special or block
124special file, and the device associated with this special file
125does not exist.
126.TP 15
b5984ffe
KM
127[ENXIO]
128The O_NDELAY flag is given, and the file is a communications device
129on which their is no carrier present.
130.TP 15
131[EIO]
132An I/O error occurred while making the directory entry or
133allocating the inode for O_CREAT.
134.TP 15
87d6e244
KM
135[ETXTBSY]
136The file is a pure procedure (shared text) file that is being
137executed and the \fIopen\fP call requests write access.
138.TP 15
139[EFAULT]
140.I Path
141points outside the process's allocated address space.
142.TP 15
87d6e244
KM
143[EEXIST]
144O_EXCL was specified and the file exists.
145.TP 15
87d6e244
KM
146[EOPNOTSUPP]
147An attempt was made to open a socket (not currently implemented).
7c9fa549 148.SH "SEE ALSO"
87d6e244 149chmod(2), close(2), dup(2), lseek(2), read(2), write(2), umask(2)