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

  1. /*-----------------------------------------------------------------------*
  2.  * filename - stat.cas
  3.  *
  4.  * function(s)
  5.  *        stat - gets information about open file
  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. #pragma inline
  19. #include <asmrules.h>
  20. #include <sys\stat.h>
  21. #include <dos.h>
  22. #include <_io.h>
  23. #include <fcntl.h>
  24. #include <io.h>
  25. #include <errno.h>
  26.  
  27. /*-----------------------------------------------------------------------*
  28.  
  29. Name        stat - gets information about open file
  30.  
  31. Usage        #include <sys\stat.h>
  32.         int stat(char *pathname, struct stat *buff)
  33.  
  34. Prototype in    sys\stat.h
  35.  
  36. Description    Gather statistics about the file named by *pathP and place them
  37.         in the buffer *bufP.
  38.  
  39.         It should be noted that on MSDOS this call can return more
  40.         information about a file than is possible with the fstat()
  41.         call.  In particular it is possible to get statistics for
  42.         directories and the time of the file is accessible for DOS
  43.         versions prior to 3.0.
  44.  
  45.         Not all of the fields are relevant to MSDOS.  The statistics
  46.         fields are set thus:
  47.  
  48.         st_dev        set to -1 if S_IFCHR, else set to drive
  49.                 holding the file.
  50.         st_ino        0
  51.         st_mode        Unix-style bit-set for file access rights
  52.         st_nlink    1
  53.         st_uid        0
  54.         st_gid        0
  55.         st_rdev        same as st_dev
  56.         st_size        file size (0 if S_IFDIR or S_IFCHR)
  57.         st_atime    time file last changed (seconds since 1970)
  58.         st_mtime    same as st_atime
  59.         st_ctime    same as st_atime
  60.  
  61.         The file access rights bit-set may contain S_IFCHR, S_IFDIR,
  62.         S_IFREG, S_IREAD, S_IWRITE, or S_IEXEC.
  63.  
  64.         If the name is for a device, the time fields will be zero
  65.         and the size field is undefined.
  66.  
  67. Return value       The return value is 0 if the call was successful, otherwise
  68.         -1 is returned and errno contains the reason.  The buffer
  69.         is not touched unless the call is successful.
  70.  
  71. *------------------------------------------------------------------------*/
  72. int stat (char *pathP, struct stat *bufP)
  73.  
  74. {
  75.     dosSearchInfo    info;
  76.  
  77.     pushDS_
  78. #if LDATA
  79. asm    push    SS
  80. asm    pop    DS
  81. #endif
  82. asm    lea    dx, info
  83. asm    mov    ah, 1Ah            /* set Device Transfer Address    */
  84. asm    int    21h
  85.     popDS_
  86. asm    jc    statFailed
  87.  
  88.     pushDS_
  89. asm    LDS_    dx, pathP
  90. asm    mov    cx, 16h         /* include directory, hidden, and system files */
  91. asm    mov    ah, 4Eh         /* Find First    */
  92. asm    int    21h
  93.     popDS_
  94. asm    jc    statFailed
  95.  
  96. asm    mov    dl, info.ds_attrib
  97. asm    sub    si, si            /* SI = file mode    */
  98. /*
  99.   It is a non-documented feature of the FindFirst function that devices
  100.   can be "found".  They are distinguished by the attribute 40h.
  101. */
  102. asm    test    dl, 40h            /* is it a character stream ?    */
  103. asm    jnz    sta_isDevice
  104.  
  105.     /*    Arrive here if the info is for a regular file.    */
  106.  
  107.     _SI |= S_IREAD;
  108. asm    dec    BY0(info.ds_drive)    /* drives from 0..n-1, not 1..n */
  109. asm    test    dl, 10h            /* directory ?    */
  110. asm    jnz    sta_isDir
  111.  
  112.     _SI |= S_IFREG | S_IREAD;
  113. asm    test    dl, 1            /* read only ?    */
  114. asm    jnz    sta_convertTime
  115.     _SI |= S_IWRITE;        /* write allowed    */
  116. asm    jmp    short    sta_convertTime
  117.  
  118.     /*    Arrive here if MSDOS calls failed.    */
  119. statFailed:
  120.     return __IOerror (_AX);
  121.  
  122.  
  123. sta_isDir:
  124.     _SI |= S_IFDIR | S_IEXEC;
  125.  
  126. /*
  127.   MSDOS time is a 32-bit record, which must be converted into the Unix
  128.   style of seconds since 1970.
  129. */
  130. sta_convertTime:
  131.     __DOStimeToU (*((long *) &info.ds_time));
  132. asm    xchg    cx, ax            /* result in DX:CX */
  133. asm    jmp    short    sta_construct
  134.  
  135. /* Arrive here if FindFirst identified the name as a character device. */
  136.  
  137. sta_isDevice:
  138. asm    mov    W0 (info.ds_drive), -1
  139.     _SI |= S_IFCHR | S_IREAD | S_IWRITE;
  140. asm    sub    cx, cx
  141. asm    mov    dx, cx            /* zero time    */
  142.  
  143. /* Arrive here with SI = mode, DX:CX = time, and info = directory info. */
  144.  
  145. sta_construct:
  146. asm    LES_    di, bufP
  147. #if (! LDATA)
  148. asm    push    DS
  149. asm    pop    ES
  150. #endif
  151. asm    cld
  152. asm    mov    al, info.ds_drive
  153. asm    cbw
  154. asm    stosw            /* device    */
  155. asm    xchg    bx, ax        /* keep a copy    */
  156. asm    sub    ax, ax
  157. asm    stosw            /* inode    */
  158. asm    xchg    ax, si
  159. asm    stosw            /* mode */
  160. asm    mov    ax, 1
  161. asm    stosw            /* number of links    */
  162. asm    xchg    ax, si        /* bring back the zero    */
  163. asm    stosw            /* user (owner) id    */
  164. asm    stosw            /* group id    */
  165. asm    xchg    ax, bx
  166. asm    stosw            /* real device    */
  167. asm    mov    ax, W0 (info.ds_size)
  168. asm    stosw            /* file..    */
  169. asm    mov    ax, W1 (info.ds_size)
  170. asm    stosw            /*   ..size    */
  171. asm    xchg    ax, cx
  172. asm    stosw
  173. asm    xchg    ax, dx
  174. asm    stosw            /* access time        */
  175. asm    xchg    ax, dx
  176. asm    stosw
  177. asm    xchg    ax, dx
  178. asm    stosw            /* modification time    */
  179. asm    xchg    ax, dx
  180. asm    stosw
  181. asm    xchg    ax, dx
  182. asm    stosw            /* status change time    */
  183.  
  184.     return 0;
  185. }
  186.