home *** CD-ROM | disk | FTP | other *** search
/ ftp.shrubbery.net / 2015-02-07.ftp.shrubbery.net.tar / ftp.shrubbery.net / pub / pc / unix / unx.arc / FFIND.ASM < prev    next >
Assembly Source File  |  1985-06-10  |  4KB  |  166 lines

  1. page 66,132
  2. ;compressed mode on prn.
  3. page
  4.  
  5.  
  6. ;---- Ffind.obj ------------------------------------------------
  7. ; For those of you who like to use wildcards:
  8. ; allows pathnames for find first.  Makes FIND NEXT again wonderful.
  9. ;
  10. ; MCN  3 July 84
  11. ; To use these routines, include the following lise in caller's file:
  12. ;    EXTRN    FFIRST:NEAR,FNEXT:NEAR,FNAM:BYTE
  13. ; To call, load registers as for DOS function calls 4EH and 4FH, and
  14. ;    CALL    FFIRST        ( or FNEXT )
  15. ; And then to actually use the found filespec, (e.g. open that file):
  16. ;    LEA    DX, FNAM    ; get ptr to found filespec
  17. ;    MOV    AH, 3DH        ; DOS for OPEN FILE
  18. ;    MOV    AL, 0        ; open for, say, read access
  19. ;    INT    21H
  20. ; Further examples can be found in \bin\asm\TOUCH.ASM etc.
  21. ;---------------------------------------------------------------
  22.  
  23. public    ffirst, fnext, fnam, finder
  24. extrn    new:near
  25.  
  26. ffnam    equ    30        ; filename goes to dta + 30d
  27.  
  28. code    segment    public 'CODE'
  29. assume cs:code,ds:code,es:code
  30.  
  31. ;---- Ffirst: --------------------------------------------------------------
  32. ; Finds first file with filespec pointed to by DS:DX, attribs in CX.
  33. ; Just like DOS fn 4EH, but also returns full filespec in buffer FNAM.
  34. ; Either backslashes or forward slashes may be used in a pathname.
  35. ; All registers preserved.
  36.  
  37. ffirst proc    near
  38.  
  39.     push    ax
  40.     push    bx
  41.     push    si
  42.     push    di
  43.     push    es
  44.  
  45.     push    cx        ; allocate room for our finder
  46.     push    dx
  47.     mov    cx, 44
  48.     call    new
  49.     mov    finder, bx
  50.     mov    ah, 1ah
  51.     mov    dx, bx
  52.     int    21H        ; set DTA
  53.     pop    dx
  54.     pop    cx
  55.  
  56.     ; Now copy the goddamned drive and path.  Of course,
  57.     ; we don't know yet how much is path and how much is
  58.     ; filename. So, we will copy the whole filespec, and
  59.     ; just remember the last char in the set [:,/,\], the
  60.     ; default memory being of childhood.
  61.  
  62.     ; Pointer to text in DS:DX.  Put it in SI; 
  63.     ; put pointer to our buffer in DI.
  64.     mov    si,dx
  65.     lea    di,fnam
  66.  
  67.     mov    fnf, di        ; save offset of beginning of filename
  68.     dec    fnf
  69.  
  70.     ; copy until we find a null; remember location of any [:,/,\].
  71. copyem:
  72.         lodsb
  73.         cmp    al,':'
  74.         jz    foundone
  75.         cmp    al,'/'
  76.         jz    foundone
  77.         cmp    al,'\'
  78.         jnz    nope
  79. foundone:
  80.         mov    fnf,di
  81. nope:
  82.         stosb
  83.         or    al,al        ; copy until null...
  84.         jnz    copyem
  85.     inc    fnf        ; point to region AFTER last metachar.
  86.  
  87.     ; Whee, that was fun.  Now call DOS to find first.
  88.     ; Find first requires pointer to filespec in DX. Fortumately, s'there.
  89.     mov    ah, 4eh
  90.     int    21h        ; call DOS
  91.     jc    nofile        ; if carry set, no file found.
  92.  
  93.     ; Following a FIND, we must copy the found whizdoodle to 
  94.     ; *fnf.
  95.     jmp    short copynam
  96.  
  97. ffirst    endp
  98.  
  99.  
  100. ;--- Fnext: -----------------------------------------------------------
  101. ; Fnext is called just like the DOS function FIND NEXT, but it
  102. ; places the path and filename in the read-only buffer FNAM.
  103. ; Don't need to set AH.
  104. ; All registers preserved.
  105.  
  106. fnext    proc    near
  107.  
  108.     push    ax
  109.     push    bx
  110.     push    si
  111.     push    di
  112.     push    es
  113.  
  114.     mov    ah, 4fh        ;ask DOS for the next file
  115.     int    21h
  116.     jc    nofile
  117.  
  118. copynam:
  119.     ; 1. Get DTA into ES:BX.
  120.     mov    ah, 2fh
  121.     int    21h
  122.  
  123.     ; 2. SI= ES ptr to what FIND returns, at DTA + FFNAM.
  124.     mov    si, bx
  125.     add    si, ffnam
  126.  
  127.     ; 3. DI= DS ptr to old pathspec, at *fnf.
  128.     mov    di, fnf        ; ptr to end of pathspec
  129.  
  130.     ; 4. Append the file name to the pathspec.
  131.     ; What should be a Move String is horribly complicated.
  132.     ; We need to swap segment registers....
  133.     push    ds
  134.     push    es
  135.     pop    ds
  136.     pop    es
  137.  
  138. copyl:
  139.         lods    byte ptr [SI]    ; (Default seg reg is DS.)
  140.         or    al,al
  141.         stos    byte ptr [DI]    ; (Default seg reg is ES.)
  142.  
  143.         jnz    copyl
  144.  
  145.     push    es
  146.     pop    ds
  147.     ; Don't need to restore DTA's ES, as we don't need it anymore.
  148.  
  149.     clc            ; return "no errors"
  150. nofile:
  151.     pop    es
  152.     pop    di
  153.     pop    si
  154.     pop    bx
  155.     pop    ax
  156.     ret
  157. fnext    endp
  158.  
  159. ;------- Data area ------------------
  160. fnf    dw    ?        ; points to file name field in filespec below
  161. finder    dw    ?        ; points to DTA we set up.
  162.  
  163. fnam    db    90 dup (?)
  164. code    ends
  165.     end
  166.