open(), open64()

open a file 



#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,...);


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:


Open for reading only.


Open for writing only.


Open for both reading and writing.

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


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


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.


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


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.


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.


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


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.


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.


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.


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.


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.


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


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.


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


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.



Is the name of the file to open.


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


Specifies what permissions the file has when it is created.


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.


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.


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


The pathname parameter is not a valid pointer.


The call was interrupted by a signal.


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


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


This process is currently using too many open file descriptors.


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


Too many files are currently open within the system.


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.


The system is unable to allocate memory for the descriptor.


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


A component of the pathname prefix is not a directory.


O_NONBLOCK is set, the named file is a FIFO, O_WRONLY is set, and no process has the file open for reading.


The named file is a regular file and the size of the file cannot be represented correctly in an object of type off_t.


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.


POSIX.1 (1996), with exceptions.

open64(): Large File Specification, revision 1.5.




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.


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


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

lf64, struct stat

PTC MKS Toolkit 10.4 Documentation Build 39.