open

OS/161 Reference Manual

Name

open - open a file

Library

Standard C Library (libc, -lc)

Synopsis

#include <unistd.h>
#include <fcntl.h>

int
open(const char *filename, int flags);
int
open(const char *filename, int flags, mode_t mode);

Description

open opens the file, device, or other kernel object named by the pathname filename. The flags argument specifies how to open the file. The optional mode argument provides the file permissions to use and is only meaningful in Unix, or if you choose to implement Unix-style security later on. it can be ignored in OS/161.

The flags argument should consist of one of
  O_RDONLY Open for reading only.
O_WRONLY Open for writing only.
O_RDWR Open for reading and writing.

It may also have any of the following flags OR'd in:
  O_CREAT Create the file if it doesn't exist.
O_EXCL Fail if the file already exists.
O_TRUNC Truncate the file to length 0 upon open.
O_APPEND Open the file in append mode.
O_EXCL is only meaningful if O_CREAT is also used.

O_APPEND causes all writes to the file to occur at the end of file, no matter what gets written to the file by whoever else, including concurrently. (This functionality may be optional; consult your course's assignments.)

open returns a file handle suitable for passing to read, write, close, etc. This file handle must be greater than or equal to zero. Note that file handles 0 (STDIN_FILENO), 1 (STDOUT_FILENO), and 2 (STDERR_FILENO) are used in special ways and are typically assumed by user-level code to always be open.

If you are implementing symbolic links, there are some additional points to take note of. If filename refers to a symbolic link, that link does not point to an existing object, and O_CREAT is specified, a new file is created at the name the link points to. However, if in this case both O_CREAT and O_EXCL are specified, open fails with EEXIST. These semantics are a nuisance to implement but important for correct functioning.

open (like all system calls) should be atomic. It is important for the handling of O_EXCL in the destination directory to be atomic. Note, however, that in practice looking up directories that contain .. is usually not quite atomic.

Return Values

On success, open returns a nonnegative file handle. On error, -1 is returned, and errno is set according to the error encountered.

Errors

The following error codes should be returned under the conditions given. Other error codes may be returned for other cases not mentioned here.
  ENODEV The device prefix of filename did not exist.
ENOTDIR A non-final component of filename was not a directory.
ENOENT A non-final component of filename did not exist.
ENOENT The named file does not exist, and O_CREAT was not specified.
EEXIST The named file exists, and O_EXCL was specified.
EISDIR The named object is a directory, and it was to be opened for writing.
EMFILE The process's file table was full, or a process-specific limit on open files was reached.
ENFILE The system file table is full, if such a thing exists, or a system-wide limit on open files was reached.
ENXIO The named object is a block device with no filesystem mounted on it.
ENOSPC The file was to be created, and the filesystem involved is full.
EINVAL flags contained invalid values.
EIO A hard I/O error occurred.
EFAULT filename was an invalid pointer.