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 - <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 the file 'pathname' for shared reading or writing, as
    defined by 'oflag' and 'shflag'.

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

        O_APPEND    Reposition the file pointer to end-of-file before
                    every write operation.
        O_CREAT     Create and open a new file for writing.
        O_EXCL      Used with O_CREAT to return an error value if
                    'pathname' already exists.
        O_RDONLY    Open file for reading only.
        O_RDWR      Open file for both reading and writing.
        O_TRUNC     Open and truncate an existing file to 0 length. The
                    file must have write permission; the contents of the
                    file are destroyed.
        O_WRONLY    Open file for writing only.
        O_BINARY    Open file in binary (untranslated) mode. (See FOPEN).
        O_TEXT      Open file in text (translated) mode. (See FOPEN).

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

            SH_COMPAT:   Set compatibility mode.
            SH_DENYRW:   Deny read and write access to file.
            SH_DENYWR:   Deny write access to file.
            SH_DENYRD:   Deny read access to file.
            SH_DENYNO:   Permit read and write access to file.

    The 'pmode' argument is needed only when O_CREAT is used. 'pmode'
    specifies the file's permission setting. 'pmode' is set by using one
    of the following constants:

            S_IWRITE:             Writing permitted.
            S_IREAD:              Reading permitted.
            S_IREAD|S_IWRITE:     Reading and writing permitted.

    Returns:    A file handle for the opened file; or -1 if an error
                occurs.  On error, '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      Used O_CREAT and O_EXCL when the named file already
                    exists.
        EINVAL      SHARE.COM not installed.
        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.

                File sharing modes will not work correctly for buffered
                files.  Do not use fdopen() to associate a file opened
                for sharing with a stream.

  -------------------------------- 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