home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / MISC / THELPPAT.ZIP / FFC.ASM next >
Encoding:
Assembly Source File  |  1990-08-02  |  5.0 KB  |  96 lines

  1. ;------ FindFirst() -------------------------------------------- COMPACT ------
  2. ;
  3. ;       by Christopher Antos
  4. ;
  5. ;       This is a replacement function for the Turbo C++ findfirst() function,
  6. ;       which has a problem with setting the _doserrno global variable after an
  7. ;       unsuccessful search.  As packaged with Turbo C++, the function sets
  8. ;       _doserrno to 87, no matter what the error is (or even if there is no
  9. ;       error).
  10. ;
  11. ;       This replacement routine performs the same function as the original
  12. ;       findfirst() routine, but it correctly sets _doserrno if there is an
  13. ;       error.  If there is no error, _doserrno retains its former value.
  14. ;       The global variable _errno is not set by this function.
  15. ;
  16. ;
  17. ;       int FindFirst( char *pathname, struct ffblk *fb, int attrib )
  18. ;
  19. ;------------------------------------------------------------------------------
  20.  
  21.  
  22.         DOSSEG                                          ; select Turbo C++ segment ordering
  23.         .MODEL  compact,C                               ; select compact model (near code far data)
  24.  
  25.  
  26.         .DATA                                           ; external data
  27.         EXTRN   C       _doserrno:WORD                  ; Turbo C++ global variable
  28.  
  29.  
  30.         .CODE
  31.         PUBLIC  C       FindFirst                       ; so C++ program can use this function
  32.  
  33. FindFirst       PROC    C       NEAR
  34.  
  35.         ; declare arguments, allocate local variables, and save stack context
  36.         ARG     pathname:DWORD, fb:DWORD, attrib:WORD   ; arguments passed to function
  37.         LOCAL   DTAoffset:WORD, DTAsegment:WORD = LSIZE ; declare local variables
  38.  
  39.         ; save address of DTA
  40.         mov     ah,2fh                                  ; DOS function 2Fh
  41.         int     21h                                     ; get current DTA address
  42.         mov     [DTAoffset],bx                          ; save DTA offset
  43.         mov     [DTAsegment],es                         ; save DTA segment
  44.  
  45.         ; point new DTA to [fb]
  46.         push    ds                                      ; save ds
  47.         mov     ah,1ah                                  ; DOS function 1Ah
  48.         mov     dx,WORD PTR [fb]                        ; load offset of new DTA
  49.         mov     ds,WORD PTR [fb+2]                      ; load segment of new DTA
  50.         int     21h                                     ; set new DTA
  51.         pop     ds                                      ; restore ds
  52.  
  53.         ; call the DOS "Find first file" function
  54.         push    ds                                      ; save ds
  55.         mov     ah,4eh                                  ; DOS function 4Eh
  56.         mov     cx,[attrib]                             ; give attributes to match
  57.         mov     dx,WORD PTR [pathname]                  ; give offset of ASCIIZ pathname buffer
  58.         mov     ds,WORD PTR [pathname+2]                ; give segment of ASCIIZ pathname buffer
  59.         int     21h                                     ; find first file matching given attributes and wildcard
  60.         jc      @@error                                 ; if carry set, handle error
  61.         xor     ax,ax                                   ; zero ax register (FindFirst()==0 on success)
  62.         push    ax                                      ; push return value on stack
  63.         jmp     @@exit                                  ; return
  64.  
  65.         ; set _doserrno and return -1
  66. @@error:
  67.         pop     ds                                      ; restore ds
  68.         push    ds                                      ; put it back (we pop it again below)
  69.         mov     [_doserrno],ax                          ; load _doserrno with error code
  70.         mov     ax,-1                                   ; set ax to -1 (FindFirst()==-1 on error)
  71.         push    ax                                      ; push return value on stack
  72.  
  73.         ; restore DTA and stack context, then return
  74. @@exit:
  75.         pop     ax                                      ; pop to get to ds
  76.         pop     ds                                      ; restore ds
  77.         push    ds                                      ; put it back (we pop it again below)
  78.         push    ax                                      ; ditto
  79.         mov     ah,1ah                                  ; DOS function 1Ah
  80.         mov     dx,[DTAoffset]                          ; load DTA offset
  81.         mov     ds,[DTAsegment]                         ; load DTA segment
  82.         int     21h                                     ; restore DTA address
  83.         pop     ax                                      ; load return value from stack into ax
  84.         pop     ds                                      ; restore ds
  85.  
  86.         ; return (TASM automatically restores stack context and deallocates local variables)
  87.         ret                                             ; return to caller
  88.  
  89. FindFirst       ENDP
  90.  
  91.  
  92.         END                                             ; end of assembly source file
  93.  
  94.  
  95. ;------ EOF -------------------------------------------------------- EOF ------
  96.