home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / bcfamily / source / open.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-12  |  3.8 KB  |  139 lines

  1. //
  2. //      *******************************************************************
  3. //        JdeBP C++ Library Routines          General Public Licence v1.00
  4. //            Copyright (c) 1991,1992     Jonathan de Boyne Pollard
  5. //      *******************************************************************
  6. //
  7. // Part of FamAPI.LIB
  8. //
  9.  
  10. #include "famapi.h"
  11. #include "dosdos.h"
  12.  
  13. #define    O_ANYSHARE    0xF0F0
  14.  
  15. //
  16. //    Open a file
  17. //
  18. //
  19. // BUG NOTE:  NewSize parameter is ignored
  20. //
  21. // We do not use Microsoft C's method of DosDosOpen()ing the file and then
  22. // checking for truncation flags, because on a Novell network with SEARCH MODE
  23. // set to anything other than 2, this may overwrite files in \PUBLIC.
  24. // Instead we use DosQFileMode, which Netware does not apply SEARCH MODE to.
  25. //
  26. // The down side is that DosQFileMode("*.*",...) behaves in a non-MS-DOS
  27. // fashion under Netware.  Swings and roundabouts ...
  28. //
  29. USHORT _APICALL
  30. DosOpen    ( const char far *FileName,
  31.           unsigned short far *PtrFileHandle,
  32.           unsigned int far *PtrActionTaken,
  33.           unsigned long NewSize,
  34.           unsigned int Attribute,
  35.           unsigned int OpenFlags,
  36.           unsigned int OpenMode,
  37.           unsigned long Reserved)
  38. {
  39.     unsigned short Version ;
  40.  
  41.     DosGetVersion (&Version) ;
  42.  
  43.     if (Version > 0x3FF) {
  44.         //
  45.         //    DOS 4.00 or later, use the extended open call
  46.         //
  47.         return DosDosXOpen(FileName, PtrFileHandle, PtrActionTaken,
  48.                            Attribute, OpenFlags, OpenMode) ;
  49.     } else {
  50.         //
  51.         //    DOS 3.xx, use the ordinary open/create calls
  52.         //
  53.         USHORT err ;
  54.         USHORT attr ;
  55.  
  56.         //
  57.         // First obtain attributes of existing file, if any.
  58.         //
  59.         err = DosQFileMode(FileName, &attr, 0);
  60.  
  61.         if (err) {
  62.             //
  63.             // If the file does not exist, fail if necessary.
  64.             //
  65.             if (err != ERROR_FILE_NOT_FOUND) return err ;
  66.  
  67.             *PtrActionTaken = 0x0000 ;        // File did not exist
  68.  
  69.             if (!(OpenFlags & 0x0010)) return ERROR_OPEN_FAILED ;
  70.  
  71.             //
  72.             // Now create the file.  If sharing has not been requested
  73.             // then we can finalise here and now.  Otherwise we need
  74.             // to DosClose and then DosDosOpen the file again.  In the
  75.             // latter case, we create the file _A_NORMAL otherwise
  76.             // creating a readonly file for read-write access would not
  77.             // work.
  78.             //
  79.             attr = (OpenMode & O_ANYSHARE) ? _A_NORMAL : Attribute ;
  80.  
  81.             err = DosDosCreate(FileName, attr, PtrFileHandle);
  82.             if (err) return err;
  83.  
  84.             *PtrActionTaken |= 0x0002 ;        // File was created
  85.  
  86.             if (!(OpenFlags & O_ANYSHARE)) return NO_ERROR ;
  87.  
  88.             DosClose(*PtrFileHandle);
  89.  
  90.         } else {
  91.             //
  92.             // If the file exists, fail if necessary.
  93.             // Otherwise, drop through to DosDosOpen.
  94.             //
  95.             // Under Netware, we will have reached here if the name contained
  96.             // enough wildcards to match a file in the directory.  Either
  97.             // the call will fail here, or in DosDosOpen further on. Since
  98.             // the call is nonsense, the error return can acceptably
  99.             // be nonsense also.
  100.             //
  101.             *PtrActionTaken = 0x0001 ;    // File existed
  102.  
  103.             if (!(OpenFlags & 0x000F)) return ERROR_OPEN_FAILED ;
  104.  
  105.         }
  106.  
  107.         err = DosDosOpen(FileName, OpenMode, PtrFileHandle);
  108.  
  109.         if (err) return err;
  110.  
  111.         //
  112.         // If the file is not a device, then truncate it if need be.
  113.         //
  114.         USHORT type = 0;
  115.  
  116.         err = DosDosDevIOCtl(*PtrFileHandle, 0x00, NULL, 0, &type);
  117.  
  118.         if (!err && !(type & 0x80U) && (OpenFlags & 0x0002)) {
  119.             USHORT len;
  120.  
  121.             *PtrActionTaken |= 0x0002 ;        // File was truncated
  122.  
  123.             DosDosWrite(*PtrFileHandle, NULL, 0, &len);
  124.         }
  125.  
  126.         //
  127.         // If we have re-opened a file which we created with DosDosCreate, we
  128.         // should now attempt to change it to the correct attributes.  Note
  129.         // that Attribute could conceivably contain _A_HIDDEN, so we make the
  130.         // test general.
  131.         //
  132.         if ((OpenFlags & 0x0010) && (Attribute != _A_NORMAL) && (OpenMode & O_ANYSHARE)) {
  133.             DosSetFileMode(FileName, Attribute, 0L);
  134.         }
  135.  
  136.         return NO_ERROR ;
  137.     }
  138. }
  139.