?? open.html
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>open</title></head><body bgcolor=white><center><font size=2>The Single UNIX ® Specification, Version 2<br>Copyright © 1997 The Open Group</font></center><hr size=2 noshade><h4><a name = "tag_000_007_919"> </a>NAME</h4><blockquote>open - open a file</blockquote><h4><a name = "tag_000_007_920"> </a>SYNOPSIS</h4><blockquote><pre><code>#include <<a href="systypes.h.html">sys/types.h</a>>#include <<a href="sysstat.h.html">sys/stat.h</a>>#include <<a href="fcntl.h.html">fcntl.h</a>>int open(const char *<i>path</i>, int <i>oflag</i>, ... );</code></pre></blockquote><h4><a name = "tag_000_007_921"> </a>DESCRIPTION</h4><blockquote>The<i>open()</i>function establishes the connection between a file and afile descriptor.It creates an open file description that refers to a fileand a file descriptor that refers to that open file description.The file descriptor is used by other I/O functions to refer to that file. The<i>path</i>argument points to a pathname naming the file.<p>The<i>open()</i>function will return a file descriptor for the named filethat is the lowest file descriptor not currently open forthat process.The open file description is new, and therefore the filedescriptor does not share it with any other process in thesystem. The FD_CLOEXEC file descriptor flag associated with the new filedescriptor will be cleared.<p>The file offset used to mark the currentposition within the file is set to the beginning of the file.<p>The file status flags and file access modes of the open filedescription will be set according to the value of<i>oflag</i>.<p>Values for<i>oflag</i>are constructed by a bitwise-inclusive-OR offlags from the following list, defined in<i><a href="fcntl.h.html"><fcntl.h></a></i>.Applications must specify exactly one of the first threevalues (file access modes) below in the value of<i>oflag</i>:<dl compact><dt>O_RDONLY<dd>Open for reading only.<dt>O_WRONLY<dd>Open for writing only.<dt>O_RDWR<dd>Open for reading and writing. The result is undefined if this flag isapplied to a FIFO.</dl><p>Any combination of the following may be used:<dl compact><dt>O_APPEND<dd>If set, the file offset will be set to the end of the fileprior to each write.<dt>O_CREAT<dd>If the file exists, this flag has no effectexcept as noted under O_EXCL below.Otherwise, the file is created; the user IDof the file is set to the effective user IDof 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 IDof the process; and the access permission bits (see<i><a href="sysstat.h.html"><sys/stat.h></a></i>)of the file mode are set to the value ofthe third argument taken as type<b>mode_t</b>modified as follows:a bitwise-AND is performed on the file-mode bits and the correspondingbits in the complement of the process' file mode creation mask.Thus, all bits in the file mode whose corresponding bit in thefile mode creation mask is set are cleared.When bits other than the file permission bits are set, the effect isunspecified. The third argument does not affect whetherthe file is open for reading, writing or for both.<dt>O_DSYNC<dd>Write I/O operations on the file descriptor complete as defined bysynchronised I/O data integrity completion<dt>O_EXCL<dd>If O_CREAT and O_EXCL are set,<i>open()</i>will fail if the file exists.The check for the existence of the file and the creation of thefile if it does not exist will be atomic with respect to otherprocesses executing<i>open()</i>naming the same filename in the same directorywith O_EXCL and O_CREAT set.If O_CREAT is not set, the effect is undefined.<dt>O_NOCTTY<dd>If set and<i>path</i>identifies a terminal device,<i>open()</i>will not cause the terminal device to become the controllingterminal for the process.<dt>O_NONBLOCK<dd>When opening a FIFO with O_RDONLY or O_WRONLY set:If O_NONBLOCK is set:<dl compact><dt> <dd>An<i>open()</i>for reading only will return without delay. An<i>open()</i>for writing only will return an error if no processcurrently has the file open for reading.</dl><p>If O_NONBLOCK is clear:<dl compact><dt> <dd>An<i>open()</i>for reading only will block the calling threaduntil a thread opens the file for writing. An<i>open()</i>for writing only will block the calling threaduntil a threadopens the file for reading.</dl><p>When opening a block special or character specialfile that supports non-blocking opens:<p>If O_NONBLOCK is set:<dl compact><dt> <dd>The<i>open()</i>function will return without blocking for the deviceto be ready or available.Subsequent behaviour of the device is device-specific.</dl><p>If O_NONBLOCK is clear:<dl compact><dt> <dd>The<i>open()</i>function will block the calling threaduntil the device is ready or available before returning.</dl><p>Otherwise, the behaviour of O_NONBLOCK is unspecified.<p><dt>O_RSYNC<dd>Read I/O operations on the file descriptor complete at thesame level of integrity as specified by the O_DSYNC andO_SYNC flags. If both O_DSYNC and O_RSYNC are set in<i>oflag,</i>all I/O operations on the file descriptor complete as defined bysynchronised I/O data integrity completion.If both O_SYNC and O_RSYNC are set in flags, all I/O operations on thefile descriptor complete as defined by synchronised I/O file integritycompletion.<p><dt>O_SYNC<dd>Write I/O operations on the file descriptor complete as defined bysynchronised I/O file integrity completion.<p><dt>O_TRUNC<dd>If the file exists and is a regular file, and the file is successfullyopened O_RDWR or O_WRONLY, its lengthis truncated to 0 and the mode and owner are unchanged. It will have noeffect on FIFO special files orterminal device files. Its effect on other filetypes is implementation-dependent. The result of using O_TRUNC withO_RDONLY is undefined.<p></dl><p>If O_CREAT is set and the file did not previously exist, upon successfulcompletion,<i>open()</i>will mark for update the<i>st_atime,</i><i>st_ctime</i>and<i>st_mtime</i>fields of the file and the<i>st_ctime</i>and<i>st_mtime</i>fields of the parent directory.<p>If O_TRUNC is set and the file did previously exist,upon successful completion,<i>open()</i>will mark for update the<i>st_ctime</i>and<i>st_mtime</i>fields of the file.<p>If both the O_SYNC and O_DSYNCflags are set, the effect is as if only theO_SYNC flag was set.<p>If <i>path</i> refers to a STREAMS file,<i>oflag</i>may be constructed from O_NONBLOCK OR-ed with either O_RDONLY, O_WRONLY orO_RDWR.Other flag values are not applicable to STREAMS devices and have no effect onthem. The value O_NONBLOCK affects the operation of STREAMS drivers andcertain functions applied to file descriptors associated with STREAMS files.For STREAMS drivers, the implementation of O_NONBLOCK is device-specific.<p>If<i>path</i>names the master side of a pseudo-terminal device, then it is unspecifiedwhether<i>open()</i>locks the slave side so that it cannot be opened. Portable applications mustcall<i><a href="unlockpt.html">unlockpt()</a></i>before opening the slave side.<p>The largest value that can be represented correctly in an object oftype<b>off_t</b>will be established as the offset maximum in the open file description. </blockquote><h4><a name = "tag_000_007_922"> </a>RETURN VALUE</h4><blockquote>Upon successful completion, the function will open the fileand return a non-negative integer representing the lowest numberedunused file descriptor.Otherwise, -1 is returned and<i>errno</i>is set to indicate the error.No files will be created or modified if the function returns -1.</blockquote><h4><a name = "tag_000_007_923"> </a>ERRORS</h4><blockquote>The<i>open()</i>function will fail if:<dl compact><dt>[EACCES]<dd>Search permission is denied on a component of the path prefix,or the file exists and the permissions specified by<i>oflag</i>are denied, or the file does not exist and write permission isdenied for the parent directory of the file to be created, orO_TRUNC is specified and write permission is denied.<dt>[EEXIST]<dd>O_CREAT and O_EXCL are set, and the named file exists.<dt>[EINTR]<dd>A signal was caught during<i>open()</i>.<dt>[EINVAL]<dd>The implementation does not support synchronised I/O for this file.<dt>[EIO]<dd>The <i>path</i> argument names a STREAMS file and a hangup or error occurredduring the<i>open()</i>.<dt>[EISDIR]<dd>The named file is a directory and<i>oflag</i>includes O_WRONLY or O_RDWR.<dt>[ELOOP]<dd>Too many symbolic links were encountered in resolving <i>path</i>.<dt>[EMFILE]<dd>{OPEN_MAX}file descriptors are currently open in the calling process.<dt>[ENAMETOOLONG]<dd>The length of the<i>path</i>argument exceeds {PATH_MAX} or a pathname component is longer than{NAME_MAX}.<dt>[ENFILE]<dd>The maximum allowable number of files is currently open in thesystem.<dt>[ENOENT]<dd>O_CREAT is not set and the named file does not exist;or O_CREAT is set and either the path prefix does not existor the<i>path</i>argument points to an empty string.<dt>[ENOSR]<dd>The <i>path</i> argument names a STREAMS-based fileand the system is unable to allocate a STREAM.<dt>[ENOSPC]<dd>The directory or file system that would contain thenew file cannot be expanded, the file does not exist, andO_CREAT is specified.<dt>[ENOTDIR]<dd>A component of the path prefix is not a directory.<dt>[ENXIO]<dd>O_NONBLOCK is set, the named file is a FIFO,O_WRONLY is set and no process hasthe file open for reading.<dt>[ENXIO]<dd>The named file is a character special or block special file,and the device associated with this special file does not exist.<dt>[EOVERFLOW]<dd>The named file is a regular file and the size of the file cannot berepresented correctly in an object of type<b>off_t</b>.<dt>[EROFS]<dd>The named file resides on a read-only file system andeither O_WRONLY, O_RDWR, O_CREAT(if file does not exist) or O_TRUNC is set in the<i>oflag</i>argument.</dl><p>The<i>open()</i>function may fail if:<dl compact><dt>[EAGAIN]<dd>The<i>path</i>argument names the slave side of a pseudo-terminal device that is locked.<dt>[EINVAL]<dd>Thevalue of the<i>oflag</i>argument is not valid.<dt>[ENAMETOOLONG]<dd>Pathname resolution of a symbolic link produced an intermediate result whoselength exceeds {PATH_MAX}.<dt>[ENOMEM]<dd>The <i>path</i> argument names a STREAMS file and the system is unable toallocate resources.<dt>[ETXTBSY]<dd>The file is a pure procedure (shared text) file that is beingexecuted and<i>oflag</i>is O_WRONLY or O_RDWR.</dl></blockquote><h4><a name = "tag_000_007_924"> </a>EXAMPLES</h4><blockquote>None.</blockquote><h4><a name = "tag_000_007_925"> </a>APPLICATION USAGE</h4><blockquote>None.</blockquote><h4><a name = "tag_000_007_926"> </a>FUTURE DIRECTIONS</h4><blockquote>None.</blockquote><h4><a name = "tag_000_007_927"> </a>SEE ALSO</h4><blockquote><i><a href="chmod.html">chmod()</a></i>,<i><a href="close.html">close()</a></i>,<i><a href="creat.html">creat()</a></i>,<i><a href="dup.html">dup()</a></i>,<i><a href="fcntl.html">fcntl()</a></i>,<i><a href="lseek.html">lseek()</a></i>,<i><a href="read.html">read()</a></i>,<i><a href="umask.html">umask()</a></i>,<i><a href="unlockpt.html">unlockpt()</a></i>,<i><a href="write.html">write()</a></i>,<i><a href="fcntl.h.html"><fcntl.h></a></i>,<i><a href="sysstat.h.html"><sys/stat.h></a></i>,<i><a href="systypes.h.html"><sys/types.h></a></i>.</blockquote><h4>DERIVATION</h4><blockquote>Derived from Issue 1 of the SVID.</blockquote><hr size=2 noshade><center><font size=2>UNIX ® is a registered Trademark of The Open Group.<br>Copyright © 1997 The Open Group<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]</font></center><hr size=2 noshade></body></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -