home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / FCOPY32.ZIP / FARREAD.ASM < prev    next >
Assembly Source File  |  1992-05-12  |  8KB  |  136 lines

  1. ;-----------------------------------------------------------------------;
  2. ; FARREAD.ASM                                                           ;
  3. ;                                                                       ;
  4. ; This module contains a function that duplicates the Turbo C library   ;
  5. ; function _read(), except that this new function expects a far pointer ;
  6. ; to the file buffer.  Thus, when used with farmalloc(), the calling    ;
  7. ; program can set up large file buffers using the far heap, regardless  ;
  8. ; of the memory model being used.                                       ;
  9. ;                                                                       ;
  10. ; The bsize variable, indicating the buffer size, can be any int sized  ;
  11. ; value up to (but not including) 65,535.  65,535 is also used to       ;
  12. ; represent -1, which is used as the return value in case of error.     ;
  13. ; It is recommended that the buffer be a multiple of the sector size    ;
  14. ; (512 bytes in the current version of DOS), since reads and writes     ;
  15. ; are more efficient if they involve whole sectors.  A good value to    ;
  16. ; use is 65024 (decimal), since this is 1 sector (512 bytes) short      ;
  17. ; of a complete segment (64k).                                          ;
  18. ;-----------------------------------------------------------------------;
  19. ; Usage:                                                                ;
  20. ;                                                                       ;
  21. ; #include "fcopy.h"                                                    ;
  22. ;                                                                       ;
  23. ; Function prototype:                                                   ;
  24. ;                                                                       ;
  25. ; int _farread (int handle, void far *buf, unsigned bsize)              ;
  26. ;                                                                       ;
  27. ; Returns:  If successful, return number of bytes written.  On          ;
  28. ;           end-of-file, returns 0.                                     ;
  29. ;                                                                       ;
  30. ;           If function fails, returns -1 and sets errno and            ;
  31. ;           _doserrno to one of the following:                          ;
  32. ;                                                                       ;
  33. ;               EACCES  Permission denied                               ;
  34. ;               EBADF   Bad file number                                 ;
  35. ;-----------------------------------------------------------------------;
  36. ; Revision history:                                                     ;
  37. ;                                                                       ;
  38. ;       1.0      5 JAN 92       Original.                               ;
  39. ;                                                                       ;
  40. ;                               Uses conditional assembly for all       ;
  41. ;                               memory models by defining _model.       ;
  42. ;                               Defaults to the small model if          ;
  43. ;                               _model is not defined.                  ;
  44. ;                                                                       ;
  45. ;       1.1     19 JAN 92       Split _farread and _farwrite into       ;
  46. ;                               separate modules.  Removed TASM         ;
  47. ;                               extensions so that the modules would    ;
  48. ;                               assemble using either TASM or MASM.     ;
  49. ;                                                                       ;
  50. ;       1.2     19 APR 92       Added conditional to enable local       ;
  51. ;                               labels when assembling with TASM.       ;
  52. ;                                                                       ;
  53. ;                               Rearranged exit code to avoid an        ;
  54. ;                               extra push of DS in huge data models.   ;
  55. ;                                                                       ;
  56. ;       1.3     12 MAY 92       Now makes no assumption about setting   ;
  57. ;                               of DS on entry.  For large data models  ;
  58. ;                               (compact and large), sets DS to the     ;
  59. ;                               default data segment.  For huge model,  ;
  60. ;                               sets DS to segment containing errno     ;
  61. ;                               so that errno and _doserrno can be set  ;
  62. ;                               on error.                               ;
  63. ;-----------------------------------------------------------------------;
  64. ;   Copyright (c) 1992 Ray Waters                                       ;
  65. ;   All Rights Reserved                                                 ;
  66. ;-----------------------------------------------------------------------;
  67.  
  68. EACCES  EQU     5                       ; equate for permission denied
  69. EBADF   EQU     6                       ; equate for bad file number
  70.  
  71. IFDEF   _model                          ; if a _model was defined,
  72.         .MODEL  _model, C               ;  use it
  73. ELSE                                    ; else, default to
  74.         .MODEL  SMALL, C                ;  SMALL model
  75. ENDIF
  76.  
  77. IFDEF   ??version                       ; if using TASM,
  78.         LOCALS                          ;  enable local labels
  79. ENDIF
  80.  
  81.  
  82.         EXTRN   C errno:WORD            ; global error variable
  83.         EXTRN   C _doserrno:WORD        ; global DOS error variable
  84.  
  85.         .CODE                           ; open code segment
  86.  
  87.         PUBLIC  _farread                ; make visible to Linker
  88. ;-----------------------------------------------------------------------;
  89. ; int _farread (int handle, void far *buf, unsigned bsize)              ;
  90. ;                                                                       ;
  91. ; Reads a file into a far buffer.                                       ;
  92. ;                                                                       ;
  93. ; Returns:  If successful, returns number of bytes read.  On            ;
  94. ;           end-of-file, returns 0.                                     ;
  95. ;                                                                       ;
  96. ;           If read failed, returns -1 and sets errno and _doserrno     ;
  97. ;           to one of the following:                                    ;
  98. ;                                                                       ;
  99. ;               EACCES  Permission denied                               ;
  100. ;               EBADF   Bad file number                                 ;
  101. ;-----------------------------------------------------------------------;
  102. _farread        PROC    C \
  103.                 handle:WORD, buf:FAR PTR BYTE, bsize:WORD
  104.  
  105.         push    ds                      ; save original data segment
  106.         mov     ah,3Fh                  ; DOS read file function
  107.         mov     bx,[handle]             ; file handle
  108.         mov     cx,[bsize]              ; number bytes to read
  109.         lds     dx,[buf]                ; far pointer to buffer
  110.         int     21h
  111.         jc      @@read_error            ; jump on error, else...
  112.         pop     ds                      ; restore original data segment
  113.         ret                             ; return to caller
  114.  
  115. @@read_error:                           ; if read error:
  116. IF @DataSize EQ 2                       ; if huge data model,
  117.         mov     bx,SEG errno            ; get segment address
  118.         mov     ds,bx                   ;  of errno into DS
  119. ELSEIF @DataSize EQ 1                   ; if large data model,
  120.         mov     bx,@data                ;  get default data segment
  121.         mov     ds,bx                   ;  into DS
  122. ELSE                                    ; if small data model,
  123.         pop     ds                      ;  restore original data segment
  124. ENDIF
  125.         mov     [errno],ax              ; set errno
  126.         mov     [_doserrno],ax          ; set _doserrno
  127. IF @DataSize                            ; if large data model,
  128.         pop     ds                      ;  restore original data segment
  129. ENDIF
  130.         mov     ax,-1                   ; return int value of -1
  131.         ret                             ; return to caller
  132.  
  133. _farread        ENDP
  134.  
  135.         END
  136.