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 / DF.ASM < prev    next >
Assembly Source File  |  1986-05-15  |  4KB  |  203 lines

  1.     page    66,132
  2.     page
  3.  
  4. ;---- df.com ---------------------------------------------------------------
  5. ; Usage: df {-option} {drive}
  6. ; Gives free space left on given drive.
  7. ; Option: -nnn where nnn is a decimal number < 65536
  8. ;    Errorlevel is set to zero if free space > nnn kilobytes, two otherwise.
  9. ; If no drive is given, defaults to current drive.
  10. ; If invalid drive, errorlevel set to one.
  11. ; DRK 16 July 84
  12. ; Rev 15 Aug 84
  13. ;------------------------------------------------------------------------------
  14.  
  15. stdout    equ    1
  16. stderr    equ    2
  17.  
  18.     extrn    _args:near, _shift:near, argc:word, argv:word
  19.     extrn    new:near
  20.  
  21.     extrn    sprintf:near
  22.  
  23.  
  24. code    segment    public 'CODE'
  25. assume cs:code,ds:code
  26.  
  27.     org    100h
  28.  
  29. main    proc
  30.     jmp    df
  31. main    endp
  32.  
  33.  
  34.     db    27, "[2J"
  35. usagem    db    "df    - show disk free space", 13, 10
  36.     db    "Usage: df {-nnnn} {drive:}", 13, 10
  37.     db    "Options: -nnnn: if (kilobytes_free < nnnn) errorlevel=2."
  38.     db    13, 10
  39. usagelen    equ    $-usagem
  40.     db    26            ; eof mark for the casual typist
  41.             
  42. threshold    dw    ?        ; value of -nnn
  43. bufptr        dw    ?        ; where to store strings
  44.  
  45. dfmsg    db    '%ld bytes free.\n', 0
  46. bdmsg    db    '? df: bad drivespec %s.\n', 0
  47.  
  48.  
  49. ;---- df ---------------------------------------------------------------------
  50.  
  51. df    proc    near
  52.  
  53.     call    _args
  54.  
  55.     mov    cx, 100        ; init printf
  56.     call    new
  57.     mov    bufptr, bx
  58.  
  59.  
  60.     mov    threshold, 0
  61.  
  62. argl:    mov    dl, 0        ; 0 = default drive
  63.     cmp    argc, 0
  64.     jz    got_args
  65.  
  66.     mov    si, argv[2]    ; get ptr to first arg
  67.     lodsb
  68.     cmp    al, '-'        ; is it an option?
  69.     jnz    no_option
  70.         call    dec2w    ; read argument into AX
  71.         mov    threshold, ax
  72.         call    _shift
  73.         dec    argc
  74.         jmp    argl
  75. no_option:
  76.     ; Then it was a drive code.
  77.     mov    dl, al
  78.     lodsb
  79.     cmp    al, ':'
  80.     jnz    usage
  81.  
  82.     and    dl, 0DFh        ; convert to uppercase
  83.     sub    dl, '@'            ; convert to drive code
  84. got_args:
  85.     mov    ah, 36h
  86.     int    21h            ; get disk space
  87.     inc    ax
  88.     jz    baddrive        ; ax = -1 -> bad drive number
  89.     dec    ax
  90.  
  91.     mul    cx            ; ax = bytes/cluster
  92.     mul    bx            ; dx:ax = bytes free
  93.  
  94.     push    dx
  95.     push    ax            ; save total bytes free for comparison
  96.  
  97.     mov    ax, offset dfmsg
  98.     push    ax
  99.     mov    bx, stdout
  100.     call    fdprintf
  101.     add    sp, 2
  102.  
  103.     pop    ax            ; recover disk free space
  104.     pop    dx
  105.  
  106.     mov    cx, 10
  107. shloop:    shr    dx,1
  108.     rcr    ax,1
  109.     loop    shloop            ; convert to kilobytes
  110.  
  111.     or    dx, dx
  112.     jnz    muchroom
  113.  
  114.     cmp    ax, threshold
  115.     ja    muchroom
  116.         mov    al, 2
  117.         jmp    short exit    ; below threshold- set errorlevel
  118. muchroom:
  119.     mov    al, 0
  120.  
  121. exit:    mov    ah,4ch
  122.     int    21h            ; terminate, return status in AL.
  123.  
  124. df    endp
  125.  
  126.  
  127. ;---- usage ------------------------------------------------------------
  128. ; Print the usage admonishment to stderr.
  129. usage:
  130.     mov    bx, stderr
  131.     mov    cx, usagelen
  132.     mov    dx, offset usagem
  133.     mov    ah, 40h
  134.     int    21h
  135.     mov    al, 1
  136.     jmp    exit
  137.  
  138.  
  139. baddrive:
  140.     push    argv[2]
  141.     mov    ax, offset bdmsg
  142.     push    ax
  143.     mov    bx, stderr
  144.     call    fdprintf
  145.     mov    al, 1
  146.     jmp    exit
  147.  
  148. printf_rtn    dw    ?
  149. printf_hand    dw    ?
  150.  
  151. ;---- fdprintf ------------------------------------------------------------
  152. ; Print a formatted string to handle in bx.
  153. fdprintf    proc    near
  154.     mov    printf_hand,bx
  155.     pop    printf_rtn
  156.     push    bufptr
  157.     call    sprintf
  158.     add    sp, 2
  159.     mov    dx, bufptr
  160.     mov    cx, di
  161.     sub    cx, dx
  162.     dec    cx
  163.     mov    bx, printf_hand
  164.     mov    ah, 40h
  165.     int    21h
  166.     jmp    word ptr printf_rtn
  167. fdprintf    endp
  168.  
  169.  
  170. ;----- dec2w ---------------------------------------------------------
  171. ; converts ascii decimal string @DS:SI++ to AX.
  172. ; (String must be null-terminated.)
  173. dec2w    proc    near
  174.     mov    cx, 0
  175.     mov    bx, 10
  176.  
  177. dloop:    ; 1. Terminate if digit is a null.
  178.     lodsb
  179.     or    al, al
  180.     jz    ddone
  181.  
  182.     ; 2. Multiply older digits by ten.
  183.     xchg    ax, cx
  184.     mul    bx
  185.     xchg    cx, ax
  186.  
  187.     ; 3. Add current digit.
  188.     sub    al, '0'
  189.     cbw            ; never signed
  190.     add    cx, ax
  191.  
  192.     jmp    dloop
  193.  
  194. ddone:    xchg    ax, cx
  195.     ret
  196.  
  197. dec2w    endp
  198.  
  199. code    ends
  200.  
  201.     end    main
  202.  
  203.