open(), open64()

open a file 

Function


SYNOPSIS

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

int open(const char *pathname, int oflag,...);

int open64(const char *pathname, int oflag,...);


DESCRIPTION

The open() function establishes the connection between a file and a file descriptor. It creates an open file description that refers to a file and a file descriptor that refers to the open file description. The file descriptor is used by other I/O functions to refer to that file.

The open() function returns a file descriptor for the named file that is the lowest file descriptor not currently open for that process. The open file description is new, and therefore the file descriptor does not share it with any other process in the system. The FD_CLOEXEC file descriptor flag associated with the new file descriptor is cleared.

The open64() function is a part of the large file extensions, and is equivalent to calling open() with the O_LARGEFILE flag.

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 are set according to the value of oflag. The value of oflag is the bitwise inclusive-OR of values from the following lists.

Applications must specify exactly one of the following three values (file access modes) in the value of oflag:

O_RDONLY 

Open for reading only.

O_WRONLY 

Open for writing only.

O_RDWR 

Open for both reading and writing.

Any combination of the following file status flags may be specified as well:

O_APPEND 

If set, the file offset is set to the end of the file prior to each write.

O_ASYNC 

If the specified path refers to a communications device, enables delivery of SIGIO signals when data is available on the device. Refer to /dev/com for more information. This flag is ignored for other types of file.

O_BINARY 

If set, causes the file to be opened in binary mode. Refer to PORTING ISSUES for more information.

O_CREAT 

If the file exists, this flag has no effect except as noted under O_EXCL. Otherwise, the file is created; the user ID of the file is set to the effective user ID of the process; the group ID is set to the effective group ID of the process; and the access permission bits 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. This, 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. Refer to chmod(), umask(), and struct stat for more information on the mode bits. Upon successful completion, open() marks for update the st_atime, st_ctime and st_mtime fields of the file.

O_EXCL 

If O_CREAT and O_EXCL are set, open() fails if the file exists. The check for the existence of the file and the creation of the file if it does not exists is atomic with respect to other processes executing open() naming the same file name in the same directory with O_EXCL and O_CREAT set. If O_CREAT is not set, the effect is undefined.

O_LARGEFILE 

Sets the file descriptor to indicate a large file aware application.

O_NOCTTY 

If set and the specified path refers to a terminal device, open() does not cause the terminal to become the controlling terminal for the process. This flag is currently ignored by the NuTCRACKER Platform.

O_NONBLOCK 

When opening a FIFO with O_RDONLY or O_WRONLY set:

  • If O_NONBLOCK is set, then an open() for reading only returns without delay, and an open() for writing only returns an error if no process currently has the file open for reading. If O_NONBLOCK is clear, then an open() for reading only blocks the calling thread until a thread opens the file for writing, and an open() for writing only blocks the calling thread until a thread opens the file for reading.

When opening a device that supports non-blocking opens (for example, communications ports):

  • If O_NONBLOCK is set, the open() function returns without blocking for the device to be ready or available. Subsequent behavior of the device is device-specific. If O_NONBLOCK is clear, the open() function blocks the calling thread until the device is ready or available before returning.

Otherwise, the behavior of O_NONBLOCK is unspecified.

O_RANDOM 

If set, indicates that the file is accessed randomly. The system can use this as a hint to optimize file caching. This corresponds to the Win32 flag FILE_FLAG_RANDOM_ACCESS.

O_SEQUENTIAL 

If set, indicates that the file is accessed sequentially from beginning to end. The system can use this as a hint to optimize file caching. This corresponds to the Win32 flag FILE_FLAG_SEQUENTIAL_SCAN.

O_SHORT_LIVED 

If set, indicates that the file is being used for temporary storage. File systems try to keep all of the file in memory for quicker access rather than flushing the data back to mass storage. This corresponds to the Win32 flag FILE_ATTRIBUTE_TEMPORARY.

O_SYNC 

