home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 2.ddi / CLIBSRC3.ZIP / OPEN.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  10.9 KB  |  291 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - open.cas
  3.  *
  4.  * function(s)
  5.  *        dosCreat     - creates a file
  6.  *        dosWriteNone - writes zero bytes to a file
  7.  *        open         - opens a file for reading or writing
  8.  *-----------------------------------------------------------------------*/
  9.  
  10. /*
  11.  *      C/C++ Run Time Library - Version 5.0
  12.  *
  13.  *      Copyright (c) 1987, 1992 by Borland International
  14.  *      All Rights Reserved.
  15.  *
  16.  */
  17.  
  18.  
  19. #pragma inline
  20.  
  21. #ifndef __PAS__
  22.  
  23. #define __IN_OPEN
  24.  
  25. #include <asmrules.h>
  26. #include <dos.h>
  27. #include <errno.h>
  28. #include <io.h>
  29. #include <_io.h>
  30. #include <fcntl.h>
  31. #include <sys\stat.h>
  32. #include <RtlData.h>
  33.  
  34. #if !defined( _RTLDLL )
  35. extern  unsigned      _notUmask;
  36. #endif
  37.  
  38. /*-----------------------------------------------------------------------*
  39.  
  40. Name            dosCreat -  creates a file
  41.  
  42. Usage           static  int pascal near  dosCreat (char *pathP,
  43.                                                    unsigned attrib);
  44.  
  45. Description     creates a file using DOS function 0x3c
  46.  
  47. Return value    file handle or -1 on error
  48.  
  49. *------------------------------------------------------------------------*/
  50. static  int pascal near  dosCreat (char *pathP, unsigned attrib)
  51. {
  52.         pushDS_
  53. asm     mov     cx, attrib
  54. asm     mov     ah, 3Ch
  55. asm     LDS_    dx, pathP
  56. asm     int     21h     /* AX = creat (DS:DX, CX) */
  57.         popDS_
  58. asm     jc      dosCreatFailed
  59.  
  60.         return _AX;
  61.  
  62. dosCreatFailed:
  63.         return  __IOerror (_AX);
  64. }
  65.  
  66.  
  67. /*-----------------------------------------------------------------------*
  68.  
  69. Name            dosWriteNone - writes zero bytes to a file
  70.  
  71. Usage           static  void pascal near    dosWriteNone (int handle);
  72.  
  73. Description     write 0 bytes to a file
  74.  
  75. Return value    Nothing
  76.  
  77. *------------------------------------------------------------------------*/
  78. static  void pascal near    dosWriteNone (int handle)
  79. {
  80. asm             mov             bx, handle
  81. asm             sub             cx, cx   /* zero length causes truncation */
  82. asm             sub             dx, dx
  83. asm             mov             ah, 40h
  84. asm             int             21h      /* dosWrite (fildes, dont-care, 0) */
  85.  
  86.                 return;                    /* no error checking */
  87. }
  88.  
  89.  
  90. /*-----------------------------------------------------------------------*
  91.  
  92. Name            open - opens a file for reading or writing
  93.  
  94. Usage           #include <fcntl.h>
  95.                 #include<sys\stat.h>
  96.                 int open(const char *pathname, int access [,unsigned permiss]);
  97.  
  98. Related
  99. functions usage int _open(const char *pathname, int access);
  100.  
  101. Prototype in    io.h
  102.  
  103. Description     open opens the file specified by pathname, then
  104.                 prepares it for reading and/or writing as determined by
  105.                 the value of access.
  106.  
  107.                 For open, access is constructed by bitwise ORing flags
  108.                 from the following two lists. Only one flag from the first
  109.                 list may be used; the remaining flags may be used in any
  110.                 logical combination.
  111.  
  112.                 List 1: Read/Write flags
  113.  
  114.                 O_RDONLY        Open for reading only.
  115.                 O_WRONLY        Open for writing only.
  116.                 O_RDWR          Open for reading and writing.
  117.  
  118.  
  119.                 List 2: Other access flags
  120.  
  121.                 O_NDELAY        Not used; for UNIX compatibility.
  122.                 O_APPEND        If set, the file pointer will be set
  123.                                 to the end of the file prior to each
  124.                                 write.
  125.                 O_CREAT         If the file exists, this flag has no effect.
  126.                                 If the file does not exist, the file is
  127.                                 created, and the bits of permiss are used
  128.                                 to set the file attribute bits, as in chmod.
  129.                 O_TRUNC         If the file exists, its length is truncated
  130.                                 to 0.
  131.                                 The file attributes remain unchanged.
  132.                 O_EXCL          Used only with O_CREAT. If the file already
  133.                                 exists, an error is returned.
  134.                 O_BINARY        This flag can be given to explicitly open
  135.                                 the file in binary mode.
  136.                 O_TEXT          This flag can be given to explicitly open
  137.                                 the file in text mode.
  138.  
  139.                 These O_... symbolic constants are defined in fcntl.h.
  140.  
  141.                 If neither O_BINARY nor O_TEXT is given, the file is
  142.                 opened in the translation mode set by the global variable
  143.                 _fmode.
  144.  
  145.                 If the O_CREAT flag is used in constructing access, you need
  146.                 to supply the permiss argument to open, from the following
  147.                 symbolic constants defined in sys\stat.h.
  148.  
  149.                 Value of permiss        Access Permission
  150.  
  151.                 S_IWRITE                Permission to write
  152.                 S_IREAD                 Permission to read
  153.                 S_IREAD|S_IWRITE        Permission to read and write
  154.  
  155.  
  156.                 For _open, the value of access in MS-DOS 2.x is limited to
  157.                 O_RDONLY, O_WRONLY, and O_RDWR. For MS-DOS 3.x, the following
  158.                 additional values can also be used:
  159.  
  160.                 O_NOINHERIT     Included if the file is not to be passed to
  161.                                 child programs.
  162.                 O_DENYALL       Allows only the current handle to have access to
  163.                                 the file.
  164.                 O_DENYWRITE     Allows only reads from any other open to the
  165.                                 file.
  166.                 O_DENYREAD      Allows only writes from any other open to the
  167.                                 file.
  168.                 O_DENYNONE      Allows other shared opens to the file.
  169.  
  170.                 Only one of the O_DENYxxx values may be included in a single
  171.                 _open under DOS 3.x. These file-sharing attributes are in
  172.                 addition to any locking performed on the files.
  173.  
  174.                 The maximum number of simultaneously open files is a system
  175.                 configuration parameter.
  176.  
  177. Return value    On successful completion, these routines return a
  178.                 non-negative integer (the file handle), and the file pointer
  179.                 (that marks the current position in the file) is set to the
  180.                 beginning of the file. On error, they return -1 and errno is
  181.                 set to one of the following:
  182.  
  183.                         ENOENT  Path or file name not found
  184.                         EMFILE  Too many open files
  185.                         EACCES  Permission denied
  186.                         EINVACC Invalid access code
  187.  
  188. *------------------------------------------------------------------------*/
  189. /* open declared old style since prototype has ... */
  190.  
  191. int  _FARFUNC open(pathP, oflag, mode)
  192.     const char *pathP; register int oflag; unsigned mode;
  193. /*
  194.   Open a new file or replace an existing file with the given pathname.
  195.   The opened file's read/write permission will be (pmode && !_notUmask),
  196.   unless the file already exists in which case its present mode is kept.
  197. */
  198. {
  199.         unsigned  attrib;
  200.         unsigned  devstat;
  201.         register int  fildes;
  202.         _QRTLDataBlock;
  203.  
  204.         if (! (oflag & (O_TEXT | O_BINARY)))
  205.                 oflag |= _QRTLInstanceData(_fmode) & (O_TEXT | O_BINARY);
  206.  
  207.         attrib = _chmod (pathP, 0);
  208.         if (oflag & O_CREAT)
  209.         {
  210.                 if (((mode &= _QRTLInstanceData(_notUmask)) & (S_IREAD | S_IWRITE)) == 0)
  211.                         __IOerror (1);
  212.  
  213.                 if (attrib == ~0U)      /* couldn't get file attributes? */
  214.                 {
  215.                         /* If it's any error other than 'file not found',
  216.                          * return immediately.  This prevents multiple
  217.                          * accesses on non-existent drives.
  218.                          */
  219.                         if (_doserrno != e_fileNotFound)
  220.                                 return (__IOerror(_doserrno));
  221.                         attrib = (mode & S_IWRITE) ? _A_NORMAL : _A_RDONLY;
  222.  
  223.                 }
  224.                 else
  225.                 {
  226.                         if (oflag & O_EXCL)
  227.                                 return(__IOerror (e_fileExists));
  228.                         else  /* ignore O_CREAT if file exists */
  229.                                 goto OpenExisting;
  230.                 }
  231.  
  232. /* Are any sharing/inheritance bits specified ?  If so, then we need to use
  233.    _open(), so  we must create, close,  then reopen it. Since  the creation
  234.    may be read only mode, but we may need read-write access, we must create
  235.    it with      write access to  allow us to  reopen for write,  and then later
  236.    change the mode after a successful open.
  237. */
  238.                 if (oflag & 0xF0)               /* any sharing/inheritance ? */
  239.                 {
  240.                         if ((fildes = dosCreat ((char *)pathP, 0)) < 0)
  241.                                 return(fildes);
  242.                         _close (fildes);
  243.                         goto OpenExisting;
  244.                 }
  245.                 else
  246.                         if ((fildes = dosCreat ((char *)pathP, attrib)) < 0)
  247.                                 return(fildes);
  248.         }
  249.         else
  250.         {
  251. OpenExisting:
  252.                 if ((fildes = _open (pathP, oflag)) >= 0)
  253.                 {
  254.  
  255.                         if ( (devstat = ioctl (fildes, 0)) & 0x80)
  256.                         {
  257.                                 oflag |= O_DEVICE;
  258. /* Set mode to Binary */
  259.                                 if (oflag & O_BINARY)
  260.                                         ioctl(fildes, 1, (void *)((devstat & 0xff) | 0x20));
  261.                         }
  262.                         else
  263.                         {
  264.                                 if ((oflag & O_TRUNC))
  265.                                         dosWriteNone (fildes);
  266. #if CPM_ctlZ    /* see commentary in file "zapctlz.cas" */
  267.                                 else
  268.                                         if (! (oflag & (O_BINARY | O_RDONLY)))
  269.                                                 __TrimCtlZ (fildes);
  270. #endif
  271.                         }
  272.  
  273.                         /* set shared file to read-only */
  274.                         if ((attrib & _A_RDONLY) && (oflag & O_CREAT) && (oflag & 0xF0))
  275.                         {
  276.                                 _chmod (pathP, 1, _A_RDONLY);
  277.                         }
  278.                 }
  279.         }
  280.  
  281.         if (fildes >= 0)
  282.             {
  283.             _QRTLInstanceData(_openfd) [fildes] = (oflag & ~_O_RUNFLAGS) |
  284.                        ((oflag & (O_CREAT | O_TRUNC)) ? O_CHANGED : 0)|
  285.                        ((attrib & _A_RDONLY) ? 0 : _O_WRITABLE);
  286.             }
  287.         return( fildes );
  288. }
  289.  
  290. #endif
  291.