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 access, int share, ... );
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 will 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_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, which contains the file permissions to be used for the new file. The access permissions for the file or directory are specified as a combination of bits. These are defined in the sys/stat.h header file, and are described in “Header Files in /usr/include/sys” of the C Library Overview 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:
Note that
open( path, oflag, ... );
is the same as:
sopen( path, oflag, SH_COMPAT, ... );
If successful, sopen() returns a descriptor for the file. When an error occurs while opening the file, -1 is returned. When an error has occurred, errno contains a value that indicates the type of error that has been detected.
chsize(), close(), creat(), dup(), dup2(), errno, eof(), exec... functions, fcntl(), filelength(), fileno(), fstat(), isatty(), lseek(), open(), read(), setmode(), stat(), tell(), umask(), write()
#include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <share.h> void main() { 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 ); }
WATCOM
All (except Netware, DOS/PM)