OPEN(2) COMMAND REFERENCE OPEN(2)
NAME
open - open a file for reading or writing, or create a new
file
SYNOPSIS
#include <sys/file.h>
fd = open(path, flags, mode)
int fd;
char *path;
int flags, mode;
DESCRIPTION
The command open opens the file named by path as specified
by the flags argument and returns a descriptor for that file
in fd.
The flags argument may indicate the file is to be created if
it does not already exist (by specifying the O_CREAT flag),
in which case the file is created with mode mode as
described in chmod(2) and modified by the process' umask
value (see umask(2)). Refer to the CAVEATS section for
specifics on setting modes.
The path name path is the address of a string of ASCII
characters representing a path name, terminated by a null
character; flags is constructed by or'ing the following
values, defined in <sys/file.h>:
O_RDONLY Open for reading only.
O_WRONLY Open for writing only.
O_RDWR Open for reading and writing.
O_NDELAY Do not block on open.
If the open call would result in the process being
blocked for some reason (e.g., waiting for carrier
on a dialup line), the open returns immediately.
O_APPEND Append on each write.
If set, the file pointer is set to the end-of-file
prior to each write.
O_CREAT Create file if it does not exist. When a file is
created, the owner of the file is the effective
user-ID of the creating process, and the group is
the group of the file's parent directory.
O_TRUNC Truncate size to 0.
Printed 4/6/89 1
OPEN(2) COMMAND REFERENCE OPEN(2)
If the file exists, it is truncated to zero
length.
O_EXCL Error if create and file exists.
If O_EXCL and O_CREAT are set, open fails if the
file exists.
Upon successful completion a non-negative integer fd, termed
a file descriptor, is returned. The file pointer used to
mark the current position within the file is set to the
beginning of the file.
The new descriptor is set to remain open across execve
system calls; see close(2).
There is a limit on the number of file descriptors a process
may have open simultaneously. This number is NOFILE,
defined in <sys/max.h>. The getdtablesize(2) call returns
the current value of NOFILE.
DIAGNOSTICS
The named file is opened unless one or more of the following
are true:
[ENAMETOOLONG] The argument path is too long.
[ENOASCII] The path name contains a character with the
high-order bit set.
[ENOTDIR] A component of the path prefix is not a
directory.
[ENOENT] O_CREAT is not set and the named file does
not exist.
[EACCES] A component of the path prefix denies search
permission.
[EACCES] The required permissions (for reading and/or
writing) are denied for the named flag.
[EISDIR] The named file is a directory, and the
arguments specify it is to be opened for
writing.
[EROFS] The named file resides on a read-only file
system, and the file is to be modified.
[EMFILE] NOFILE files are currently open (see
getdtablesize(2)).
Printed 4/6/89 2
OPEN(2) COMMAND REFERENCE OPEN(2)
[ENXIO] The named file is a character special or
block special file, and the device associated
with this special file does not exist.
[ETXTBSY] The file is a pure procedure (shared text)
file that is being executed and the open call
requests write access.
[EFAULT] Path points outside the process's allocated
address space.
[ELOOP] Too many symbolic links were encountered in
translating the path name.
[EEXIST] O_EXCL is specified and the file exists.
[ENOSPC] O_CREAT is specified, and the file system is
out of inodes.
[ENOSPC] The directory in which the entry for the new
file is being placed cannot be extended
because there is no space left on the file
system containing the directory, the file
does not exist and O_CREAT is specified.
[ENFILE] O_CREAT is specified, and the system inode
table is full.
[EIO] An I/O error occurred while reading from or
writing to the file system.
[ENXIO] The O_NDELAY flag is given, and the file is a
communications device on which their is no
carrier present.
[EBUSY] An exclusively-opened port is already opened.
[EOPNOTSUPP] An attempt is made to open a socket (not
currently implemented).
RETURN VALUE
If no error occurred, open returns the file descriptor in
fd. Otherwise, -1 is returned and errno is set to indicate
the error.
CAVEATS
The command open will not allow a superuser to set a mode
including the sticky-bit (save text image after execution);
this prevents unreasonably large files from being created
within another process. (For example, the command
open(name,O_CREAT,01777) results in the mode being set to
0777 instead of 01777.)
Printed 4/6/89 3
OPEN(2) COMMAND REFERENCE OPEN(2)
SEE ALSO
chmod(2), close(2), dup(2), getdtablesize(2), lseek(2),
read(2), write(2), umask(2), and unlink(2).
Printed 4/6/89 4
%%index%%
na:240,120;
sy:360,944;
de:1304,1951;3567,872;
di:4439,1179;5930,1647;
rv:7577,321;
ca:7898,458;
se:8668,358;
%%index%%000000000143