int open(char *filename, int flag, mode_t mode);
The open function converts a file name to a file descriptor and returns the
descriptor number. The descriptor returned is always the smallest descriptor that
is not currently open in the process. The flags argument indicates how the process
intends to access the file:
O_RDONLY: Reading only
O_WRONLY: Writing only
O_RDWR: Reading and writing
The flags argument can also be or’d with one or more bit masks that provide
additional instructions for writing:
O_CREAT: If the file does not exist, then create a truncated (empty) version
of it.
O_TRUNC: If the file already exists, then truncate it.
O_APPEND: Before each write operation, set the file position to the end of
the file.
The mode argument specifies the access permission bits of new files. The
symbolic names for these bits are shown in Figure 10.1. As part of its context,
each process has a umask that is set by calling the umask function. When a process
creates a new file by calling the open function with some mode argument, then the
access permission bits of the file are set to mode & ~umask.