open: fn(file: string, omode: int) :ref FD; create:fn(file: string, omode, perm: int):ref FD;
open: fn(file: string, omode: int) :ref FD;
## returns a reference to an open file descriptor (nil if open is unsuccessful).The open function opens the file for I/O and returns an associated file descriptor. The omode argument is one of
OREAD | open for reading |
OWRITE | open for writing |
ORDWR | open for reading and writing |
The following values may be OR'ed with the above values for additional actions:
OTRUNC | Truncate the file before opening it; this requires write permission even if omode is OREAD. |
ORCLOSE | Remove the file on closure. |
The open function returns nil if the file does not exist or the user does not have permission to open it as requested (see stat, fstat, fwstat, wstat - get and put file status for a description of permissions).
create:fn(file: string, omode, perm: int): ref FD;
## returns a reference to an open file descriptor (nil if create is unsuccessful).The create function creates a new file or prepares to rewrite an existing file. The function opens it according to omode as described for open, and returns an associated file descriptor.
If the file is new, the owner is set to the user id of the creating process group, the group to that of the containing directory. Also the permissions to perm are AND'ed with the permissions of the containing directory. See the section - The create transaction in Chapter 3 of open.
If the file already exists, it is truncated to 0 length, and the permissions, owner, and group remain unchanged.
The create function returns nil if the path up to the last element of file cannot be evaluated, if the user does not have write permission in the final directory, or if the file already exists and does not permit the access defined by omode.
If the file is new and the directory in which it is created is a union directory (see Limbo System Module), then the directory where the file is created depends on the structure of the union: see byte2char, char2byte - conversion functions.
outfile := "outputfile"; outfd: ref Sys->FD; outfd = sys->open(outfile, sys->OWRITE); if(outfd == nil) { outfd = sys->create(outfile, sys->OWRITE, 8r755); if (outfd == nil) { sys->fprint(stderr, "write error: %r\n"); return; } }
For devices and network protocols where shutdown must be guaranteed, write a hangup message to the associated control file and use the return value of the write to verify closure. For example, see ip - TCP, UDP network protocols over IP in Chapter 2.