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