www.delorie.com/djgpp/doc/libc/libc_600.html   search  
libc.a reference

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

open

Syntax

 
#include <fcntl.h>
#include <sys/stat.h> /* for mode definitions */

int open(const char *file, int mode /*, int permissions */);

Description

This function opens the named file in the given mode, which is any combination of the following bits:

O_RDONLY

The file is opened for reading.

O_WRONLY

The file is opened for writing.

O_RDWR

The file is opened for both reading and writing.

O_CREAT

If the file does not exist, it is created. See section creat.

O_TRUNC

If the file does exist, it is truncated to zero bytes.

O_EXCL

If the file exists, and O_CREAT is also specified, the open call will fail. If the file is a symlink and O_CREAT is also specified, then the contents of the symlink are ignored - the open call will fail.

O_APPEND

The file pointer is positioned at the end of the file before each write.

O_TEXT

The file is opened in text mode, meaning that Ctrl-M characters are stripped on reading and added on writing as needed. The default mode is specified by the _fmode variable _fmode.

O_BINARY

The file is opened in binary mode.

When called to open the console in binary mode, open will disable the generation of SIGINT when you press Ctrl-C (Ctrl-Break will still cause SIGINT), because many programs that use binary reads from the console will also want to get the `^C' characters. You can use the __djgpp_set_ctrl_c library function (see section __djgpp_set_ctrl_c) if you want Ctrl-C to generate interrupts while console is read in binary mode.

O_NOINHERIT

Child processes will not inherit this file handle. This is also known as close-on-exec--see fcntl. This bit is DOS- and Windows-specific; portable programs should use fcntl instead.

O_NOFOLLOW

open will fail with errno set to ELOOP, if the last path component in file is symlink.

O_NOLINK

If file is a symlink, open will open symlink file itself instead of referred file.

O_TEMPORARY

Delete file when all file descriptors that refer to it are closed.

Note that file should not also be opened with the low-level functions _creat, _creatnew, _dos_creat, _dos_creatnew, and _dos_open. Otherwise file may not be deleted as expected.

If the file is created by this call, it will be given the read/write permissions specified by permissions, which may be any combination of these values:

S_IRUSR

The file is readable. This is always true for MS-DOS.

S_IWUSR

The file is writable.

Other S_I* values may be included, but they will be ignored.

You can specify the share flags (a DOS specific feature) in mode. And you can indicate default values for the share flags in __djgpp_share_flags. See section __djgpp_share_flags.

You can open directories using open, but there is limited support for POSIX file operations on directories. In particular, directories cannot be read using read (see section read) or written using write (see section write). The principal reason for allowing open to open directories is to support changing directories using fchdir (see section fchdir). If you wish to read the contents of a directory, use the opendir (see section opendir) and readdir (see section readdir) functions instead. File descriptors for directories are not inherited by child programs.

Return Value

If successful, the file descriptor is returned. On error, a negative number is returned and errno is set to indicate the error.

Portability

ANSI/ISO C No
POSIX 1003.2-1992; 1003.1-2001

Example

 
int q = open("/tmp/foo.dat", O_RDONLY|O_BINARY);


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2004   by DJ Delorie     Updated Apr 2004