home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_11 / 2n11026a < prev    next >
Text File  |  1991-09-13  |  3KB  |  96 lines

  1. /* ----------------------------------------------------
  2.  *  LISTING 5
  3.  *
  4.  *  Filename:           dos.c
  5.  *  Summary:            Implementation-specific code
  6.  *                      (DOS) for BFILE module.
  7.  *  Author:             T.W. Nelson
  8.  *  Compile options:    
  9.  *  Version:            1.00
  10.  *  Date:               07-Sep-1991
  11.  *  Notes:
  12.  *
  13.  *  Source code Copyright (c) 1991 T.W. Nelson. May be
  14.  *  used only for non-commercial purposes with
  15.  *  appropriate acknowledgement of copyright.
  16.  * ------------------------------------------------- */
  17.  
  18. #include <errno.h>
  19. #include "bfile.h"
  20.  
  21. #pragma inline      //use inline assembly
  22.  
  23. //Intersegment memory-move function.......
  24. void move_mem( bufp_t *dest,    //to destination
  25.                bufp_t *src,     //from source
  26.                size_t nbytes )  //#bytes to move
  27. {
  28.     asm push ds
  29.     asm push es
  30.     asm lds si,src
  31.     asm les di,dest
  32.     asm mov cx,nbytes
  33.     asm cld
  34.     asm shr cx,1        //convert to word count
  35.     asm rep movsw
  36.     asm jnc exit        //done if no carry
  37.     asm movsb           //move odd byte
  38. exit:
  39.     asm pop es
  40.     asm pop ds
  41. }
  42.  
  43. //Read a file into a far buffer. Returns
  44. //# bytes read, or sets 'errno' and returns -1
  45. //on error........
  46. int read_file( int fd,          //file handle
  47.                bufp_t *buf,     //destination buffer
  48.                size_t nbytes )  //#bytes to read
  49. {
  50.     asm push ds
  51.     asm mov ah,03fh     //DOS read file
  52.     asm mov bx,fd       //file handle
  53.     asm mov cx,nbytes   //xfer count
  54.     asm lds dx,buf      //destination
  55.     asm int 21h
  56.     asm pop ds
  57.     asm jnc exit        //read OK
  58.     errno = EBADF;      //assume bad handle
  59.     asm cmp ax,6        //invalid handle?
  60.     asm je error        //yes
  61.     errno = EACCES;     //no, permission denied
  62. error:
  63.     asm mov ax,-1       //return -1 on error
  64. exit:
  65.  
  66.     return _AX;
  67. }
  68.  
  69. //Write a file from a far buffer. Returns
  70. //# bytes written, or sets 'errno' and returns -1
  71. //on error........
  72. int write_file( int fd,         //file handle
  73.                 bufp_t *buf,    //source buffer
  74.                 size_t nbytes ) //#bytes to write
  75. {
  76.     asm push ds
  77.     asm mov ah,40h      //DOS write file
  78.     asm mov bx,fd       //file handle
  79.     asm mov cx,nbytes   //xfer count
  80.     asm lds dx,buf      //source
  81.     asm int 21h
  82.     asm pop ds
  83.     asm jnc exit        //read OK
  84.     errno = EBADF;      //assume bad handle
  85.     asm cmp ax,6        //invalid handle?
  86.     asm je error        //yes
  87.     errno = EACCES;     //no, permission denied
  88. error:
  89.     asm mov ax,-1       //return -1 on error
  90. exit:
  91.  
  92.     return _AX;
  93. }
  94.  
  95. /* ---- End of File -------------------------------- */
  96.