blob: 51ff2cdf2ac6eae261e8ca2fa9560a7773b820da [file] [log] [blame]
rsccfa37a72004-04-10 18:53:55 +00001.TH OPEN 3
2.SH NAME
3open, create, close \- open a file for reading or writing, create file
4.SH SYNOPSIS
5.B #include <u.h>
6.br
7.B #include <libc.h>
8.PP
9.B
10int open(char *file, int omode)
11.PP
12.B
13int create(char *file, int omode, ulong perm)
14.PP
15.B
16int close(int fd)
17.SH DESCRIPTION
18.I Open
19opens the
20.I file
21for I/O and returns an associated file descriptor.
22.I Omode
23is one of
24.BR OREAD ,
25.BR OWRITE ,
26.BR ORDWR ,
27or
28.BR OEXEC ,
29asking for permission to read, write, read and write, or execute, respectively.
30In addition, there are three values that can be ORed with the
31.IR omode :
32.B OTRUNC
33says to truncate the file
34to zero length before opening it;
35.B OCEXEC
36says to close the file when an
rscbf8a59f2004-04-11 03:42:27 +000037.IR exec (3)
rsccfa37a72004-04-10 18:53:55 +000038or
39.I execl
40system call is made;
41and
42.B ORCLOSE
43says to remove the file when it is closed (by everyone who has a copy of the file descriptor).
44.I Open
45fails if the file does not exist or the user does not have
46permission to open it for the requested purpose
47(see
rscbf8a59f2004-04-11 03:42:27 +000048.IR stat (3)
rsccfa37a72004-04-10 18:53:55 +000049for a description of permissions).
50The user must have write permission on the
51.I file
52if the
53.B OTRUNC
54bit is set.
55For the
56.I open
57system call
58(unlike the implicit
59.I open
60in
rscbf8a59f2004-04-11 03:42:27 +000061.IR exec (3)),
rsccfa37a72004-04-10 18:53:55 +000062.B OEXEC
63is actually identical to
64.BR OREAD .
65.PP
66.I Create
67creates a new
68.I file
69or prepares to rewrite an existing
70.IR file ,
71opens it according to
72.I omode
73(as described for
74.IR open ),
75and returns an associated file descriptor.
76If the file is new,
77the owner is set to the userid of the creating process group;
78the group to that of the containing directory;
79the permissions to
80.I perm
81ANDed with the permissions of the containing directory.
82If the file already exists,
83it is truncated to 0 length,
84and the permissions, owner, and group remain unchanged.
85The created file is a directory if the
86.B DMDIR
87bit is set in
88.IR perm ,
89an exclusive-use file if the
90.B DMEXCL
91bit is set, and an append-only file if the
92.B DMAPPEND
93bit is set.
94Exclusive-use files may be open for I/O by only one client at a time,
95but the file descriptor may become invalid if no I/O is done
96for an extended period; see
rsc058b0112005-01-03 06:40:20 +000097.IR open (9p).
rsccfa37a72004-04-10 18:53:55 +000098.PP
99.I Create
100fails if the path up to the last element of
101.I file
102cannot be evaluated, if the user doesn't have write permission
103in the final directory, if the file already exists and
104does not permit the access defined by
105.IR omode ,
106of if there there are no free file descriptors.
107In the last case, the file may be created even when
108an error is returned.
rsc058b0112005-01-03 06:40:20 +0000109.\" If the file is new and the directory in which it is created is
110.\" a union directory (see
111.\" .IR intro (3))
112.\" then the constituent directory where the file is created
113.\" depends on the structure of the union: see
114.\" .IR bind (3).
rsccfa37a72004-04-10 18:53:55 +0000115.PP
116Since
117.I create
118may succeed even if the file exists, a special mechanism is necessary
119for those applications that require an atomic create operation.
120If the
121.B OEXCL
122.RB ( 0x1000 )
123bit is set in the
124.I mode
125for a
126.IR create,
127the call succeeds only if the file does not already exist;
128see
rsc058b0112005-01-03 06:40:20 +0000129.IR open (9p)
rsccfa37a72004-04-10 18:53:55 +0000130for details.
131.PP
132.I Close
133closes the file associated with a file descriptor.
134Provided the file descriptor is a valid open descriptor,
135.I close
136is guaranteed to close it; there will be no error.
137Files are closed automatically upon termination of a process;
138.I close
139allows the file descriptor to be reused.
140.SH SOURCE
rscc3674de2005-01-11 17:37:33 +0000141.B \*9/src/lib9
rsccfa37a72004-04-10 18:53:55 +0000142.SH SEE ALSO
rscbf8a59f2004-04-11 03:42:27 +0000143.IR intro (3),
rscbf8a59f2004-04-11 03:42:27 +0000144.IR stat (3)
rsccfa37a72004-04-10 18:53:55 +0000145.SH DIAGNOSTICS
146These functions set
147.IR errstr .