If set, write I/O operations on the file descriptor complete only when the data has actually be written to the mass storage device.

O_TEMPORARY 

If set, the file is deleted when the last file descriptor referring to it is closed. This corresponds to the Win32 flag FILE_FLAG_DELETE_ON_CLOSE.

O_TEXT 

If set, causes the file to be opened in text mode. Refer to PORTING ISSUES for more information.

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 has no affect on FIFO special files or terminal device files. Its affect on other file types is undefined. The result of using O_TRUNC with O_RDONLY is undefined.


PARAMETERS

pathname 

Is the name of the file to open.

oflag 

Is the bitwise inclusive-OR of the file access modes and file status flags, as defined in the DESCRIPTION section.

mode 

Specifies what permissions the file has when it is created.


RETURN VALUES

If successful, open() and open64() open the file and return a non-negative integer that represents the lowest numbered unused file descriptor. If failed, they return a value of -1, do not create nor modify any files, and set errno to indicate the error.

EACCES 

The file does not exist and the parent directory denies write permission of the file to be created.

A component of the path name prefix denies search permission.

The file exists and the permissions specified by oflag are denied.

O_TRUNC is specified and write permission is denied.

EEXIST 

O_CREAT and O_EXCL are set, and the named file exists.

EFAULT 

The pathname parameter is not a valid pointer.

EINTR 

The call was interrupted by a signal.

EINVAL 

The oflag parameter contains invalid flags, or flags specified in oflag are invalid for pathname.

EISDIR 

The oflag parameter specifies O_WRONLY or O_RDWR and the named file is a directory.

EMFILE 

This process is currently using too many open file descriptors.

ENAMETOOLONG 

The pathname parameter string length exceeds PATH_MAX or a path name component is longer than NAME_MAX.

ENFILE 

Too many files are currently open within the system.

ENOENT 

O_CREAT is not set and the named file does not exist.

O_CREAT is set and either the pathname prefix does not exist or the pathname parameter points to an empty string.

ENOMEM 

The system is unable to allocate memory for the descriptor.

ENOSPC 

O_CREAT is set, and the directory that would contain the file cannot be extended.

ENOTDIR 

A component of the pathname 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.

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; if the file does not exist, either O_WRONLY, O_RDWR, O_CREAT, or O_TRUNC is set in the oflag parameter.


CONFORMANCE

POSIX.1 (1996), with exceptions.

open64(): Large File Specification, revision 1.5.


MULTITHREAD SAFETY LEVEL

Async-signal-safe.


PORTING ISSUES

The NuTCRACKER Platform supports open() and open64() on directories in read-only mode and the resulting file descriptor can be used only for certain information operations such as fpathconf() and fstat(). File operations such as read(), select() locking with fcntl(), and lseek() fail with the errno set to EISDIR.

Refer to Security and File Management in the Windows Concepts chapter of the PTC MKS Toolkit UNIX to Windows Porting Guide for a detailed discussion of file security and file handling issues, such as text and binary mode.

The test which causes the EOVERFLOW error condition to be returned can be disabled by using the _NutConf() function to turn off the _NC_SET_FULL_LFS_SEMANTICS flag.

File names are treated as multibyte sequences and are converted to Unicode (UTF-16) before passing to Win32. The conversion is either performed based on the current thread locale, set using uselocale() or the process locale as set by a call to setlocale(), and overridden by _NutConf() _NC_SET_ANSI_LOCALE and _NC_SET_UTF8_LOCALE options. Under all other conditions, the multibyte sequences are considered to be from the ANSI code page for the current windows system locale.


AVAILABILITY

PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Functions:
chmod(), close(), creat(), dup(), dup2(), fcntl(), fpathconf(), fstat(), lseek(), read(), select(), stat(), umask(), write(), xlocale()

Miscellaneous:
lf64, struct stat


PTC MKS Toolkit 10.5 Documentation Build 40.