home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / os2 / ntree / ntreecpy.asm < prev    next >
Encoding:
Assembly Source File  |  1988-03-01  |  6.9 KB  |  301 lines

  1.         NAME    DOSCALLS
  2.         TITLE    DOSCALLS
  3.  
  4.         PAGE    60,132
  5. ;
  6. ; This is coded for Microsoft C. The small memory model
  7. ; (for both _code and _data) are assumed. This is easily changed.
  8. ;
  9. ; Constants which affect compilation:
  10. ;    OS2ONLY        causes ntree to determine amount of memory
  11. ;            available before issueing the DOSALLOCSEG call.
  12. ;
  13.  
  14.         .MODEL    SMALL
  15.         .LALL            ; list all macro expansion
  16.                     ; .XALL will list those 4 code
  17.  
  18. ;_TEXT        SEGMENT    BYTE USE16 PUBLIC 'CODE'
  19. ;_TEXT        ENDS
  20. ;_DATA        SEGMENT WORD USE16 PUBLIC 'DATA'
  21. ;_DATA        ENDS
  22. ;CONST        SEGMENT    WORD USE16 PUBLIC 'CONST'
  23. ;CONST        ENDS
  24. ;_BSS        SEGMENT WORD USE16 PUBLIC 'BSS'
  25. ;_BSS        ENDS
  26.  
  27. ;DGROUP        GROUP    CONST, _BSS, _DATA
  28. ;        ASSUME    CS:_TEXT, DS:DGROUP, SS:DGROUP, ES:DGROUP
  29.  
  30.  
  31.         .DATA
  32. ;_DATA        SEGMENT
  33.  
  34.         PUBLIC    alloced,msize,inhandle,outhandle,action
  35.         PUBLIC    bytesread,byteswritten,mselector,memavail
  36.         PUBLIC    memavailhi,memavaillo,fileinfobuf
  37.  
  38.         EXTRN    _errno:near    ; C errno variable
  39.  
  40. alloced        dw    0        ; have we alloced yet?
  41. msize        dw    0        ; segment size (bytes)
  42. inhandle    dw    0        ; in file handle
  43. outhandle    dw    0        ; out file handle
  44. action        dw    0        ; action taken for open call
  45. bytesread    dw    0        ; # of bytes read in DOSREAD()
  46. byteswritten    dw    0        ; # of bytes written in DOSWRITE()
  47. mselector    dw    0        ; selector for memory segment
  48.  
  49. memavail    label    DWORD
  50. memavailhi    dw    0        ; for DOSMEMAVAIL() call
  51. memavaillo    dw    0
  52.  
  53. fileinfobuf    label    WORD
  54. create        dd    ?
  55. lastaccess    dd    ?
  56. lastwrite    dd    ?
  57. endofdata    dd    ?
  58. allocation    dd    ?
  59. fileattrib    dw    ?
  60. FIBUFSIZE    EQU    $-fileinfobuf
  61.  
  62. ;_DATA        ENDS
  63.  
  64.  
  65.  
  66.         .CODE
  67. ;_TEXT        SEGMENT
  68. ;IGROUP        GROUP    _TEXT
  69.  
  70.         extrn    DOSALLOCSEG:FAR
  71.         extrn    DOSMEMAVAIL:FAR
  72.         extrn    DOSFREESEG:FAR
  73.         extrn    DOSOPEN:FAR
  74.         extrn    DOSCLOSE:FAR
  75.         extrn    DOSREAD:FAR
  76.         extrn    DOSWRITE:FAR
  77.         extrn    DOSQFILEINFO:FAR
  78.         extrn    DOSSETFILEINFO:FAR
  79.  
  80.  
  81. ;
  82. ; openfile    <filename, @filehandle, @faction, attribute, flags, mode>
  83. ;
  84. openfile    MACRO    fname,fhandle,faction,fattrib,fflag,fmode
  85.         push    ds        ; filename
  86.         mov    ax,fname
  87.         push    ax
  88.         push    ds        ; handle
  89.         mov    ax,fhandle
  90.         push    ax
  91.         push    ds        ; actiontaken
  92.         mov    ax,faction
  93.         push    ax
  94.         xor    ax,ax        ; new file size
  95.         push    ax
  96.         push    ax
  97.         mov    ax,fattrib    ; file attribute
  98.         push    ax
  99.         mov    ax,fflag    ; open flags
  100.         push    ax
  101.         mov    ax,fmode    ; open mode
  102.         push    ax
  103.         xor    ax,ax        ; reserved
  104.         push    ax
  105.         push    ax
  106.         call    DOSOPEN
  107.         or    ax,ax        ; set result flags
  108.         ENDM
  109.  
  110.  
  111.         PAGE
  112. ;   +-------------------------------------------------------------------+
  113. ;   |  docopyfile(from,to)                        |
  114. ;   |  copy file from from to to                    |
  115. ;   |  errors returned:            (actual error code is in _errno)    |
  116. ;   |     1: error opening source file                    |
  117. ;   |     2: error opening dest file                    |
  118. ;   |     3: error reading source file                    |
  119. ;   |     4: error writing dest file                    |
  120. ;   |     5: error allocating memory                    |
  121. ;   +-------------------------------------------------------------------+
  122.  
  123.         public    _docopyfile
  124.  
  125. _docopyfile    proc    near
  126.  
  127.         push    bp
  128.         mov    bp,sp
  129.         cmp    word ptr alloced,0
  130.         jne    do_it_now        ; all set to go
  131.  
  132.         IFDEF    OS2ONLY
  133.         ; first see how much memory we can get
  134.         push    ds
  135.         mov    ax,offset memavail
  136.         push    ax
  137.         call    DOSMEMAVAIL
  138.         or    ax,ax            ; check for error
  139.         jnz    allocerror
  140.  
  141.         ; now prepare to request memory for our use
  142.         ; note that the memory available can change before this!
  143.         mov    bx,0FFFFh        ; almost 64K
  144.         cmp    memavailhi,0        ; more than 64k available?
  145.         jnz    doalloc            ; yes: go request [BX]
  146.         cmp    memavaillo,bx        ; less than what we want?
  147.         jge    doalloc            ; no: go request [BX]
  148.         mov    bx,word ptr memavaillo    ; request what is available
  149.  
  150.         ELSE    ; IF OS2ONLY
  151.         mov    bx,0FFFFh        ; request just under 64kb
  152.         ENDIF    ;;
  153.  
  154.  
  155.         ; now request [BX] bytes of memory
  156. doalloc:    push    bx
  157.         push    ds
  158.         mov    ax,offset mselector
  159.         push    ax
  160.         xor    ax,ax            ; allocation flags
  161.         push    ax
  162.         call    DOSALLOCSEG
  163.         or    ax,ax            ; any error
  164.         jz    got_mem            ; no: go ahead and process
  165.  
  166. allocerror:    jmp    alloc_mem_err
  167.  
  168. got_mem:    mov    word ptr msize,bx    ; save # of bytes allocated
  169.         mov    word ptr alloced,1    ; set flag
  170.  
  171.         ; we have allocated our memory, now we can open files
  172. do_it_now:    openfile  [BP+4],<OFFSET inhandle>,<OFFSET action>,0,1,20h
  173.         jz    open_src_ok
  174.         jmp    open_src_err
  175.  
  176. open_src_ok:    openfile  [BP+6],<OFFSET outhandle>,<OFFSET action>,0,12h,12h
  177.         jz    copy_loop
  178.         jmp    open_dst_err        ; error opening dest
  179.  
  180.         ; copy data in file
  181. copy_loop:
  182.         ; read data from source file
  183.         mov    ax,word ptr inhandle    ; file handle
  184.         push    ax
  185.         mov    ax,word ptr mselector    ; read to mselector:0000h
  186.         push    ax
  187.         xor    ax,ax
  188.         push    ax
  189.         mov    word ptr bytesread,ax    ; zero out # of bytes read
  190.         mov    ax,word ptr msize    ; # of bytes to read
  191.         push    ax
  192.         mov    ax,offset bytesread    ; # of bytes actually read
  193.         push    ds
  194.         push    ax
  195.         call    DOSREAD
  196.         or    ax,ax            ; any errors?
  197.         jz    read_okay        ; no
  198.         jmp    read_src_err        ; yes
  199.  
  200. read_okay:    mov    ax,word ptr bytesread
  201.         cmp    ax,0            ; at end of file?
  202.         je    at_eof            ; yup.
  203.  
  204.         ; now write out the buffer
  205.         mov    ax,word ptr outhandle    ; output handle
  206.         push    ax
  207.         mov    ax,word ptr mselector    ; buffer at mselector:0000h
  208.         push    ax
  209.         xor    ax,ax
  210.         push    ax
  211.         mov    word ptr byteswritten,ax ; zero out # of bytes written
  212.         mov    ax,word ptr bytesread    ; # of bytes to write
  213.         push    ax
  214.         mov    ax,offset byteswritten    ; # of bytes written
  215.         push    ds
  216.         push    ax
  217.         call    DOSWRITE
  218.         or    ax,ax            ; any error
  219.         jnz    write_dst_err        ; couldn't write
  220.  
  221.         mov    bx,word ptr bytesread
  222.         cmp    word ptr byteswritten,bx  ; wrote the same amt read?
  223.         jne    write_dst_err
  224.         jmp    copy_loop
  225.  
  226. at_eof:
  227. ; copy file creation date, date of last access, and date of last write
  228.         ; read data about input file
  229.         mov    ax,word ptr inhandle    ; input handle
  230.         push    ax
  231.         mov    ax,0001h        ; fileinfolevel
  232.         push    ax
  233.         mov    ax,offset fileinfobuf    ; addr of buffer for data
  234.         push    ds
  235.         push    ax
  236.         mov    ax,FIBUFSIZE        ; fileinfobuf size
  237.         push    ax
  238.         call    DOSQFILEINFO
  239.         or    ax,ax            ; any error?
  240.         jnz    read_src_err
  241.  
  242.         ; now set the file info of the output file
  243.         mov    ax,word ptr outhandle    ; output handle
  244.         push    ax
  245.         mov    ax,0001h        ; fileinfolevel
  246.         push    ax
  247.         mov    ax,offset fileinfobuf    ; addr of buffer for data
  248.         push    ds
  249.         push    ax
  250.         mov    ax,000Ch        ; size of buffer
  251.         push    ax
  252.         call    DOSSETFILEINFO
  253.         or    ax,ax
  254.         jnz    write_dst_err        ; errors...
  255.  
  256. ; close files
  257.  
  258.         ; close input file
  259.         mov    ax,word ptr inhandle
  260.         push    ax
  261.         call    DOSCLOSE
  262.         or    ax,ax
  263.         jnz    read_src_err
  264.  
  265.         ; close output file
  266.         mov    ax,word ptr outhandle
  267.         push    ax
  268.         call    DOSCLOSE
  269.         or    ax,ax
  270.         jnz    write_dst_err
  271.  
  272.         xor    ax,ax        ; our result code = 0000h
  273.  
  274. copy_exit:    clc
  275.         pop    bp
  276.         ret
  277.  
  278.  
  279. open_src_err:    mov    word ptr _errno, ax
  280.         mov    ax,1
  281.         jmp    short copy_exit
  282. open_dst_err:    mov    word ptr _errno, ax
  283.         mov    ax,2
  284.         jmp    short copy_exit
  285. read_src_err:    mov    word ptr _errno, ax
  286.         mov    ax,3
  287.         jmp    short copy_exit
  288. write_dst_err:    mov    word ptr _errno, ax
  289.         mov    ax,4
  290.         jmp    short copy_exit
  291. alloc_mem_err:    mov    word ptr _errno, ax
  292.         mov    ax,5
  293.         jmp    short copy_exit
  294.  
  295. _docopyfile    endp
  296.  
  297.  
  298. ;_TEXT        ENDS
  299.         END
  300.  
  301.