home *** CD-ROM | disk | FTP | other *** search
- NAME DOSCALLS
- TITLE DOSCALLS
-
- PAGE 60,132
- ;
- ; This is coded for Microsoft C. The small memory model
- ; (for both _code and _data) are assumed. This is easily changed.
- ;
- ; Constants which affect compilation:
- ; OS2ONLY causes ntree to determine amount of memory
- ; available before issueing the DOSALLOCSEG call.
- ;
-
- .MODEL SMALL
- .LALL ; list all macro expansion
- ; .XALL will list those 4 code
-
- ;_TEXT SEGMENT BYTE USE16 PUBLIC 'CODE'
- ;_TEXT ENDS
- ;_DATA SEGMENT WORD USE16 PUBLIC 'DATA'
- ;_DATA ENDS
- ;CONST SEGMENT WORD USE16 PUBLIC 'CONST'
- ;CONST ENDS
- ;_BSS SEGMENT WORD USE16 PUBLIC 'BSS'
- ;_BSS ENDS
-
- ;DGROUP GROUP CONST, _BSS, _DATA
- ; ASSUME CS:_TEXT, DS:DGROUP, SS:DGROUP, ES:DGROUP
-
-
- .DATA
- ;_DATA SEGMENT
-
- PUBLIC alloced,msize,inhandle,outhandle,action
- PUBLIC bytesread,byteswritten,mselector,memavail
- PUBLIC memavailhi,memavaillo,fileinfobuf
-
- EXTRN _errno:near ; C errno variable
-
- alloced dw 0 ; have we alloced yet?
- msize dw 0 ; segment size (bytes)
- inhandle dw 0 ; in file handle
- outhandle dw 0 ; out file handle
- action dw 0 ; action taken for open call
- bytesread dw 0 ; # of bytes read in DOSREAD()
- byteswritten dw 0 ; # of bytes written in DOSWRITE()
- mselector dw 0 ; selector for memory segment
-
- memavail label DWORD
- memavailhi dw 0 ; for DOSMEMAVAIL() call
- memavaillo dw 0
-
- fileinfobuf label WORD
- create dd ?
- lastaccess dd ?
- lastwrite dd ?
- endofdata dd ?
- allocation dd ?
- fileattrib dw ?
- FIBUFSIZE EQU $-fileinfobuf
-
- ;_DATA ENDS
-
-
-
- .CODE
- ;_TEXT SEGMENT
- ;IGROUP GROUP _TEXT
-
- extrn DOSALLOCSEG:FAR
- extrn DOSMEMAVAIL:FAR
- extrn DOSFREESEG:FAR
- extrn DOSOPEN:FAR
- extrn DOSCLOSE:FAR
- extrn DOSREAD:FAR
- extrn DOSWRITE:FAR
- extrn DOSQFILEINFO:FAR
- extrn DOSSETFILEINFO:FAR
-
-
- ;
- ; openfile <filename, @filehandle, @faction, attribute, flags, mode>
- ;
- openfile MACRO fname,fhandle,faction,fattrib,fflag,fmode
- push ds ; filename
- mov ax,fname
- push ax
- push ds ; handle
- mov ax,fhandle
- push ax
- push ds ; actiontaken
- mov ax,faction
- push ax
- xor ax,ax ; new file size
- push ax
- push ax
- mov ax,fattrib ; file attribute
- push ax
- mov ax,fflag ; open flags
- push ax
- mov ax,fmode ; open mode
- push ax
- xor ax,ax ; reserved
- push ax
- push ax
- call DOSOPEN
- or ax,ax ; set result flags
- ENDM
-
-
- PAGE
- ; +-------------------------------------------------------------------+
- ; | docopyfile(from,to) |
- ; | copy file from from to to |
- ; | errors returned: (actual error code is in _errno) |
- ; | 1: error opening source file |
- ; | 2: error opening dest file |
- ; | 3: error reading source file |
- ; | 4: error writing dest file |
- ; | 5: error allocating memory |
- ; +-------------------------------------------------------------------+
-
- public _docopyfile
-
- _docopyfile proc near
-
- push bp
- mov bp,sp
- cmp word ptr alloced,0
- jne do_it_now ; all set to go
-
- IFDEF OS2ONLY
- ; first see how much memory we can get
- push ds
- mov ax,offset memavail
- push ax
- call DOSMEMAVAIL
- or ax,ax ; check for error
- jnz allocerror
-
- ; now prepare to request memory for our use
- ; note that the memory available can change before this!
- mov bx,0FFFFh ; almost 64K
- cmp memavailhi,0 ; more than 64k available?
- jnz doalloc ; yes: go request [BX]
- cmp memavaillo,bx ; less than what we want?
- jge doalloc ; no: go request [BX]
- mov bx,word ptr memavaillo ; request what is available
-
- ELSE ; IF OS2ONLY
- mov bx,0FFFFh ; request just under 64kb
- ENDIF ;;
-
-
- ; now request [BX] bytes of memory
- doalloc: push bx
- push ds
- mov ax,offset mselector
- push ax
- xor ax,ax ; allocation flags
- push ax
- call DOSALLOCSEG
- or ax,ax ; any error
- jz got_mem ; no: go ahead and process
-
- allocerror: jmp alloc_mem_err
-
- got_mem: mov word ptr msize,bx ; save # of bytes allocated
- mov word ptr alloced,1 ; set flag
-
- ; we have allocated our memory, now we can open files
- do_it_now: openfile [BP+4],<OFFSET inhandle>,<OFFSET action>,0,1,20h
- jz open_src_ok
- jmp open_src_err
-
- open_src_ok: openfile [BP+6],<OFFSET outhandle>,<OFFSET action>,0,12h,12h
- jz copy_loop
- jmp open_dst_err ; error opening dest
-
- ; copy data in file
- copy_loop:
- ; read data from source file
- mov ax,word ptr inhandle ; file handle
- push ax
- mov ax,word ptr mselector ; read to mselector:0000h
- push ax
- xor ax,ax
- push ax
- mov word ptr bytesread,ax ; zero out # of bytes read
- mov ax,word ptr msize ; # of bytes to read
- push ax
- mov ax,offset bytesread ; # of bytes actually read
- push ds
- push ax
- call DOSREAD
- or ax,ax ; any errors?
- jz read_okay ; no
- jmp read_src_err ; yes
-
- read_okay: mov ax,word ptr bytesread
- cmp ax,0 ; at end of file?
- je at_eof ; yup.
-
- ; now write out the buffer
- mov ax,word ptr outhandle ; output handle
- push ax
- mov ax,word ptr mselector ; buffer at mselector:0000h
- push ax
- xor ax,ax
- push ax
- mov word ptr byteswritten,ax ; zero out # of bytes written
- mov ax,word ptr bytesread ; # of bytes to write
- push ax
- mov ax,offset byteswritten ; # of bytes written
- push ds
- push ax
- call DOSWRITE
- or ax,ax ; any error
- jnz write_dst_err ; couldn't write
-
- mov bx,word ptr bytesread
- cmp word ptr byteswritten,bx ; wrote the same amt read?
- jne write_dst_err
- jmp copy_loop
-
- at_eof:
- ; copy file creation date, date of last access, and date of last write
- ; read data about input file
- mov ax,word ptr inhandle ; input handle
- push ax
- mov ax,0001h ; fileinfolevel
- push ax
- mov ax,offset fileinfobuf ; addr of buffer for data
- push ds
- push ax
- mov ax,FIBUFSIZE ; fileinfobuf size
- push ax
- call DOSQFILEINFO
- or ax,ax ; any error?
- jnz read_src_err
-
- ; now set the file info of the output file
- mov ax,word ptr outhandle ; output handle
- push ax
- mov ax,0001h ; fileinfolevel
- push ax
- mov ax,offset fileinfobuf ; addr of buffer for data
- push ds
- push ax
- mov ax,000Ch ; size of buffer
- push ax
- call DOSSETFILEINFO
- or ax,ax
- jnz write_dst_err ; errors...
-
- ; close files
-
- ; close input file
- mov ax,word ptr inhandle
- push ax
- call DOSCLOSE
- or ax,ax
- jnz read_src_err
-
- ; close output file
- mov ax,word ptr outhandle
- push ax
- call DOSCLOSE
- or ax,ax
- jnz write_dst_err
-
- xor ax,ax ; our result code = 0000h
-
- copy_exit: clc
- pop bp
- ret
-
-
- open_src_err: mov word ptr _errno, ax
- mov ax,1
- jmp short copy_exit
- open_dst_err: mov word ptr _errno, ax
- mov ax,2
- jmp short copy_exit
- read_src_err: mov word ptr _errno, ax
- mov ax,3
- jmp short copy_exit
- write_dst_err: mov word ptr _errno, ax
- mov ax,4
- jmp short copy_exit
- alloc_mem_err: mov word ptr _errno, ax
- mov ax,5
- jmp short copy_exit
-
- _docopyfile endp
-
-
- ;_TEXT ENDS
- END
-