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