home *** CD-ROM | disk | FTP | other *** search
/ The CIA World Factbook 1992 / k3bimage.iso / sel / 09 / 0012 / kill.asm < prev    next >
Encoding:
Assembly Source File  |  1991-12-02  |  8.9 KB  |  299 lines

  1. ;**
  2. ;*
  3. ;*  Name:        kill.asm
  4. ;*  Version:     1.1
  5. ;*  Description: delete files anywhere on hard disk
  6. ;*  Author:      Paul Roub
  7. ;*
  8. ;*  the commmand line syntax is: KILL <mask>, where mask is any valid
  9. ;*    DOS filename, wildcards included (paths are allowed, but silly:
  10. ;*    the results = DEL <mask>).
  11. ;*
  12. ;*  note: KILL *.* is not allowed
  13. ;*          only directories without extensions are searched, for speed
  14. ;*          purposes - change 'stardot' if this is unsatisfactory
  15. ;*        output is through video BIOS - again, for speed
  16. ;*
  17. ;*  remember to use EXE2BIN, or better yet, EXE2COM
  18. ;*
  19. ;**
  20.  
  21.  
  22. cseg    segment
  23.                 assume  CS:cseg, DS:cseg, ES:cseg, SS:cseg
  24.  
  25.                 ORG     80h
  26. ParamLen        LABEL   BYTE          ; address of command-line length byte
  27.  
  28.                 ORG     100h
  29. start:          JMP     kill
  30.  
  31.  
  32. ;**;
  33. ;**
  34. ;*
  35. ;*  data area
  36. ;*
  37. ;**
  38. victim          db      80 DUP (0)    ; our target
  39.  
  40. starstar        db      "*.*",0       ; for command-line comparison
  41. stardot         db      "*.",0        ; for directory searches
  42. dotdot          db      "..",0        ; for dropping back one directory
  43.  
  44. LevelsIn        db      0             ; subdirectory nesting level
  45. OldDTAofs       dw      1             ; old DTA offset
  46.  
  47. DTAofs          dw      stacktop + 1  ; offset of current DTA
  48.  
  49. space           db      ' '           ; leading space for listing files
  50. drive           db      ' :'          ; drive of killed file
  51. slash           db      '\'
  52. CurDir          db      66 dup (0)
  53. tslash          db      '\', 0
  54. OldDirSl        db      '\'
  55. OldDir          db      66 dup (0)
  56.  
  57.  
  58. MemErrMsg       db      'KILL: Insufficient memory',13,10,0
  59. SuicideMsg      db      '"KILL *.*" not allowed',13,10,0
  60. BadParam        db      'Syntax: KILL mask'
  61. crlf            db      13,10,0
  62.  
  63.  
  64. ;**;
  65. ;**
  66. ;*
  67. ;*  main routine
  68. ;*
  69. ;**
  70. kill proc near
  71.                 mov     sp,offset stacktop   ; adjust our memory allocation
  72.                 mov     bx,offset progend
  73.                 mov     cl,4
  74.                 shr     bx,cl
  75.                 mov     ah,4Ah
  76.                 int     21h
  77.                 jnb     GotMem
  78.  
  79.                 mov     si,offset MemErrMsg  ; if carry set, not enough mem.
  80. ErrExit:        call    WriteStr             ; display error message
  81.                 mov     ax,4C01h             ; and quit with error code 1
  82.                 int     21h
  83.  
  84. GotMem:         mov     cl,[ParamLen]        ; get command line len
  85.                 or      cl,cl
  86.                 jnz     FindStart            ; not zero, go on
  87.  
  88. ParamErr:       mov     si,offset BadParam   ; zero, so point to usage msg
  89.                 jmp     ErrExit              ;   and quit
  90.  
  91. FindStart:      mov     si,0082h             ; start of usable cmd line
  92.                 cld
  93. NextFS:         lodsb                        ; look for first non-space char
  94.                 cmp     al,13                ; if it's CR, forget it
  95.                 jz      ParamErr
  96.  
  97.                 cmp     al,20h
  98.                 jz      NextFS               ; if space, try again
  99.  
  100.                 dec     si
  101.                 push    si
  102. FindEnd:        lodsb                        ; now look for last char
  103.                 cmp     al,20h
  104.                 jz      GotParam
  105.  
  106.                 cmp     al,13
  107.                 jnz     FindEnd
  108.  
  109. GotParam:       pop     di                   ; ofs. of start
  110.                 xchg    si,di
  111.                 dec     di                   ; ofs of end
  112.                 sub     di,si                ; get length
  113.                 mov     cx,di                ; into CX
  114.                 mov     di,offset victim
  115.                 cld
  116.                 repz    movsb                ; store it in 'victim'
  117.                 mov     [di],cl              ; cl is zero, why not use it?
  118.                 mov     cl,4
  119.                 mov     di,offset victim
  120.                 mov     si,offset starstar   ; compare to *.*
  121.                 repz    cmpsb
  122.                 jcxz    NoWay                ; if it's equal, quit
  123.  
  124.                 jmp     ParamSafe
  125.  
  126. NoWay:          mov     si,offset SuicideMsg ; explain quitting...
  127.                 jmp     ErrExit
  128.  
  129. ParamSafe:      mov     si,offset OldDir     ; save starting dir
  130.                 mov     ah,47h
  131.                 xor     dl,dl
  132.                 int     21h
  133.                 mov     dx,offset slash      ; change to root
  134.                 call    ChDir
  135.  
  136.                 mov     ah,2Fh               ; save old DTA address
  137.                 int     21h
  138.                 mov     OldDTAofs,bx
  139.                 call    SetDta               ; set DTA to current DTAofs
  140.  
  141. FindFirstDir:   mov     dx,offset stardot    ; look for subdirs
  142.                 mov     cx,0030h
  143.                 mov     ah,4Eh               ; find first
  144.                 jmp     FindFND
  145.  
  146. FindNextDir:    mov     ah,4Fh               ; find next
  147. FindFND:        int     21h
  148.                 jb      FindFirstFile        ; no more dirs if carry set
  149.  
  150.                 mov     bx,DTAofs
  151.                 mov     al,[bx + 21]         ; attribute
  152.                 and     al,10h               ; is it a subdir?
  153.                 jz      FindNextDir          ; no, look for more
  154.  
  155.                 cmp     Byte Ptr [bx+30],'.' ; is it '.' or '..'
  156.                 jnz     FoundDir             ; no, so process it
  157.  
  158.                 jmp     FindNextDir          ; yes, so try again
  159.  
  160. FoundDir:       mov     dx,DTAofs            ; point to dir name
  161.                 add     dx,30
  162.                 call    ChDir
  163.  
  164.                 add     DTAofs,43            ; move up one DTA level
  165.                 call    SetDta
  166.  
  167.                 inc     LevelsIn             ; one dir deeper
  168.                 jmp     FindFirstDir         ; look for MORE dirs
  169.  
  170. FindFirstFile:  mov     ah,4Eh               ; now we look for files
  171.                 mov     dx,offset victim
  172.                 xor     cx,cx
  173. FindFNF:        int     21h
  174.                 jb      CheckLevel           ; if none, time to drop back
  175.  
  176.                 mov     si,offset CurDir     ; get current dir name
  177.                 xor     dl,dl
  178.                 mov     ah,47h
  179.                 int     21h
  180.  
  181.                 mov     ah,19h               ; get drive letter
  182.                 int     21h
  183.  
  184.                 add     al,41h
  185.                 mov     drive,al
  186.                 mov     si,offset space
  187.                 call    WriteStr             ; display the drive
  188.  
  189.                 cmp     CurDir, 0
  190.                 jz      WriteFilName
  191.  
  192.                 mov     si,offset tslash     ; write slash
  193.                 call    WriteStr
  194.  
  195. WriteFilName:   mov     si,DTAofs
  196.                 add     si,30
  197.                 call    WriteStr             ; write file name
  198.  
  199.                 mov     si,offset crlf       ; and CR
  200.                 call    WriteStr
  201.  
  202.                 mov     dx,DTAofs            ; finally, delete the bugger
  203.                 add     dx,30
  204.                 mov     ah,41h
  205.                 int     21h
  206.                 mov     ah,4Fh               ; and look for more matches
  207.                 jmp     FindFNF
  208.  
  209. CheckLevel:     cmp     LevelsIn,0           ; any more dirs to drop back?
  210.                 jz      quit                 ; no, quit
  211.  
  212.                 mov     dx,offset dotdot     ; yes, so CD ..
  213.                 call    ChDir
  214.  
  215.                 sub     DTAofs,43            ; decrement DTA pointer to prev
  216.                 call    SetDta
  217.  
  218.                 dec     LevelsIn
  219.                 jmp     FindNextDir          ; look for more directories
  220.  
  221. quit:           mov     dx,OldDTAofs         ; restore original DTA
  222.                 mov     DTAofs,dx
  223.                 call    SetDta
  224.  
  225.                 mov     dx,offset OldDirSl   ; change to original dir
  226.                 call    ChDir
  227.                 mov     ax,4C00h             ; exit with error code 0
  228.                 int     21h
  229. kill   endp
  230.  
  231.  
  232. ;**;
  233. ;**
  234. ;*
  235. ;*  SetDta
  236. ;*
  237. ;*  sets DTA address to offset specified in 'DTAofs'
  238. ;*
  239. ;**
  240. SetDta proc near
  241.                 mov     dx,DTAofs
  242.                 mov     ah,1Ah
  243.                 int     21h
  244.                 ret
  245. SetDta endp
  246.  
  247.  
  248. ;**;
  249. ;**
  250. ;*
  251. ;*  WriteStr
  252. ;*
  253. ;*  uses BIOS calls to display (zero-terminated) string pointed to by
  254. ;*    DS:SI
  255. ;*
  256. ;**
  257. WriteStr proc near
  258.                 mov     ah,0Eh
  259. NextWS:         lodsb
  260.                 or      al,al
  261.                 jz      exitWS
  262.  
  263.                 int     10h
  264.                 jmp     NextWS
  265.  
  266. exitWS:         ret
  267. WriteStr endp
  268.  
  269.  
  270. ;**;
  271. ;**
  272. ;*
  273. ;*  ChDir
  274. ;*
  275. ;*  changes to dir pointed to by DS:DX
  276. ;*
  277. ;**
  278. ChDir  proc near
  279.                 mov     ah,3Bh
  280.                 int     21h
  281.                 ret
  282. ChDir  endp
  283.  
  284.  
  285. ;**;
  286. ;**
  287. ;*
  288. ;*  equates for DTA storage and stuff - some of it stolen directly
  289. ;*    from charles petzold
  290. ;*
  291. ;**
  292. stackbottom     LABEL   BYTE
  293. stacktop        EQU     stackbottom + 100h
  294. progend         EQU     stacktop + 43 * 32 + 1
  295.  
  296. cseg    ends
  297.                 end     start
  298.  
  299.