Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>sopen() open a file for file sharing</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 sopen()                 Open A File for File Sharing

 #include   <fcntl.h>
 #include   <sys\types.h>
 #include   <sys\stat.h>
 #include   <share.h>
 #include   <io.h>                       Required for declarations only

 int        sopen(pathname,oflag,shflag[,pmode]);
 char       *pathname;                   File path name
 int        oflag;                       Type of operations allowed
 int        shflag;                      Type of sharing allowed
 int        pmode;                       Permission setting

    sopen() opens a file for shared reading or writing. The file is
    specified by 'pathname'; read/write operations are controlled by
    'oflag'; and the sharing mode is defined by 'shflag'.

    'oflag' is formed by combining one or more of the following
    constants:

       O_APPEND     Move file pointer to end-of-file before every write
                    operation.
        O_CREAT     Create a new file and open it for writing; no effect
                    if 'pathname' refers to an existing file (see 'pmode'
                    below).
         O_EXCL     Used with O_CREAT; returns an error value if
                    'pathname' already exists.
       O_RDONLY     Open with read access only; cannot be used with
                    O_RDWR or O_WRONLY.
         O_RDWR     Open with read/write access; cannot be used with
                    O_RDONLY or O_WRONLY.
        O_TRUNC     Open an existing file (which must have write
                    permission) and truncate to 0 length. Warning: File
                    contents are destroyed.
       O_WRONLY     Open with write access; cannot be used with O_RDONLY
                    or O_RDWR.
       O_BINARY     Open in binary (untranslated) mode. (See FOPEN).
         O_TEXT     Open in text (translated) mode. (See FOPEN).

    'shflag' is set by using one of the following constants:

            SH_COMPAT:   Compatibility mode.
            SH_DENYRW:   Read and write access denied.
            SH_DENYWR:   Write access denied.
            SH_DENYRD:   Read access denied.
            SH_DENYNO:   Read and write access permitted.

    The 'pmode' argument specifies the permission setting for a newly
    created file; it is needed only when O_CREAT is used.  'pmode' is set
    by using one of the following constants:

            S_IWRITE:             Write permission.
            S_IREAD:              Read permission.
            S_IREAD|S_IWRITE:     Read and write permission.

    Returns:    A handle for the opened file. On error, -1 is returned
                and 'errno' is set to one of the following:

         EACCES     Tried to open a directory; tried to open a read-only
                    file for writing; or a sharing violation.
         EEXIST     Set when O_CREAT and O_EXCL are used but the named
                    file already exists.
         EINVAL     SHARE.COM (or SHARE.EXE) not installed, or an invalid
                    'shrflag' specified.
         EMFILE     Too many open files.
         ENOENT     File name or path name not found.

      Notes:    O_TRUNC destroys the entire contents of an existing file.
                Be careful.

                Use sopen() only under MS-DOS version 3.0 or later.

                When a file is opened for sharing, fdopen() should not be
                used to associate the file with a stream; file-sharing
                modes do not work correctly for buffered files.

 Portability:   Applies to MS DOS only.

   -------------------------------- Example ---------------------------------

    The following statements test the current operating system version
    number and take the appropriate action based on the test.  If the
    version is MS-DOS 3.0 or later, the file is open for shared reading
    and writing; otherwise, it is opened for reading and writing.

           #include <fcntl.h>               /*constants defined*/
           #include <sys\types.h>
           #include <sys\stat.h>            /*constants defined*/
           #include <share.h>               /*constants defined*/
           #include <io.h>

           extern unsigned char _osmajor;
           int fhndl;

           main()
           {
               if (_osmajor >= 3)
                 fhndl = sopen("info.dat",O_CREAT,SH_DENYNO,
                                S_IREAD|S_IWRITE);
               else
                 fhndl = open("info.dat",O_CREAT,S_IREAD|S_IWRITE);
           }


See Also: close() creat() fopen() open() umask()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson