home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / CODE4-1.ZIP / SOURCE.ZIP / U4OPEN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-14  |  2.5 KB  |  117 lines

  1. /*  u4open.c   (C)Copyright  Sequiter Software Inc., 1987, 1988, 1989.  All rights reserved.
  2. */
  3.  
  4. #include "d4base.h"
  5. #include "u4error.h"
  6.  
  7. #include <fcntl.h>
  8.  
  9. #ifdef TURBO
  10.    #include <sys\stat.h>
  11.    #include <share.h>
  12.    #include <io.h>
  13.  
  14.    extern unsigned char _osmajor;
  15. #else
  16. #ifndef UNIX
  17.    #include <sys\types.h>
  18.    #include <sys\stat.h>
  19.    #include <share.h>
  20.    #include <io.h>
  21.  
  22.    extern unsigned char _osmajor;
  23. #endif
  24. #endif
  25.  
  26.  
  27. /* For Paramter 'open_code' */
  28. #define  OPEN_EXIST   0 
  29. #define  SAFE_CREATE  1 
  30. #define  DO_CREATE    2
  31.  
  32.  
  33. u4open( file_name, open_code ) 
  34. char *file_name ;
  35. int   open_code ;
  36. {
  37.    int  hand, oflag, pmode, err_code ;
  38.  
  39.    #ifdef TURBO
  40.       oflag    =  (int) (O_BINARY | O_RDWR) ;
  41.       pmode    =  (int) (S_IREAD  | S_IWRITE) ;
  42.    #else
  43.    #ifdef UNIX
  44.       oflag    =  (int)  O_RDWR ;
  45.       pmode    =   0666 ;  /* Allow complete read and write permissions */
  46.    #else
  47.       oflag    =  (int) (O_BINARY | O_RDWR) ;
  48.       pmode    =  (int) (S_IREAD  | S_IWRITE) ;
  49.    #endif
  50.    #endif
  51.  
  52.    err_code =  (int) E_CREATE ;
  53.  
  54.    switch( open_code )
  55.    {
  56.       case DO_CREATE:
  57.      #ifdef TURBO
  58.         unlink( file_name ) ;
  59.      #else
  60.         remove( file_name ) ;
  61.      #endif
  62.      oflag |=  O_CREAT ;
  63.      break ;
  64.  
  65.       case SAFE_CREATE:
  66.      oflag |=  (O_CREAT | O_EXCL) ;
  67.  
  68.      #ifdef TURBO
  69.         /* O_EXCL ignored in TURBO C 1.5 - Make sure file does not exist */
  70.         hand =  open( file_name, O_RDWR ) ;
  71.         if ( hand >= 0 )
  72.         {
  73.            /* Generate Error if File Opened !! */
  74.            close( hand ) ;
  75.            u4error( E_CREATE, file_name, (char *) 0 ) ;
  76.            return( -1 ) ;
  77.         }
  78.      #endif
  79.      break ;
  80.  
  81.       default:
  82.      err_code =  E_OPEN ;
  83.    }
  84.  
  85.    /* try to open the file */
  86.    #ifdef TURBO
  87.       if (_osmajor >= 3)
  88.      hand =  open( file_name, oflag | SH_DENYNONE, pmode ) ;
  89.       else
  90.          hand =  open( file_name, oflag, pmode ) ;
  91.    #else
  92.    #ifdef UNIX
  93.    {
  94.       char lwr_name[68] ;  /* Force to lower case for SCO Foxbase Compatibility */
  95.  
  96.       strncpy( lwr_name, file_name, sizeof(lwr_name) ) ;
  97.       lwr_name[sizeof(lwr_name)-1] = '\0' ;
  98.       strlwr( lwr_name ) ;
  99.  
  100.       hand =  open( lwr_name, oflag | O_SYNCW, pmode ) ;
  101.    }
  102.    #else
  103.       if (_osmajor >= 3)
  104.      hand = sopen( file_name, oflag, SH_DENYNO, pmode ) ;
  105.       else
  106.          hand =  open( file_name, oflag, pmode ) ;
  107.    #endif
  108.    #endif
  109.  
  110.    if ( hand < 0)
  111.    {
  112.       u4error( err_code, file_name, (char *) 0 ) ;
  113.       return( -1) ;
  114.    }
  115.    return ( hand ) ;
  116. }
  117.