sopen

Opens a file or stream for shared access

Local Servers:blocking
Remote Servers:blocking
Platform:NLM
Service:Operating System I/O

Syntax

  #include <nwfileio.h>  
   
  int sopen  (  
     const char   *path,   
     int           oflag,   
     int           shflag,   
     ...);
  

Parameters

path
(IN) Points to the path of the file to open.
oflag
(IN) Specifies the access mode.
shflag
(IN) Specifies the sharing mode of the file.

Return Values

When an error occurs while opening the file, a value of –1 is returned. Otherwise, an integer (not equal to –1), known as the file handle, is returned to be used with the other functions that operate on the file.

When an error occurs, errno is set to:

Decimal

Constant

Description

1

ENOENT

No such file.

6

EACCESS

Permission denied.

9

EINVAL

Invalid argument.

When an error occurs, NetWareErrno is set to:

Decimal

Hex

Constant

107

(0x6B)

ERR_BAD_SHFLAG

108

(0x6C)

ERR_BAD_ACCESS

152

(0x98)

ERR_INVALID_VOLUME

156

(0x9C)

ERR_INVALID_PATH

NOTE:A problem was introduced in the 4.01d version of clib.nlm. It manifests itself when sopen is called multiple times by the same thread.

In clib.nlm versions prior to 4.01d, a valid file handle will be returned from sopen when a file is opened multiple times by the same thread. In the 4.01d version of clib.nlm (and later versions) an error occurs and returns -1 while errno is set to EINUSE.

Beginning with version 4.10d (notice this is 4.1 and not 4.01) PTR 2/1/95, you can pass the NWSH_PRE_401D_COMPAT bit (0x80000000) in as part of the share flags passed to sopen to get functionality of clib versions prior to 4.01d (if you have experienced problems with multiple threads opening the same file multiple times).

As an alternative to the above work around, you can pass in a CLIB_OPT switch on the command line of the .nlm you are using. CLIB_OPT/U86414 will provide the same functionality as passing in the share flags with the additional bits ORed in. This is only available on clib.nlm version 4.10d PTF.

Remarks

This function also works on the DOS partition.

The path of the file to be opened is given by the path parameter. The file is accessed according to the access mode specified by the oflag parameter.

The access mode is established as a combination of the bits defined in the FCNTL.H header file. The following bits can be set:

O_RDONLY

Permits the file to be only read.

O_WRONLY

Permits the file to be only written.

O_RDWR

Permits the file to be both read and written.

O_APPEND

Causes each record that is written to be written at the end of the file.

O_CREAT

Has no effect when the file indicated by the filename parameter already exists; otherwise, the file is created.

O_TRUNC

Causes the file to be truncated to contain no data when the file exists; has no effect when the file does not exist. O_TRUNC must be ORed with write access to truncate a file:

 

 

O_TRUNC | O_RDWR

 

 

O_TRUNC | O_WRONY

O_BINARY

Causes the data to be transmitted unchanged. (Text mode for first-level handles is not supported. In text mode, carriage- return/line-feed pairs are translated to line feeds on input, and line feeds are translated to carriage-return/line-feed pairs on output.)

O_CREAT must be specified when the file does not exist and it is to be written.

The shared access for the file is established by the combination of bits set in the shflag parameter where the following values are defined in NWFATTR.H.

SH_COMPAT

Sets compatibility mode.

SH_DENYRW

Prevents read or write access to the file.

SH_DENYWR

Prevents write access of the file.

SH_DENYRD

Prevents read access to the file.

SH_DENYNO

Permits both read and write access to the file.

NOTE:If a new file is created by this function, the share flag is ignored.

An optional fourth parameter, int permission, is used when the file is to be created (O_CREAT is specified) to set file permissions. File permissions are set according to the value contained in the permission parameter. The access permissions for the file is specified as a combination of bits (defined in the SYS\STAT.H header file).

S_IWRITE

The file is writable.

S_IREAD

The file is readable.

The permission parameter can be specified as S_IWRITE, S_IREAD or S_IWRITE|S_IREAD. Specifying 0 also makes a file both writable and readable.

See Also

close, dup, dup2, open

Example

  #include <errno.h>  
  #include <fcntl.h>  
  #include <stddef.h>  
  #include <stdio.h> 
  #include <stdlib.h>  
  #include <string.h>  
  #include <dirent.h>  
  #include <nwbindry.h>  
  #include <nwfattr.h> 
  #include <nwerrno.h> 
  #include <nwconn.h> 
  #include <nwfileio.h> 
  #include <unistd.h> 
  #define BUFFER_SIZE 512  
  main()  
  {  
     int ccode, handle; 
     errno=0;  
     printf("Login %s\r\n\n", LoginToFileServer("ADMIN",//  
        supervisor for NW 3.x OT_USER,"\x0")?"Failed":"Succeeded");  
     handle=sopen("test.c", O_RDWR, SH_DENYNO, 0);  
     if (handle==-1)  
     {  
        printf("\r%s\r\n\n",strerror(errno));  
        close(handle); 
        Logout(); 
        exit(0);  
     }  
     ccode=lock(handle, 1, 10); 
     if (ccode) 
     { 
        printf("\n***lock failed\n"); 
        printf("r%s\r\n\n", strerror(errno)); 
     } 
     else 
        printf("\n***lock succeeded\n"); 
     getch(); 
     ccode=unlock(handle, 1, 10); 
     if (ccode) 
     { 
        printf("\n***unlock failed\n"); 
        printf("\r%s\r\n\n", strerror(errno)); 
     } 
     else 
        printf("\n***unlock succeeded\n"); 
     close(handle); 
     Logout();  
  }