home *** CD-ROM | disk | FTP | other *** search
- ;------ FindFirst() -------------------------------------------- COMPACT ------
- ;
- ; by Christopher Antos
- ;
- ; This is a replacement function for the Turbo C++ findfirst() function,
- ; which has a problem with setting the _doserrno global variable after an
- ; unsuccessful search. As packaged with Turbo C++, the function sets
- ; _doserrno to 87, no matter what the error is (or even if there is no
- ; error).
- ;
- ; This replacement routine performs the same function as the original
- ; findfirst() routine, but it correctly sets _doserrno if there is an
- ; error. If there is no error, _doserrno retains its former value.
- ; The global variable _errno is not set by this function.
- ;
- ;
- ; int FindFirst( char *pathname, struct ffblk *fb, int attrib )
- ;
- ;------------------------------------------------------------------------------
-
-
- DOSSEG ; select Turbo C++ segment ordering
- .MODEL small,C ; select small model (near code near data)
-
-
- .DATA ; external data
- EXTRN C _doserrno:WORD ; Turbo C++ global variable
-
-
- .CODE
- PUBLIC C FindFirst ; so C++ program can use this function
-
- FindFirst PROC C NEAR
-
- ; declare arguments, allocate local variables, and save stack context
- ARG pathname:WORD, fb:WORD, attrib:WORD ; arguments passed to function
- LOCAL DTAoffset:WORD, DTAsegment:WORD = LSIZE ; declare local variables
-
- ; save address of DTA
- mov ah,2fh ; DOS function 2Fh
- int 21h ; get current DTA address
- mov [DTAoffset],bx ; save DTA offset
- mov [DTAsegment],es ; save DTA segment
-
- ; point new DTA to [fb]
- mov ah,1ah ; DOS function 1Ah
- mov dx,WORD PTR [fb] ; load offset of new DTA
- int 21h ; set new DTA
-
- ; call the DOS "Find first file" function
- mov ah,4eh ; DOS function 4Eh
- mov cx,[attrib] ; give attributes to match
- mov dx,WORD PTR [pathname] ; give offset of ASCIIZ pathname buffer
- int 21h ; find first file matching given attributes and wildcard
- jc @@error ; if carry set, handle error
- xor ax,ax ; zero ax register (FindFirst()==0 on success)
- push ax ; push return value on stack
- jmp @@exit ; return
-
- ; set _doserrno and return -1
- @@error:
- mov [_doserrno],ax ; load _doserrno with error code
- mov ax,-1 ; set ax to -1 (FindFirst()==-1 on error)
- push ax ; push return value on stack
-
- ; restore DTA and stack context, then return
- @@exit:
- push ds ; save ds
- mov ah,1ah ; DOS function 1Ah
- mov dx,[DTAoffset] ; load DTA offset
- mov ds,[DTAsegment] ; load DTA segment
- int 21h ; restore DTA address
- pop ds ; restore ds
- pop ax ; load return value from stack into ax
-
- ; return (TASM automatically restores stack context and deallocates local variables)
- ret ; return to caller
-
- FindFirst ENDP
-
-
- END ; end of assembly source file
-
-
- ;------ EOF -------------------------------------------------------- EOF ------
-