sopen()

Updated: October 28, 2024

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 oflag, 
           int share,
           ... );

Arguments:

filename
The path name of the file that you want to open.
oflag
Flags that specify the status and access modes of the file. This argument is a combination of the following bits (defined in <fcntl.h>):
  • 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's written to be written at the end of the file.
  • O_CREAT — create the file if it doesn't exist. This bit has no effect if the file already exists.
  • O_TRUNC — truncate the file to contain no data if the file exists; this bit has no effect if the file doesn't exist.
  • O_EXCL — open the file for exclusive access. If the file exists and you also specify O_CREAT, the open fails (that is, use O_EXCL to ensure that the file doesn't already exist).
share
The shared access for the file. This is a combination of the following bits (defined in <share.h>):
  • SH_COMPAT — this flag is ignored in the QNX implementation.
  • SH_DENYRW — fail this open if the file is already open for read or write.
  • SH_DENYWR — fail this open if the file is already open for write.
  • SH_DENYRD — fail this open if the file is already open for read.
  • SH_DENYNO — ignore existing opens of the file.

If you set O_CREAT in oflag, you must also specify the following argument:

mode_t mode
An object of type mode_t that specifies the access mode that you want to use for a newly created file. For more information, see the entry for struct stat.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

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 is accessed according to the access mode specified by oflag. You must specify O_CREAT if the file doesn't exist.

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 oflag mode; you must provide this when the file is to be created.

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

Note that:

open( path, oflag, ... );

is the same as:

sopen( path, oflag, SH_DENYNO, ... );
Note: The sopen() function ignores advisory locks that you may have set by calling fcntl().

Returns:

A descriptor for the file, or -1 if an error occurs while opening the file (errno is set).

Errors:

EACCES
Search permission is denied on a component of the path prefix, or the file exists and the permissions specified by oflag are denied, or the file doesn't exist and write permission is denied for the parent directory of the file to be created.
EBUSY
Sharing mode (share) was denied due to a conflicting open.
EEXIST
The O_CREAT and O_EXCL flags are set, and the named file exists.
EISDIR
The named file is a directory, and the oflag argument specifies write-only or read/write access.
ELOOP
Too many levels of symbolic links or prefixes.
EMFILE
All file descriptors available to the process are currently open.
ENOENT
Path or file not found.
ENOSYS
The sopen() function isn't implemented for the filesystem underlying the path specified in path.

Examples:

#include <stdlib.h>
#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 doesn't exist */

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

Classification:

Unix

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