<html> <head> <title>open</title> <body bgcolor=#ffffff> <h2 align=center>open</h2> <h4 align=center>OS/161 Reference Manual</h4> <h3>Name</h3> open - open a file <h3>Library</h3> Standard C Library (libc, -lc) <h3>Synopsis</h3> #include <unistd.h><br> <br> int<br> open(const char *<em>filename</em>, int <em>flags</em>);<br> int<br> open(const char *<em>filename</em>, int <em>flags</em>, int <em>mode</em>);<br> <h3>Description</h3> open opens the file, device, or other kernel object named by the pathname <em>filename</em>. The <em>flags</em> argument specifies how to open the file. The optional <em>mode</em> argument is only meaningful in Unix (or if you choose to implement Unix-style security later on) and can be ignored. <p> The flags argument should consist of one of <blockquote><table width=90%> <tr><td>O_RDONLY</td> <td>Open for reading only.</td></tr> <tr><td>O_WRONLY</td> <td>Open for writing only.</td></tr> <tr><td>O_RDWR</td> <td>Open for reading and writing.</td></tr> </table></blockquote> It may also have any of the following flags OR'd in: <blockquote><table width=90%> <tr><td>O_CREAT</td> <td>Create the file if it doesn't exist.</td></tr> <tr><td>O_EXCL</td> <td>Fail if the file already exists.</td></tr> <tr><td>O_TRUNC</td> <td>Truncate the file to length 0 upon open.</td></tr> <tr><td>O_APPEND</td> <td>Open the file in append mode.</td></tr> </table></blockquote> O_EXCL is only meaningful if O_CREAT is also used. <p> O_APPEND causes all writes to the file to occur at the end of file, no matter what gets written to the file by whoever else. (This functionality may be optional; consult your course's assignments.) <p> open returns a file handle suitable for passing to <A HREF=read.html>read</A>, <A HREF=write.html>write</A>, <A HREF=close.html>close</A>, etc. This file handle must be greater than or equal to zero. Note that file handles 0 (STDIN_FILENO), 1 (STDOUT_FILENO), and 2 (STDERR_FILENO) are used in special ways and are typically assumed by user-level code to always be open. <h3>Return Values</h3> On success, open returns a nonnegative file handle. On error, -1 is returned, and <A HREF=errno.html>errno</A> is set according to the error encountered. <h3>Errors</h3> The following error codes should be returned under the conditions given. Other error codes may be returned for other errors not mentioned here. <blockquote><table width=90%> <td width=10%> </td><td> </td></tr> <tr><td>ENODEV</td> <td>The device prefix of <em>filename</em> did not exist.</td></tr> <tr><td>ENOTDIR</td> <td>A non-final component of <em>filename</em> was not a directory.</td></tr> <tr><td>ENOENT</td> <td>A non-final component of <em>filename</em> did not exist.</td></tr> <tr><td>ENOENT</td> <td>The named file does not exist, and O_CREAT was not specified.</td></tr> <tr><td>EEXIST</td> <td>The named file exists, and O_EXCL was specified.</td></tr> <tr><td>EISDIR</td> <td>The named object is a directory, and it was to be opened for writing.</td></tr> <tr><td>EMFILE</td> <td>The process's file table was full, or a process-specific limit on open files was reached.</td></tr> <tr><td>ENFILE</td> <td>The system file table is full, if such a thing exists, or a system-wide limit on open files was reached.</td></tr> <tr><td>ENXIO</td> <td>The named object is a block device with no mounted filesystem.</td></tr> <tr><td>ENOSPC</td> <td>The file was to be created, and the filesystem involved is full.</td></tr> <tr><td>EINVAL</td> <td><em>flags</em> contained invalid values.</td></tr> <tr><td>EIO</td> <td>A hard I/O error occurred.</td></tr> <tr><td>EFAULT</td> <td><em>filename</em> was an invalid pointer.</td></tr> </table></blockquote> </body> </html>