NAME

zkOpen - open a file

SYNOPSIS

#include "zkFcntl.h"

int zkOpen(const char* path, int oflag, ... );

DESCRIPTION

The zkOpen() function establishes the connection between a regular file and a file descriptor. The file descriptor is used by other I/O functions to refer to that file. The path argument points to a pathname naming the file.

The file offset used to mark the current position within the file is set to the beginning of the file.

The file status flags and file access modes of the open file description will be set according to the value of oflag.

Values for oflag are constructed by a bitwise-inclusive-OR of flags from the following list, defined in "zkFcntl.h". Applications must specify exactly one of the first three values (file access modes) below in the value of oflag:

O_RDONLY
Open for reading only.
O_WRONLY
Open for writing only.
O_RDWR
Open for reading and writing.

Any combination of the following may be used:

O_APPEND
If set, the file offset will be set to the end of the file upon opening the file.
O_CREAT
If the file exists, this flag has no effect except as noted under O_EXCL below. Otherwise, the file is created; the user ID of the file is set to the effective user ID of the process; the group ID of the file is set to  the group ID of the file's parent directory or to the effective group ID of the process; and the access permission bits (see "zkSysStat.h") of the file mode are set to the value of the third argument taken as type mode_t modified as follows: a bitwise-AND is performed on the file-mode bits and the corresponding bits in the complement of the process' file mode creation mask. Thus, all bits in the file mode whose corresponding bit in the file mode creation mask is set are cleared. When bits other than the file permission bits are set, the effect is unspecified. The third argument does not affect whether the file is open for reading, writing or for both.
O_EXCL
If O_CREAT and O_EXCL are set, zkOpen() will fail if the file exists. The check for the existence of the file and the creation of the file if it does not exist may be atomic with respect to other processes executing zkOpen() naming the same filename in the same directory with O_EXCL and O_CREAT set. If O_CREAT is not set, the effect is undefined.
O_TRUNC
If the file exists and is a regular file, and the file is successfully opened O_RDWR or O_WRONLY, its length is truncated to 0 and the mode and owner are unchanged. It will have no effect on FIFO special files or terminal device files. Its effect on other file types is implementation-dependent. The result of using O_TRUNC with O_RDONLY is undefined.

If O_CREAT is set and the file did not previously exist, upon successful completion, zkOpen() will mark for update the st_ctime and st_mtime fields of the file.

The largest value that can be represented correctly in an object of type off_t will be established as the offset maximum in the open file description.

 RETURN VALUE

Upon successful completion, the function will open the file and return a non-negative integer representing the file descriptor. Otherwise, -1 is returned and errno is set to indicate the error. No files will be created or modified if the function returns -1.

 ERRORS

The zkOpen() function will fail if:

[EACCES]
Search permission is denied on a component of the path prefix, or the file exists and the permissions specified by oflag are denied, or the file does not exist and write permission is denied for the parent directory of the file to be created, or O_TRUNC is specified and write permission is denied.
[EEXIST]
O_CREAT and O_EXCL are set, and the named file exists.
[EINTR]
A signal was caught during zkOpen().
[EINVAL]
The implementation does not support synchronised I/O for this file.
[EIO]
The path argument names a STREAMS file and a hangup or error occurred during the zkOpen().
[EISDIR]
The named file is a directory and oflag includes O_WRONLY or O_RDWR.
[ELOOP]
Too many symbolic links were encountered in resolving path.
[EMFILE]
Too many file descriptors are currently open in the calling process.
[ENAMETOOLONG]
The length of the path argument exceeds an implementation-defined limit or a pathname component is longer than an implementation-defined limit.
[ENFILE]
The maximum allowable number of files is currently open in the system.
[ENOENT]
O_CREAT is not set and the named file does not exist; or O_CREAT is set and either the path prefix does not exist or the path argument points to an empty string.
[ENOSR]
The path argument names a STREAMS-based file and the system is unable to allocate a STREAM.
[ENOSPC]
The directory or file system that would contain the new file cannot be expanded, the file does not exist, and O_CREAT is specified.
[ENOTDIR]
A component of the path prefix is not a directory.
[ENXIO]
O_NONBLOCK is set, the named file is a FIFO, O_WRONLY is set and no process has the file open for reading.
[ENXIO]
The named file is a character special or block special file, and the device associated with this special file does not exist.
[EOVERFLOW]
The named file is a regular file and the size of the file cannot be represented correctly in an object of type off_t.
[EROFS]
The named file resides on a read-only file system and either O_WRONLY, O_RDWR, O_CREAT (if file does not exist) or O_TRUNC is set in the oflag argument.

The zkOpen() function may fail if:

[EAGAIN]
The path argument names the slave side of a pseudo-terminal device that is locked.
[EINVAL]
The value of the oflag argument is not valid.
[ENAMETOOLONG]
Pathname resolution of a symbolic link produced an intermediate result whose length exceeds an implementation-defined limit.
[ENOMEM]
The path argument names a STREAMS file and the system is unable to allocate resources.
[ETXTBSY]
The file is a pure procedure (shared text) file that is being executed and oflag is O_WRONLY or O_RDWR.

PORTABILITY

The zkOpen() function may portably be used only to open regular files. Any attempt to open a directory, FIFO, STREAMS file, terminal device file, character special file, or block special file is non-portable.

The contents of the path argument to the zkOpen() function are platform-dependent. In particular, directory separators (eg, "/") and directory navigation specifications (eg, "../") are entirely platform-dependent.

Assumptions about the existence of certains directories (eg, "/tmp") or devices (eg, "/dev/null") are also non-portable.

The atomicity of opening a file with the O_CREAT and O_EXCL flags set depends on the platform. In particular, this operation will be atomic on UNIX and Linux platforms.

The precise semantics associated with the terms "user ID", "effective user ID", "group ID", "effective group ID", and "access permission bits" are platform-dependent.