home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c160 / 1.ddi / SOURCE / U4OPEN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-19  |  3.5 KB  |  162 lines

  1.  
  2. /*  u4open.c   (C)Copyright  Sequiter Software Inc., 1987-1990.  All rights reserved.
  3. */
  4.  
  5. #include "d4all.h"
  6. #include "u4error.h"
  7. #include "p4misc.h"
  8.  
  9. #include <fcntl.h>
  10. #include <stdio.h>
  11.  
  12. #ifndef UNIX
  13. #include <io.h>
  14. #endif
  15.  
  16. #ifdef TURBO
  17.    #include <sys\stat.h>
  18.    #include <share.h>
  19.  
  20.    extern unsigned char _osmajor;
  21. #else
  22. #ifndef UNIX
  23.    #include <sys\types.h>
  24.    #include <sys\stat.h>
  25.  
  26.    #ifndef ZORTECH
  27.       #include <share.h>
  28.    #endif
  29.  
  30.    extern unsigned char _osmajor;
  31. #endif
  32. #endif
  33.  
  34.  
  35. /* For Paramter 'open_code' */
  36. #define  OPEN_EXIST   0 
  37. #define  SAFE_CREATE  1 
  38. #define  DO_CREATE    2
  39. #define  OPEN_EXCL    4
  40.  
  41.  
  42. u4open( char *file_name, int open_code ) 
  43. {
  44.    int  hand, oflag, pmode, err_code, disp_err ;
  45.  
  46.    if ( open_code & 0x8 )
  47.    {
  48.       open_code -= 0x8 ;
  49.       disp_err = 0 ;
  50.    }
  51.    else
  52.       disp_err = 1 ;
  53.  
  54.    #ifdef TURBO
  55.       oflag    =  (int) (O_BINARY | O_RDWR) ;
  56.       pmode    =  (int) (S_IREAD  | S_IWRITE) ;
  57.    #else
  58.    #ifdef UNIX
  59.       oflag    =  (int)  O_RDWR ;
  60.       pmode    =   0666 ;  /* Allow complete read and write permissions */
  61.    #else
  62.       oflag    =  (int) (O_BINARY | O_RDWR) ;
  63.       pmode    =  (int) (S_IREAD  | S_IWRITE) ;
  64.    #endif
  65.    #endif
  66.  
  67.    err_code =  (int) E_CREATE ;
  68.  
  69.    switch( open_code )
  70.    {
  71.       case DO_CREATE:
  72.      u4remove( file_name ) ;
  73.      oflag |=  O_CREAT ;
  74.      break ;
  75.  
  76.       case SAFE_CREATE:
  77.          #ifdef ZORTECH
  78.         oflag |=  O_CREAT ;
  79.          #else
  80.         oflag |=  (O_CREAT | O_EXCL) ;
  81.          #endif
  82.  
  83.      #ifdef TURBO
  84.         /* O_EXCL ignored in TURBO C 1.5 - Make sure file does not exist */
  85.         hand =  open( file_name, O_RDWR ) ;
  86.         if ( hand >= 0 )
  87.         {
  88.            /* Generate Error if File Opened !! */
  89.            close( hand ) ;
  90.            if ( disp_err )
  91.               u4error( E_CREATE, file_name, (char *) 0 ) ;
  92.            return( -1 ) ;
  93.         }
  94.      #endif
  95.      break ;
  96.  
  97.       case OPEN_EXCL:
  98.          #ifndef ZORTECH
  99.             oflag |=  O_EXCL ;
  100.          #endif
  101.  
  102.       case OPEN_EXIST:
  103.      err_code =  E_OPEN ;
  104.    }
  105.  
  106.    /* try to open the file */
  107.    #ifdef TURBO
  108.       if (_osmajor >= 3)
  109.      hand =  open( file_name, oflag | SH_DENYNONE, pmode ) ;
  110.       else
  111.          hand =  open( file_name, oflag, pmode ) ;
  112.    #else
  113.    #ifdef UNIX
  114.    {
  115.       char lwr_name[68] ;  /* Force to lower case for SCO Foxbase Compatibility */
  116.  
  117.       strncpy( lwr_name, file_name, sizeof(lwr_name) ) ;
  118.       lwr_name[sizeof(lwr_name)-1] = '\000' ;
  119.       u4lower( lwr_name ) ;
  120.  
  121.       hand =  open( lwr_name, oflag, pmode ) ;
  122.    }
  123.    #else
  124.       #ifdef ZORTECH
  125.          if ( open_code == SAFE_CREATE || open_code == DO_CREATE )
  126.          {
  127.             int  rc ;
  128.             char match[14] ;
  129.  
  130.             rc =  18 ;
  131.             hand = -1 ;
  132.  
  133.             if ( open_code == SAFE_CREATE )
  134.                rc =  u4file_first( file_name, match ) ;
  135.  
  136.             if ( rc == 18 )    hand = creat( file_name, pmode ) ;
  137.          }
  138.          else
  139.             hand =  open( file_name, oflag ) ;
  140.       #else
  141.          #ifdef __WATCOMC__
  142.             hand =  sopen( file_name, oflag, SH_DENYNO, pmode ) ;
  143.          #else
  144.             if (_osmajor >= 3)
  145.            hand =  sopen( file_name, oflag, SH_DENYNO, pmode ) ;
  146.             else
  147.                hand =  open( file_name, oflag, pmode ) ;
  148.          #endif
  149.       #endif
  150.    #endif
  151.    #endif
  152.  
  153.    if ( hand < 0)
  154.    {
  155.       if ( disp_err )
  156.          u4error( err_code, file_name, (char *) 0 ) ;
  157.  
  158.       return( -1) ;
  159.    }
  160.    return ( hand ) ;
  161. }
  162.