home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / dirutl / purgtre1.arc / PURGE.ASM next >
Encoding:
Assembly Source File  |  1987-02-16  |  10.0 KB  |  462 lines

  1. ;------------------------------------------------------------------------
  2. ; purge.asm    from "PC MAGAZIN" #3 vom 8-Jan-86
  3. ;        plus quite a few hours from a non-assembler-programmer
  4. ;        Hans ZURFCC::ZANGGER, SWAS Kloten
  5. ;
  6. ;    BUG:    if purge with just a trailing space and CR is given
  7. ;        beautiful things happen (why not reboot from time to time?)
  8. ;
  9. ;        ..Was in PARSE which didn't check for End-of-String
  10. ;
  11. ;    Code - by the way is VERY CLUMSY - this ain't one of the better
  12. ;    examples of assembly language coding ...
  13. ;
  14. ;    Since code 'rides' on assumption, that 'STACK' handling is supplied
  15. ;    by 'lower-levels' - U need to follow :
  16. ;    MASM PURGE;        !Assemble
  17. ;    LINK PURGE;        !Link and ignore Warning about missing STACK
  18. ;    EXE2BIN PURGE.EXE PURGE.COM ! Make it non-relocatable/-sharable
  19. ;    DEL PURGE.EXE        !Get rid of LINK results
  20. ;    DEL PURGE.OBJ        !.. and MASM results.
  21. ;                            {B.E. 26-Nov-86}
  22. ;------------------------------------------------------------------------
  23. ;
  24. cseg    segment 'code'
  25.     assume cs:cseg, ds:cseg, es:cseg, ss:cseg
  26.  
  27.     org 5ch
  28. drive    equ this byte
  29.  
  30.     org 80h
  31. command_tail   equ this byte
  32.  
  33.     org 100h
  34.  
  35. last_byte    equ offset buffer
  36. found_file    equ offset fcb + 30
  37. normal        equ 20h
  38. dir        equ 10h
  39. ;
  40. display macro    string
  41.     mov    dx, offset string
  42.     mov    ah, 09h
  43.     int    21h
  44. endm
  45. ;
  46. read_kbd  macro
  47.     mov    ah, 08h
  48.     int    21h
  49. endm
  50. ;
  51. display_char  macro  character
  52.     mov    dl, character
  53.     mov    ah, 02h
  54.     int    21h
  55. endm
  56. ;
  57. ;------------------------------------------------------------------------
  58. main:    jmp entry
  59. ;------------------------------------------------------------------------
  60. help_msg    db 13, 10, 'Purge files (Nov 86 hz).', 09h
  61.         db 'Enter Y, N or F11 (ESC)', 13, 10, '$'
  62. spruch        db 'Delete file: $'
  63. bad_vers    db 'Wrong DOS Version (must be V2.xx or higher)$'
  64. bad_drive    db 'Bad Drive$'
  65. r_only        db 'Read Only$'
  66. i_path        db 'Invalid path...$'
  67. syntax_error    db 13, 10, 'Use PURGE [drv:]filnam.ext$'
  68. search_string    db 0, ':', 77 dup (0), '$'
  69. file_name    db 13 dup (0)
  70. kill_file    db 80 dup (0)
  71. found_flag    db 0
  72. dir_level    dw 0
  73. len        db 0
  74. stardotstar    db '*.*', 0
  75. dta_pointer    dw last_byte + 64
  76. fcb        db 43 dup (0)
  77. crlf        db 13, 10, '$'
  78. string        db 2 dup (?)        ;1 char + CR
  79. prompt        db ' ?', 08h, '$'
  80. deleted        db 'Yes$'
  81. notdeleted    db 'No$'
  82. abort_it    db 'aborted$'
  83. space_1        db '.'
  84. space_2        db ' '
  85. temp        db '   $'
  86.  
  87. ;------------------------------------------------------------------------
  88. entry:
  89.     cmp    byte ptr [command_tail], 0
  90.     jne    bytes_da
  91.     display syntax_error
  92.     jmp    short out
  93.  
  94. bytes_da:
  95.     cmp    al, 0ffh
  96.     jnz    drive_ok
  97.     lea    dx, bad_drive
  98.  
  99. err_end:
  100.     mov    ah, 09h
  101.     int    21h
  102. out:    mov    ah, 4ch
  103.     int    21h
  104.  
  105. drive_ok:
  106.     mov    ah, 30h
  107.     int    21h
  108.     cmp    al, 02h
  109.     jnb    vers_ok
  110.     lea    dx, bad_vers
  111.     jmp    short err_end
  112.  
  113. vers_ok:
  114.     display help_msg
  115.     mov    al, [drive]
  116.     or    al, al
  117.     jnz    not_default
  118.     mov    ah, 19h
  119.     int    21h
  120.     inc    al
  121.  
  122. not_default:
  123.     add    al, 40h
  124.     mov    search_string[0], al
  125.     mov    cl, byte ptr [command_tail]
  126.     xor    ch, ch
  127.     mov    si, cx
  128.     add    si, offset command_tail
  129.     mov    di, offset file_name
  130.     mov    bl, '\'
  131.     mov    bh, ':'
  132.     dec    cx
  133.     call    parser
  134.     mov    byte ptr [di], 0
  135.     inc    si
  136.     cmp    byte ptr [si], '\'
  137.     je    get_path
  138.     mov    di, offset search_string + 2
  139.     jmp    short entry_done
  140.     or    cx, cx
  141.     jnz    get_path
  142.     mov    di, offset search_string + 2
  143.     jmp    short entry_done
  144.  
  145. get_path:
  146.     mov    di, offset search_string + 2    ;parse path
  147.     mov    bl, 20h                ;stop at space
  148.     inc    cx
  149.     call    parser                ;read path
  150.  
  151. entry_done:
  152.     display crlf
  153.     xor    bx, bx
  154.     mov    word ptr [bx + last_byte], di    ;write first parameter
  155.                         ; in pointer chain
  156.     mov    dx, offset search_string    ;pointer to search string
  157.                         ; for find_files
  158.     mov    si, offset file_name
  159.     call    find_files            ;search first level
  160.  
  161. set_up_dir_find:
  162.     mov    bx, dir_level            ;dir_level points to pointer
  163.                         ; table of offsets for current
  164.                         ;  path
  165.     shl    bx, 1
  166.     mov    di, word ptr [bx + last_byte]    ;point to end
  167.     mov    si, offset stardotstar        ;current search pat in di
  168.     mov    cx, 4                ;add *.* to it
  169. rep    movsb
  170.  
  171. find_dirs:
  172.     mov    dx, dta_pointer
  173.     mov    ah, 1ah                ;function set dta
  174.     int    21h
  175.     cmp    found_flag, 1            ;found something before?
  176.     jz    find_next
  177.     mov    dx, offset search_string    ;else set pointer and
  178.     mov    cx, dir                ; attribute and search
  179.     mov    ah, 4eh
  180.     int    21h
  181.     jmp    short check_carry
  182.  
  183. find_next:
  184.     mov    ah, 4fh                ;find next
  185.     int    21h
  186.  
  187. check_carry:
  188.     jnc    found_one            ;no carry -> found entry
  189.     jmp    check_level            ;searched all dirs yet?
  190.  
  191. ;found entry processing and discarding of . and ..
  192. found_one:
  193.     mov    si, dta_pointer            ;point to found entry
  194.     add    si, 1eh
  195.     cmp    byte ptr [si - 1eh + 15h], dir  ;is it a directory?
  196.     jne    find_next
  197.     cmp    byte ptr [si], '.'        ;not the ones we want
  198.     je    find_next
  199.     mov    found_flag, 1
  200.     xor    ah, ah
  201.     mov    di, word ptr [bx + last_byte]    ;point to end of search_string
  202.  
  203. ;process string
  204. look4z:
  205.     lodsb                    ;read chars
  206.     cmp    ah, al                ;null char found?
  207.     je    found_z                ;yes
  208.     stosb                    ;move byte
  209.     jmp    short look4z
  210.  
  211. found_z:
  212.     mov    byte ptr [di], '\'        ;append backslash
  213.     inc    di
  214.     add    bx, 2
  215.     mov    word ptr [bx + last_byte], di    ;update pointer table
  216.     mov    dx, offset search_string    ;set pointer for find_files
  217.     mov    si, offset file_name
  218.     call    find_files
  219.  
  220. ;update pointers and variables, search next directory entry
  221. ;
  222. update:
  223.     add    dta_pointer, 43            ;update dta pointer
  224.     inc    dir_level            ;down one level
  225.     mov    found_flag, 0            ;start on new level
  226.     jmp    set_up_dir_find
  227.  
  228. check_level:
  229.     dec    dir_level
  230.     cmp    dir_level, -1
  231.     jz    end                ;this stops the game
  232.     sub    dta_pointer, 43            ;else adjust dta pointer
  233.     mov    found_flag, 1
  234.     jmp    set_up_dir_find            ; and continue searching
  235. end:
  236.     mov    ah, 4ch
  237.     int    21h
  238.  
  239. ;------------------------------------------------------------------------
  240. parser  proc    near
  241. ;------------------------------------------------------------------------
  242.     xor    ax, ax
  243.     push    ax                ;null as mark onto stack
  244.     std                    ;set reverse
  245.  
  246. parse_loop:
  247.     xor    ax, ax
  248.     lodsb                    ;load byte in al
  249.             ;First check if we're at end-of-string {B.E}
  250.     cmp    al, 0                ;is it NULL ? {B.E.}
  251.     je    parse_done
  252.     cmp    al, bh                ;Scan for 1st requ. Break-Char
  253.     je    parse_done
  254.     cmp    al, bl                ;Scan for 2nd one
  255.     je    parse_done
  256.     cmp    al, 'z'                ;Check for 'lower-case'
  257.     ja    push_byte
  258.     cmp    al, 'a'                ;Not really neccessary .. but .                                ;since DOS uppercases already
  259.     jb    push_byte
  260.     xor    al, 20h                ;UPPERcase
  261.  
  262. push_byte:
  263.     push    ax
  264.     loop    parse_loop
  265.  
  266. parse_done:
  267.     cld                    ;set forward
  268. pop_ax: pop    ax
  269.     cmp    ax, 0
  270.     je    end_parse
  271.     stosb
  272.     jmp    short pop_ax
  273. end_parse:
  274.     ret
  275. parser  endp
  276.  
  277. ;------------------------------------------------------------------------
  278. find_files proc near
  279. ;------------------------------------------------------------------------
  280. ;    dx points to current pathname
  281. ;    si    "   "  file name (search pattern)
  282. ;    di    "   "  first byte after pathname
  283. ;------------------------------------------------------------------------
  284.  
  285.     mov    bp, dx                ;save dx
  286.     mov    bx, di                ;save di
  287.     mov    dx, offset fcb
  288.     mov    ah, 1ah                ;set dta
  289.     int    21h
  290.     mov    cx, 13                ;add 13 chars after pathname
  291. rep    movsb
  292.     xor    al, al
  293.     stosb                    ;make asciiz
  294.     mov    dx, bp
  295.     mov    cx, normal
  296.     mov    ah, 4eh                ;find first
  297.     int    21h
  298.     jc    not_found
  299. ;
  300. show_it:
  301.     mov    byte ptr [bx], 0        ;mark end of pathname
  302.     mov    dx, bp
  303. ;+++
  304.     cld
  305.     mov    si, bp
  306.     mov    di, offset kill_file
  307.     mov    cx, 64
  308. rep    movsb
  309.     mov    di, offset kill_file
  310.     mov    cx, 64
  311.     xor    al, al
  312. repne    scasb                    ;find end of pathname
  313.     dec    di
  314.     push    di
  315. ;---
  316.     mov    dx, found_file            ;extract filename
  317.     mov    di, offset fcb            ;dest for stosb
  318.     add    di, 30                ;skip over fcb dir entry
  319.                         ; see page 1-136
  320.     xor    al, al                ;clear out al for scasb
  321.     mov    cx, 12                ;move 12 chars
  322. repne    scasb                    ;search for null char
  323. rep    stosb
  324. ;+++
  325.     mov    si, found_file
  326.     pop    di
  327.     mov    cx, 13
  328. rep    movsb
  329.     mov    di, offset kill_file
  330.     mov    cx, 64
  331.     xor    al, al
  332. repne    scasb
  333.     dec    di
  334.     mov    byte ptr [di], '$'
  335.     push    di
  336.     display spruch
  337.     call    lower_case
  338.     display kill_file            ;show pathname
  339.     call    spaces
  340.     pop    di
  341.     mov    byte ptr [di], 0        ;make string asciiz
  342.     call    yesno
  343.     display crlf
  344. ;---
  345.     mov    dx, bp
  346.     mov    ah, 4fh                ;find next
  347.     int    21h
  348.     jnc    show_it
  349.  
  350. not_found:
  351.     ret
  352. find_files    endp
  353.  
  354. ;------------------------------------------------------------------------
  355. delete_file    proc near
  356. ;------------------------------------------------------------------------
  357. ;
  358.     push    ax
  359.     mov    ah, 041h
  360.     mov    dx, offset kill_file
  361.     int    21h
  362.     jnc    success
  363.     cmp    ax, 5                ;is directory or r/o
  364.     je    read_only
  365.     cmp    ax, 2                ;invalid path
  366.     je    inv_path
  367. read_only:
  368.     display r_only
  369.     pop    ax
  370.     ret
  371. inv_path:
  372.     display i_path
  373.     pop    ax
  374.     ret
  375. success:
  376.     pop    ax
  377.     display deleted
  378.     ret
  379.  
  380. delete_file    endp
  381.  
  382. ;------------------------------------------------------------------------
  383. lower_case    proc near
  384. ;------------------------------------------------------------------------
  385. ;
  386.     mov    bx, offset kill_file        ;get string to lowercase
  387. start_lower:
  388.     mov    dl, [bx]
  389.     cmp    dl, '$'
  390.     je    lwr_done            ;end of string?
  391.     cmp    dl, 'A'
  392.     jl    skip_char
  393.     cmp    dl, 'Z'
  394.     jg    skip_char
  395. ;     or     dl, ' '
  396. ;     mov     byte ptr [bx], dl         ;put char back into string
  397. skip_char:
  398.     inc    bx
  399.     loop    start_lower            ;loop for all chars
  400. lwr_done:
  401.     mov    dx, bx
  402.     xor    dh, dh
  403.     mov    len, dl
  404.     ret
  405.  
  406. lower_case    endp
  407.  
  408. ;------------------------------------------------------------------------
  409. yesno    proc near
  410. ;------------------------------------------------------------------------
  411. ;
  412.     push    ax
  413.     display prompt
  414.     mov    cx, 1            ;maximum length of entry (y/n)
  415.     read_kbd
  416. ;display_char al
  417.     cmp    al, 'y'
  418.     je    delete_it
  419.     cmp    al, 'Y'
  420.     je    delete_it
  421.     cmp    al, 27            ;escape or F11
  422.     je    abort_show
  423.     display notdeleted
  424.     pop    ax
  425.     ret
  426.  
  427. delete_it:
  428.     pop    ax
  429.     call    delete_file
  430.     ret
  431.  
  432. abort_show:
  433.     pop    ax
  434.     display abort_it
  435.     display crlf
  436.     jmp    out
  437.  
  438. yesno    endp
  439.  
  440. ;------------------------------------------------------------------------
  441. spaces        proc near
  442. ;------------------------------------------------------------------------
  443. ;
  444.     display_char space_2
  445.     mov    cl, 60
  446.     sub    cl, len
  447.     mov    len, cl
  448.     xor    cx, cx
  449.     mov    cl, len
  450. disp:
  451.     display_char space_1
  452.     loop    disp
  453.     ret
  454. spaces  endp
  455.  
  456. ;------------------------------------------------------------------------
  457. ;
  458. buffer  equ this byte
  459.  
  460. cseg    ends
  461.     end main
  462.