[Previous] [Contents] [Next]

openfd()

Reserve a file descriptor

Synopsis:

#include <unistd.h>

int openfd( int fd,
            int oflag );

Library:

libc

Description:

The openfd() function opens the file associated with the file descriptor, fd. This is similar to dup(), except the new fd has private access modes and offset. The access mode, oflag, must be equal to or more restrictive than the access mode of the source fd.

The oflag argument specifies how the file is to be opened, and is a combination of the following bits:

O_RDONLY
Permit the file to be only read.
O_WRONLY
Permit the file to be only written.
O_RDWR
Permit the file to be both read and written.
O_APPEND
Cause each record that is written to be written at the end of the file.
O_TRUNC
Causes the file to be truncated to contain no data.

Returns:

A file descriptor, or -1 if an error occurred (errno is set).

Errors:

EBADF
Invalid file descriptor fd.
EACCES
The access mode specified by oflag isn't equal to or more restrictive than the access mode of the source fd.
EBUSY
Sharing mode (sflag) was denied due to a conflicting open (see sopenfd()).

Examples:

#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>

int main ( void )
{
    int fd, fd2, oflag;

    fd = open ("n/etc/passwd", O_RDONLY);
    fd2 = openfd ( fd, O_RDONLY );
    return EXIT_SUCCESS;
}

Classification:

QNX 6

Safety:
Cancellation point Yes
Interrupt handler No
Signal handler Yes
Thread Yes

See also:

dup(), sopenfd()


[Previous] [Contents] [Next]