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

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