[Previous]
[Contents]
[Next]

sopen()

open a file for shared access

Synopsis:

#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <share.h>
int sopen( const char *filename,
           int access, int share, ... );

Description:

The sopen() function opens a file at the operating system level for shared access. The name of the file to be opened is given by filename. The file can be accessed according to the access mode specified by access.

The sharing mode of the file is given by the share argument. The optional argument is the file permissions to be used when O_CREAT flag is on in the access mode; you must provide this when the file is to be created.

The access mode is established by a combination of the bits defined in the <fcntl.h> header file. The following bits may be set:

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
causes each record that is written to be written at the end of the file.
O_CREAT
has no effect when the file indicated by filename already exists; otherwise, the file is created.
O_TRUNC
causes the file to be truncated to contain no data when the file exists; has no effect when the file doesn't exist.
O_EXCL
indicates that this file is to be opened for exclusive access. If the file exists and O_CREAT was also specified then the open will fail (that is, use O_EXCL to ensure that the file doesn't already exist).

O_CREAT must be specified when the file doesn't exist.

When the file is to be created (O_CREAT is specified), an additional argument must be passed that contains the file permissions to be used for the new file, specified as a combination of bits. These are defined in the <sys/stat.h> header file, and are described in the Header Files chapter.

The sopen() function applies the current file permission mask to the specified permissions (see umask()).

The shared access for the file, share, is established by a combination of bits defined in the <share.h> header file. The following values may be set:

SH_COMPAT
Set compatibility mode.
SH_DENYRW
Prevent read or write access to the file.
SH_DENYWR
Prevent write access to the file.
SH_DENYRD
Prevent read access to the file.
SH_DENYNO
Permit both read and write access to the file.

Note that

    open( path, oflag, ... );

is the same as:

    sopen( path, oflag, SH_COMPAT, ... );

Note: The sopen() function ignores advisory locks that may have been set by the fcntl(), lock(), or locking() functions.

Returns:

A descriptor for the file. If an error occurs while opening the file, -1 is returned and errno indicates the type of error detected.

Errors:

EACCES
Access was denied because path specifies a directory, or sharing mode was denied due to a conflicting open.
EMFILE
No more descriptors available (too many open files)
ENOENT
Path or file not found

Examples:

#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <share.h>

int main( void )
  {
    int filedes ;

    /* open a file for output               */
    /* replace existing file if it exists      */

    filedes = sopen( "file",
        O_WRONLY | O_CREAT | O_TRUNC,
        SH_DENYWR,
        S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP );

    /* read a file which is assumed to exist   */

    filedes = sopen( "file", O_RDONLY, SH_DENYWR );

    /* append to the end of an existing file   */
    /* write a new file if file does not exist */

    filedes = sopen( "file",
        O_WRONLY | O_CREAT | O_APPEND,
        SH_DENYWR,
        S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP );
    return( EXIT_SUCCESS );
  }

Classification:

WATCOM

Safety:
Interrupt handler No
Signal handler No
Thread Yes

See also:

chsize(), close(), creat(), dup(), dup2(), errno, eof(), exec... functions, fcntl(), filelength(), fileno(), fstat(), isatty(), lseek(), open(), read(), setmode(), stat(), tell(), umask(), write()


[Previous]
[Contents]
[Next]