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

  1. /*---------------------------------------------------------------------------
  2.  * filename - absread.cas
  3.  *
  4.  * function(s)
  5.  *       absread  - absolute disk read
  6.  *       abswrite - absolute disk write
  7.  *--------------------------------------------------------------------------*/
  8.  
  9. /*[]------------------------------------------------------------[]*/
  10. /*|                                                              |*/
  11. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  12. /*|                                                              |*/
  13. /*|                                                              |*/
  14. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  15. /*|     All Rights Reserved.                                     |*/
  16. /*|                                                              |*/
  17. /*[]------------------------------------------------------------[]*/
  18.  
  19.  
  20. #pragma inline
  21. #include <asmrules.h>
  22. #include <dos.h>
  23. #include <errno.h>
  24.  
  25. static int big_partition( int driveno )
  26. {
  27.   static int old_driveno = -1, old_partition;
  28.   struct fatinfo p;
  29.  
  30.   if( driveno != old_driveno )     /* try to avoid disk reads if possible */
  31.   {
  32.     old_driveno = driveno;
  33.  
  34.     getfat( driveno+1, &p );
  35.  
  36.     old_partition = ((long)p.fi_sclus * p.fi_nclus) > 0xFFFF ? 1 : 0;
  37.   }
  38.  
  39.   return( old_partition );
  40. }
  41.  
  42.  
  43. /*--------------------------------------------------------------------------*
  44.  
  45. Name        absread - absolute disk read
  46.  
  47. Usage        int absread(int drive, int nsects, long sectno, void *buffer);
  48.  
  49. Prototype in    dos.h
  50.  
  51. Description    reads specific disk sectors via DOS interrupt 0x25
  52.  
  53. Return value    If successful, 0
  54.         On error, -1 and errno set to the value returned in AX.
  55.  
  56. *---------------------------------------------------------------------------*/
  57. int absread(int drive, int nsects, long lsect, void *buffer)
  58. {
  59.   SaveSI
  60.   SaveDI
  61.   pushDS_
  62.  
  63.   if( big_partition( drive ) )
  64.   {
  65.     asm     mov     al,drive
  66.     asm     push    ss
  67.     asm     pop     ds
  68.     asm     sub     sp,10
  69.     asm     mov     bx,sp
  70.     asm     mov     dx,word ptr lsect
  71.     asm     mov     word ptr [bx],dx
  72.     asm     mov     dx,word ptr lsect+2
  73.     asm     mov     word ptr [bx+2],dx
  74.     asm     mov     cx,nsects
  75.     asm     mov     word ptr [bx+4],cx
  76.     asm     mov     dx,word ptr buffer
  77.     asm     mov     word ptr [bx+6],dx
  78.  
  79. #if (LDATA)
  80.     asm     mov     dx,word ptr buffer+2
  81.     asm     mov     word ptr [bx+8],dx
  82. #else
  83.     asm     mov     word ptr [bx+8],ds
  84. #endif
  85.  
  86.     asm     mov     cx,-1
  87.     asm     int     25h
  88.     asm     sbb     cx,cx
  89.     asm     add     sp,10
  90.   }
  91.   else
  92.   {
  93.     asm     mov     al,drive
  94.     asm     mov     cx,nsects
  95.     asm     mov     dx,lsect
  96.     asm     LDS_    bx,buffer
  97.     asm     int     25h
  98.     asm     sbb     cx,cx
  99.   }
  100.  
  101.   asm     pop     bx                      /* clear old flags */
  102.           popDS_
  103.   asm     jcxz    absOK
  104.  
  105. #if defined(__HUGE__)
  106.   asm     mov     bx,seg errno
  107.   asm     mov     es,bx
  108.   asm     mov     es:errno,ax
  109. #else
  110.   asm     mov     errno,ax
  111. #endif
  112.  
  113.   return(-1);
  114.  
  115. absOK:
  116.   return(0);
  117. }
  118.  
  119.  
  120. /*--------------------------------------------------------------------------*
  121.  
  122. Name        abswrite - absolute disk write
  123.  
  124. Usage        int abswrite(int drive, int nsects, long sectno, void *buffer);
  125.  
  126. Prototype in    dos.h
  127.  
  128. Description    writes specific disk sectors via DOS interrupt 0x26
  129.  
  130. Return value    If successful, 0
  131.         On error, -1 and errno set to the value returned in AX.
  132.  
  133. *---------------------------------------------------------------------------*/
  134. int abswrite(int drive, int nsects, long lsect, void *buffer)
  135. {
  136.   SaveSI
  137.   SaveDI
  138.   pushDS_
  139.  
  140.   if( big_partition( drive ) )
  141.   {
  142.     asm     mov     al,drive
  143.     asm     push    ss
  144.     asm     pop     ds
  145.     asm     sub     sp,10
  146.     asm     mov     bx,sp
  147.     asm     mov     dx,word ptr lsect
  148.     asm     mov     word ptr [bx],dx
  149.     asm     mov     dx,word ptr lsect+2
  150.     asm     mov     word ptr [bx+2],dx
  151.     asm     mov     cx,nsects
  152.     asm     mov     word ptr [bx+4],cx
  153.     asm     mov     dx,word ptr buffer
  154.     asm     mov     word ptr [bx+6],dx
  155.  
  156. #if (LDATA)
  157.     asm     mov     dx,word ptr buffer+2
  158.     asm     mov     word ptr [bx+8],dx
  159. #else
  160.     asm     mov     word ptr [bx+8],ds
  161. #endif
  162.  
  163.     asm     mov     cx,-1
  164.     asm     int     26h
  165.     asm     sbb     cx,cx
  166.     asm     add     sp,10
  167.   }
  168.   else
  169.   {
  170.     asm     mov     al,drive
  171.     asm     mov     cx,nsects
  172.     asm     mov     dx,lsect
  173.     asm     LDS_    bx,buffer
  174.     asm     int     26h
  175.     asm     sbb     cx,cx
  176.   }
  177.  
  178.   asm     pop     bx                      /* clear old flags */
  179.           popDS_
  180.   asm     jcxz    absOK
  181.  
  182. #if     defined(__HUGE__)
  183.   asm     mov     bx,seg errno
  184.   asm     mov     es,bx
  185.   asm     mov     es:errno,ax
  186. #else
  187.   asm     mov     errno,ax
  188. #endif
  189.  
  190.   return(-1);
  191.  
  192. absOK:
  193.   return(0);
  194. }
  195.  
  196.