SYS-OPEN(2)                                           SYS-OPEN(2)

     NAME
          open, create - open a file for reading or writing, create
          file

     SYNOPSIS
          include "sys.m";
          sys := load Sys Sys->PATH;

          create: fn(file: string, omode, perm: int): ref FD;
          open:   fn(file: string, omode: int):       ref FD;

     DESCRIPTION
          Open opens the file for I/O and returns an associated file
          descriptor.  Omode is one of Sys->OREAD, Sys->OWRITE, or
          Sys->ORDWR, asking for permission to read, write, or read
          and write, respectively.  There are two values that can be
          OR'd with those to form omode: Sys->OTRUNC says to truncate
          the file before opening it, which requires write permission
          even if omode is Sys->OREAD; and Sys->ORCLOSE says to remove
          the file when it is closed (ie, when the last reference to
          this file descriptor goes away).

          Open returns nil if the file does not exist or the user does
          not have permission to open it as requested (see sys-stat(2)
          for a description of permissions).

          Create creates a new file or prepares to rewrite an existing
          file, 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, and the permissions to perm ANDed with the per-
          missions of the containing directory.

          If the file already exists, it is truncated to 0 length, but
          the permissions, owner, and group remain unchanged.

          The created file will be a directory if the Sys->CHDIR bit
          is set in perm, and omode is Sys->OREAD.

          Create 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 sys-intro(2)) then the constituent
          directory where the file is created depends on the structure
          of the union: see sys-bind(2).

     Page 1                       Plan 9            (printed 11/18/24)

     SYS-OPEN(2)                                           SYS-OPEN(2)

          There is no explicit ``close'' routine: when the last refer-
          ence to the file descriptor is released, the system closes
          the associated file.  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.

     SEE ALSO
          sys-intro(2), sys-bind(2), sys-stat(2)

     Page 2                       Plan 9            (printed 11/18/24)