![]() |
![]() |
![]() |
![]() |
![]() |
This version of this document is no longer maintained. For the latest documentation, see http://www.qnx.com/developers/docs. |
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, ... );
If you set O_CREAT in oflag, you must also specify the following argument:
libc
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_COMPAT, ... );
![]() |
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).
#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; }
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
chsize(), close(), creat(), dup(), dup2(), eof(), execl(), execle(), execlp(), execlpe(), execv(), execve(), execvp(), execvpe(), fcntl(), fileno(), fstat(), isatty(), lseek(), open(), read(), stat(), tell(), umask(), write()
![]() |
![]() |
![]() |
![]() |