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

  1. /*---------------------------------------------------------------------------
  2.  * filename - fstat.cas
  3.  *
  4.  * function(s)
  5.  *        fstat - gets open file information
  6.  *--------------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 5.0
  10.  *
  11.  *      Copyright (c) 1987, 1992 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16.  
  17. #pragma inline
  18. #include <asmrules.h>
  19. #include <sys\stat.h>
  20. #include <fcntl.h>
  21. #include <io.h>
  22. #include <_io.h>
  23. #include <dos.h>
  24. #include <mem.h>
  25. #include <RtlData.h>
  26.  
  27. /*--------------------------------------------------------------------------*
  28.  
  29. Name            fstat - gets open file information
  30.  
  31. Usage           #include <sys\stat.h>
  32.                 int fstat(int handle, struct stat *buff)
  33.  
  34. Prototype in    sys\stat.h
  35.  
  36. Description     Gather statistics about the nominated file and place
  37.                 them in the buffer *bufP.
  38.  
  39.                 Not all of the fields are relevant to MSDOS.
  40.                 The fields are set thus:
  41.  
  42.                     st_dev      set to fildes if device, else set to
  43.                                 drive holding the file.
  44.                     st_ino      0
  45.                     st_mode     Unix-style flag bits for file access rights
  46.                     st_nlink    1
  47.                     st_uid      0
  48.                     st_gid      0
  49.                     st_rdev     same as st_dev
  50.                     st_size     file size (0 if device).
  51.                     st_atime    time file last changed (seconds since 1970)
  52.                     st_mtime    same as st_atime
  53.                     st_ctime    same as st_atime
  54.  
  55.                 The file access rights flags may contain S_IFCHR or S_IFREG.
  56.                 S_IREAD is always set and S_IWRITE is set only for devices,
  57.                 since there is no method in PCDOS of inspecting the file
  58.                 attributes without knowing the file name.  Programs which
  59.                 need this information should use stat().
  60.  
  61.                 The file time fields are not available on MSDOS versions
  62.                 prior to 2.0, or if the file is a character stream (S_IFCHR),
  63.                 and will be set to zero in such cases.
  64.  
  65. Return value    The return value is 0 if the call was successful, otherwise
  66.                 -1 is returned and errno contains the reason.  The buffer is
  67.                 not touched unless the call is successful.
  68.  
  69. *---------------------------------------------------------------------------*/
  70. int _FARFUNC fstat (int fildes, struct stat *bufP)
  71. {
  72.     _QRTLDataBlock;
  73.  
  74. asm     mov     bx, fildes
  75. asm     mov     ax, 4400h               /* IOCTL, get device information */
  76. asm     int     21h
  77. asm     jnc     ioctlOK
  78. asm     jmp     fstatFailed
  79. ioctlOK:
  80. asm     sub     si, si                  /* SI = file mode       */
  81.  
  82. asm     or      dl, dl                  /* is it a character device ?   */
  83. asm     js      fst_isDevice
  84. /*
  85.   Arrive here if the fildes is for a regular file.
  86. */
  87.         _SI |= S_IFREG + S_IREAD;
  88.  
  89. asm     and     dx, 3Fh                 /* isolate the drive number     */
  90. asm     mov     di, dx                  /*     and keep it safe in DI   */
  91.  
  92.     if (_QRTLInstanceData(_openfd)[fildes] & _O_WRITABLE)
  93.         _SI |= S_IWRITE;
  94. asm     mov     bx, fildes
  95.  
  96. fst_findSize:
  97.     filelength (_BX);
  98. asm     or      dx, dx
  99. asm     jl      returnNeg1
  100.  
  101. asm     push    dx
  102. asm     push    ax                      /* save the file size   */
  103.  
  104. /*
  105.   File time-stamps can only be retrieved for MSDOS versions 2.0 or later.
  106. */
  107. asm     mov     ax, 5700h               /* get date and time    */
  108. asm     int     21h
  109. asm     jc      fst_zeroTime
  110. /*
  111.   MSDOS time is a 32-bit record, which must be converted into the Unix
  112.   style of seconds since 1970.
  113. */
  114. asm     push    dx                      /* date */
  115. asm     push    cx                      /* time */
  116. asm     call    __DOStimeToU
  117. asm     xchg    cx, ax                  /* result in DX:CX */
  118. asm     jmp     short   fst_construct
  119.  
  120. /*
  121.   Arrive here if IOCTL reported the handle to be for a character device.
  122. */
  123. fst_isDevice:
  124.         _SI |= S_IFCHR + S_IREAD + S_IWRITE;
  125. asm     mov     di, bx          /* use the fildes as device number */
  126. asm     sub     ax, ax
  127. asm     push    ax
  128. asm     push    ax              /* zero length, on stack        */
  129.  
  130. fst_zeroTime:
  131. asm     sub     cx, cx
  132. asm     mov     dx, cx
  133. /*
  134.   Arrive here with SI = mode, DI = device, DX:CX = time, and
  135.   the file length on stack.
  136. */
  137. fst_construct:
  138. asm     xchg    ax, di
  139. asm     LES_    di, bufP
  140. #if (! LDATA)
  141. asm     push    DS
  142. asm     pop     ES
  143. #endif
  144. asm     cld
  145. asm     stosw                   /* device       */
  146. asm     xchg    bx, ax          /* keep a copy  */
  147. asm     sub     ax, ax
  148. asm     stosw                   /* inode        */
  149. asm     xchg    ax, si
  150. asm     stosw                   /* mode */
  151. asm     mov     ax, 1
  152. asm     stosw                   /* number of links      */
  153. asm     xchg    ax, si          /* bring back the zero  */
  154. asm     stosw                   /* user (owner) id      */
  155. asm     stosw                   /* group id     */
  156. asm     xchg    ax, bx
  157. asm     stosw                   /* real device  */
  158. asm     pop     ax
  159. asm     stosw                   /* file..       */
  160. asm     pop     ax
  161. asm     stosw                   /*   ..size     */
  162. asm     xchg    ax, cx
  163. asm     stosw
  164. asm     xchg    ax, dx
  165. asm     stosw                   /* access time          */
  166. asm     xchg    ax, dx
  167. asm     stosw
  168. asm     xchg    ax, dx
  169. asm     stosw                   /* modification time    */
  170. asm     xchg    ax, dx
  171. asm     stosw
  172. asm     xchg    ax, dx
  173. asm     stosw                   /* status change time   */
  174.  
  175. fst_end:
  176.     return  0;
  177.  
  178. returnNeg1:
  179.     return  -1;
  180.  
  181. fstatFailed:
  182.     return  __IOerror (_AX);
  183. }
  184.