home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / FSTAT.CAS < prev    next >
Encoding:
Text File  |  1990-06-07  |  6.0 KB  |  175 lines

  1. /*---------------------------------------------------------------------------
  2.  * filename - fstat.cas
  3.  *
  4.  * function(s)
  5.  *        fstat - gets open file information
  6.  *--------------------------------------------------------------------------*/
  7.  
  8. /*[]------------------------------------------------------------[]*/
  9. /*|                                                              |*/
  10. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  11. /*|                                                              |*/
  12. /*|                                                              |*/
  13. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  14. /*|     All Rights Reserved.                                     |*/
  15. /*|                                                              |*/
  16. /*[]------------------------------------------------------------[]*/
  17.  
  18.  
  19. #pragma inline
  20. #include <asmrules.h>
  21. #include <sys\stat.h>
  22. #include <io.h>
  23. #include <_io.h>
  24. #include <dos.h>
  25. #include <mem.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 fstat (int fildes, struct stat *bufP)
  71. {
  72. asm     mov     bx, fildes
  73. asm     mov     ax, 4400h               /* IOCTL, get device information */
  74. asm     int     21h
  75. asm     jc      fstatFailed
  76.  
  77. asm     sub     si, si                  /* SI = file mode       */
  78.  
  79. asm     or      dl, dl                  /* is it a character device ?   */
  80. asm     js      fst_isDevice
  81. /*
  82.   Arrive here if the fildes is for a regular file.
  83. */
  84.         _SI |= S_IFREG + S_IREAD;
  85.  
  86. asm     and     dx, 3Fh                 /* isolate the drive number     */
  87. asm     mov     di, dx                  /*     and keep it safe in DI   */
  88.  
  89. fst_findSize:
  90.     filelength (_BX);
  91. asm     or      dx, dx
  92. asm     jl      fstatFailed
  93.  
  94. asm     push    dx
  95. asm     push    ax                      /* save the file size   */
  96.  
  97. /*
  98.   File time-stamps can only be retrieved for MSDOS versions 2.0 or later.
  99. */
  100. asm     mov     ax, 5700h               /* get date and time    */
  101. asm     int     21h
  102. asm     jc      fst_zeroTime
  103. /*
  104.   MSDOS time is a 32-bit record, which must be converted into the Unix
  105.   style of seconds since 1970.
  106. */
  107. asm     push    dx                      /* date */
  108. asm     push    cx                      /* time */
  109. asm     call    __DOStimeToU
  110. asm     xchg    cx, ax                  /* result in DX:CX */
  111. asm     jmp     short   fst_construct
  112.  
  113. /*
  114.   Arrive here if IOCTL reported the handle to be for a character device.
  115. */
  116. fst_isDevice:
  117.         _SI |= S_IFCHR + S_IREAD + S_IWRITE;
  118. asm     mov     di, bx          /* use the fildes as device number */
  119. asm     sub     ax, ax
  120. asm     push    ax
  121. asm     push    ax              /* zero length, on stack        */
  122.  
  123. fst_zeroTime:
  124. asm     sub     cx, cx
  125. asm     mov     dx, cx
  126. /*
  127.   Arrive here with SI = mode, DI = device, DX:CX = time, and
  128.   the file length on stack.
  129. */
  130. fst_construct:
  131. asm     xchg    ax, di
  132. asm     LES     di, bufP
  133. #if (! LDATA)
  134. asm     push    DS
  135. asm     pop     ES
  136. #endif
  137. asm     cld
  138. asm     stosw                   /* device       */
  139. asm     xchg    bx, ax          /* keep a copy  */
  140. asm     sub     ax, ax
  141. asm     stosw                   /* inode        */
  142. asm     xchg    ax, si
  143. asm     stosw                   /* mode */
  144. asm     mov     ax, 1
  145. asm     stosw                   /* number of links      */
  146. asm     xchg    ax, si          /* bring back the zero  */
  147. asm     stosw                   /* user (owner) id      */
  148. asm     stosw                   /* group id     */
  149. asm     xchg    ax, bx
  150. asm     stosw                   /* real device  */
  151. asm     pop     ax
  152. asm     stosw                   /* file..       */
  153. asm     pop     ax
  154. asm     stosw                   /*   ..size     */
  155. asm     xchg    ax, cx
  156. asm     stosw
  157. asm     xchg    ax, dx
  158. asm     stosw                   /* access time          */
  159. asm     xchg    ax, dx
  160. asm     stosw
  161. asm     xchg    ax, dx
  162. asm     stosw                   /* modification time    */
  163. asm     xchg    ax, dx
  164. asm     stosw
  165. asm     xchg    ax, dx
  166. asm     stosw                   /* status change time   */
  167.  
  168. fst_end:
  169.     return  0;
  170.  
  171.  
  172. fstatFailed:
  173.     return  __IOerror (_AX);
  174. }
  175.