Open a file for shared access


#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,
           ... );


The path name of the file that you want to open.
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).
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 — 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.

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.



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


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().


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


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.
Sharing mode (share) was denied due to a conflicting open.
The O_CREAT and O_EXCL flags are set, and the named file exists.
The named file is a directory, and the oflag argument specifies write-only or read/write access.
Too many levels of symbolic links or prefixes.
All file descriptors available to the process are currently open.
Path or file not found.
The sopen() function isn't implemented for the filesystem specified in path.


#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",
        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",
        S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP );
    return EXIT_SUCCESS;



Cancellation point Yes
Interrupt handler No
Signal handler No
Thread Yes