home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advos2 / ch09 / schex.asm < prev    next >
Encoding:
Assembly Source File  |  1988-12-12  |  5.5 KB  |  146 lines

  1.         title   SCHEX Example directory search program
  2.         page    55,132
  3.         .286
  4.  
  5. ; SCH-EX.ASM   Example of OS/2 directory search.
  6. ;              Finds and displays names of all files
  7. ;              in current directory with extension .ASM.
  8. ; Copyright (C) 1988 Ray Duncan 
  9. ;
  10. ; Build:        MASM SCHEX;
  11. ;               LINK SCHEX,,,OS2,SCHEX
  12. ;
  13. ; Usage:        SCHEX
  14.  
  15. stdin   equ     0                       ; standard input handle
  16. stdout  equ     1                       ; standard output handle
  17. stderr  equ     2                       ; standard error handle
  18.  
  19. cr      equ     0dh                     ; ASCII carriage return
  20. lf      equ     0ah                     ; ASCII line feed
  21.  
  22.         extrn   DosExit:far
  23.         extrn   DosFindClose:far
  24.         extrn   DosFindFirst:far
  25.         extrn   DosFindNext:far
  26.         extrn   DosWrite:far
  27.  
  28. DGROUP  group   _DATA
  29.  
  30. _DATA   segment word public 'DATA'
  31.  
  32. fname   db      '*.ASM',0               ; target name for search
  33.  
  34. sbuf    db      64 dup (?)              ; receives search results
  35. sbuf_len equ $-sbuf                     ; length of result buffer
  36.  
  37. dirhan  dw      ?                       ; receives search handle
  38. schcnt  dw      ?                       ; receives match count
  39. wlen    dw      ?                       ; receives bytes written
  40. counter dw      0                       ; number of files matched
  41.  
  42. newline db      cr,lf                   ; carriage return-linefeed
  43. newline_len equ $-newline
  44.  
  45. msg1    db      cr,lf
  46.         db      'No matching files.'
  47.         db      cr,lf
  48. msg1_len equ $-msg1
  49.  
  50. _DATA   ends
  51.  
  52.  
  53. _TEXT   segment word public 'CODE'
  54.  
  55.         assume  cs:_TEXT,ds:DGROUP
  56.  
  57. main    proc    far                     ; entry point from OS/2
  58.  
  59.         mov     dirhan,-1               ; request new search handle
  60.         mov     schcnt,1                ; set max match count
  61.  
  62.                                         ; search for first match...
  63.         push    ds                      ; search target filename
  64.         push    offset DGROUP:fname
  65.         push    ds                      ; receives search handle
  66.         push    offset DGROUP:dirhan
  67.         push    0                       ; attribute=normal files
  68.         push    ds                      ; result buffer address
  69.         push    offset DGROUP:sbuf
  70.         push    sbuf_len                ; result buffer length
  71.         push    ds                      ; receives match count
  72.         push    offset DGROUP:schcnt                    
  73.         push    0                       ; reserved DWORD 0
  74.         push    0
  75.         call    DosFindFirst            ; transfer to OS/2
  76.  
  77.         or      ax,ax                   ; any files found?
  78.         jnz     main2                   ; no, exit
  79.  
  80.         cmp     schcnt,1                ; confirm match was found
  81.         jne     main2                   ; no, exit
  82.         
  83. main1:                                  ; match was found...
  84.         inc     counter                 ; count matching files
  85.  
  86.                                         ; display one filename...
  87.         push    stdout                  ; standard output handle
  88.         push    ds                      ; file name address
  89.         push    offset DGROUP:sbuf+17h
  90.         mov     al,byte ptr sbuf+16h    ; file name length
  91.         xor     ah,ah
  92.         push    ax
  93.         push    ds                      ; receives bytes written
  94.         push    offset DGROUP:wlen
  95.         call    DosWrite                ; transfer to OS/2
  96.  
  97.                                         ; send new-line sequence
  98.         push    stdout                  ; standard output handle
  99.         push    ds                      ; new-line sequence address
  100.         push    offset DGROUP:newline
  101.         push    newline_len             ; new-line sequence length
  102.         push    ds                      ; receives bytes written
  103.         push    offset DGROUP:wlen
  104.         call    DosWrite                ; transfer to OS/2
  105.  
  106.                                         ; search for next file...
  107.         push    dirhan                  ; search handle
  108.         push    ds                      ; result buffer address
  109.         push    offset DGROUP:sbuf
  110.         push    sbuf_len                ; result buffer length
  111.         push    ds                      ; receives match count
  112.         push    offset schcnt                   
  113.         call    DosFindNext             ; transfer to OS/2
  114.  
  115.         or      ax,ax                   ; any more files?
  116.         jnz     main2                   ; no, exit
  117.  
  118.         cmp     schcnt,1                ; confirm match was found 
  119.         je      main1                   ; yes, proceed
  120.  
  121. main2:  push    dirhan                  ; release search handle...
  122.         call    DosFindClose            ; transfer to OS/2
  123.  
  124.         cmp     counter,0               ; any files found?
  125.         jne     main3                   ; yes
  126.  
  127.                                         ; no, display 'no matches'
  128.         push    stdout                  ; standard output handle
  129.         push    ds                      ; message address
  130.         push    offset DGROUP:msg1
  131.         push    msg1_len                ; message length
  132.         push    ds                      ; receives bytes written
  133.         push    offset DGROUP:wlen
  134.         call    DosWrite                ; transfer to OS/2
  135.  
  136. main3:  push    1                       ; terminate all threads
  137.         push    0                       ; return success code
  138.         call    DosExit                 ; exit program
  139.  
  140. main    endp
  141.  
  142. _TEXT   ends
  143.  
  144.         end     main
  145.  
  146.