home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / FCOPY / FARWRITE.ASM < prev    next >
Assembly Source File  |  1993-12-01  |  9KB  |  145 lines

  1. ;-----------------------------------------------------------------------;
  2. ; FARWRITE.ASM                                                          ;
  3. ;                                                                       ;
  4. ; This module contains a function that duplicates the Turbo C library   ;
  5. ; function _write(), except that this new function expects a far        ;
  6. ; pointer to the file buffer.  Thus, when used with farmalloc(), the    ;
  7. ; calling program can set up large file buffers using the far heap,     ;
  8. ; regardless 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 _farwrite (int handle, void far *buf, unsigned bsize)             ;
  26. ;                                                                       ;
  27. ; Returns:  If successful, return number of bytes written.              ;
  28. ;                                                                       ;
  29. ;           If function fails, returns -1 and sets errno and            ;
  30. ;           _doserrno to one of the following:                          ;
  31. ;                                                                       ;
  32. ;               EACCES  Permission denied                               ;
  33. ;               EBADF   Bad file number                                 ;
  34. ;-----------------------------------------------------------------------;
  35. ; Revision history:                                                     ;
  36. ;                                                                       ;
  37. ;       1.0      5 JAN 92       Original.                               ;
  38. ;                                                                       ;
  39. ;                               Uses conditional assembly for all       ;
  40. ;                               memory models by defining _model.       ;
  41. ;                               Defaults to the small model if          ;
  42. ;                               _model is not defined.                  ;
  43. ;                                                                       ;
  44. ;       1.1     19 JAN 92       Split _farread and _farwrite into       ;
  45. ;                               separate modules.  Removed TASM         ;
  46. ;                               extensions so that the modules would    ;
  47. ;                               assemble using either TASM or MASM.     ;
  48. ;                                                                       ;
  49. ;       1.2     19 APR 92       Added conditional to enable local       ;
  50. ;                               labels when assembling with TASM.       ;
  51. ;                                                                       ;
  52. ;                               Defined equate DISKFUL = -2 as an       ;
  53. ;                               error code.  A similar #define was      ;
  54. ;                               added to fcopy.h.                       ;
  55. ;                                                                       ;
  56. ;                               The jumps in the error handling code    ;
  57. ;                               were also rearranged so that only one   ;
  58. ;                               error exit routine is now needed.       ;
  59. ;                                                                       ;
  60. ;       1.3     12 MAY 92       Now makes no assumption about setting   ;
  61. ;                               of DS on entry.  For large data models  ;
  62. ;                               (compact and large), sets DS to the     ;
  63. ;                               default data segment.  For huge model,  ;
  64. ;                               sets DS to segment containing errno     ;
  65. ;                               so that errno and _doserrno can be set  ;
  66. ;                               on error.                               ;
  67. ;-----------------------------------------------------------------------;
  68. ;   Copyright (c) 1992 Ray Waters                                       ;
  69. ;   All Rights Reserved                                                 ;
  70. ;-----------------------------------------------------------------------;
  71.  
  72. EACCES  EQU     5                       ; equate for permission denied
  73. EBADF   EQU     6                       ; equate for bad file number
  74. DISKFUL EQU     -2                      ; equate for disk full condition
  75.  
  76. IFDEF   _model                          ; if a _model was defined,
  77.         .MODEL  _model, C               ;  use it
  78. ELSE                                    ; else, default to
  79.         .MODEL  SMALL, C                ;  SMALL model
  80. ENDIF
  81.  
  82. IFDEF   ??version                       ; if using TASM,
  83.         LOCALS                          ;  enable local labels
  84. ENDIF
  85.  
  86.         EXTRN   C errno:WORD            ; global error variable
  87.         EXTRN   C _doserrno:WORD        ; global DOS error variable
  88.  
  89.         .CODE                           ; open code segment
  90.  
  91.         PUBLIC  _farwrite               ; make visible to Linker
  92. ;-----------------------------------------------------------------------;
  93. ; int _farwrite (int handle, void far *buf, unsigned bsize)             ;
  94. ;                                                                       ;
  95. ; Writes to a file from a far buffer.                                   ;
  96. ;                                                                       ;
  97. ; Returns:  If successful, returns number of bytes written.             ;
  98. ;                                                                       ;
  99. ;           If write failed, returns -1 and sets errno and _doserrno    ;
  100. ;           to one of the following:                                    ;
  101. ;                                                                       ;
  102. ;               EACCES  Permission denied                               ;
  103. ;               EBADF   Bad file number                                 ;
  104. ;                   -1  Target disk full                                ;
  105. ;-----------------------------------------------------------------------;
  106. _farwrite       PROC    C \
  107.                 handle:WORD, buf:FAR PTR BYTE, bsize:WORD
  108.  
  109.         push    ds                      ; save data segment
  110.         mov     ah,40h                  ; DOS write file function
  111.         mov     bx,[handle]             ; file handle
  112.         mov     cx,[bsize]              ; number bytes to write
  113.         lds     dx,[buf]                ; far pointer to buffer
  114.         int     21h
  115.         jc      @@write_error           ; jump on error
  116.         cmp     ax,cx                   ; did all bytes get copied?
  117.         jne     @@partial_write_error   ; if not, partial write error
  118.         pop     ds                      ; if yes, restore data segment
  119.         ret                             ; return to caller
  120.  
  121. @@partial_write_error:
  122.         mov     ax,DISKFUL              ; set error code for partial write
  123.  
  124. @@write_error:                          ; if write error:
  125. IF @DataSize EQ 2                       ; if huge data model,
  126.         mov     bx,SEG errno            ; get segment address
  127.         mov     ds,bx                   ;  of errno into DS
  128. ELSEIF @DataSize EQ 1                   ; if large data model,
  129.         mov     bx,@data                ;  get default data segment
  130.         mov     ds,bx                   ;  into DS
  131. ELSE                                    ; if small data model,
  132.         pop     ds                      ;  restore original data segment
  133. ENDIF
  134.         mov     [errno],ax              ; set errno
  135.         mov     [_doserrno],ax          ; set _doserrno
  136. IF @DataSize                            ; if large data model,
  137.         pop     ds                      ;  restore original data segment
  138. ENDIF
  139.         mov     ax,-1                   ; return int value of -1
  140.         ret                             ; return to caller
  141.  
  142. _farwrite       ENDP
  143.  
  144.         END
  145.