;
;   Show File Size - Jim Butterfield.  April 29/89.
;
; Metacomco - ASSEM p.asm -o p.o -i sys:include -l ram:list
; C.A.P.E - menu "Assembler options/Include Path List" dfx:includes 
; a68k (gibbs) - A68K p.asm -op.o -isys:include -lram:list
;      note  charlie doesn't like spaces after say -i
; Lattice:  asm -idf1:Assembler_Headers/ p.asm.. note the slash
;         all options must be BEFORE filename
;         -l to 'standard output' - redirection needed
;         lattice gives NO completion advice
; DevPac: GenIm p.asm -idf0:include  or from menu, as below
;
; csect code                     ; for lattice asm
 INCDIR df0:include/             ; for DevPac
 INCLUDE "exec/types.i"
; above not needed if next line invokes types.i
 INCLUDE "libraries/dos.i"
;
; Exec library calls
            xref _LVOAllocMem          ;-$C6
            xref _LVOFreeMem           ;-$D2
            xref _LVOSetSignal         ;-$132
            xref _LVOCloseLibrary      ;-$19E
            xref _LVOOpenLibrary       ;-$228
; DOS library calls
            xref _LVOOpen              ;-$1E
            xref _LVOClose             ;-$24
            xref _LVORead              ;-$2A
            xref _LVOWrite             ;-$30
            xref _LVOOutput            ;-$3C
            xref _LVOLock              ;-$54
            xref _LVOUnLock            ;-$5A
            xref _LVOExamine           ;-$66
; define work frame
; Working variables in frame A4
CmdTail     EQU  -$4
DosBase     EQU  -$8
OutHandle   EQU  -$C
FileLock    EQU  -$10
AllocBlock  EQU  -$14
;
;-- Initial setup:
Startup     link   a4,#-$14
            move.b #$0,-1(a0,d0.w)   ; insert binary zero
SpaceLoop   move.b (a0)+,d0          ; examine character
            cmp.b  #$20,d0           ; is it space?
            beq.s  SpaceLoop         ; yes, look again
            subq.l #1,a0             ; no, back up
            move.l a0,CmdTail(a4)    ; Remember ptr to argument line.
            lea    dosname(pc),a1    ; Name 'dos.library'.
            clr.l  d0                ;   Any version (0)
            move.l $4,a6             ;   Using Exec library
            jsr    _LVOOpenLibrary(a6)  ;   Open Dos library.
            move.l d0,DosBase(a4)    ; Remember DosBase ptr.
            beq.s  StartupQuit       ;      dos not opened
            bsr.s  MainJob
            move.l DosBase(a4),a1        ; DosBase to a1;
            move.l $4,a6             ;   then using Exec library,
            jsr    _LVOCloseLibrary(a6)    ;   close Dos library.
StartupQuit unlk   a4
            rts                      ; End of Program
;
;-- Get CLI outhandle:
MainJob     move.l DosBase(a4),a6    ; set Dos library
            jsr    _LVOOutput(a6)    ;   get CLI outhandle,
            move.l d0,OutHandle(a4)  ;   & then remember it.
;
; get a 'lock' on the file, if possible.
            move.l CmdTail(a4),d1
            moveq  #ACCESS_READ,D2   ; get a read lock
            jsr _LVOLock(a6)
            move.l d0,FileLock(a4)   ; store the lock pointer
            beq    DosQuit           ; couldn't get lock
; ask for memory to hold a FileInfoBlock.
            move.l $4,a6             ; this calls for Exec
            moveq  #0,d1             ; any type of memory
            move.l #fib_SIZEOF,d0    ; size of block needed
            jsr    _LVOAllocMem(a6)  ; go get it
            move.l d0,AllocBlock(a4) ; save the block address
            beq    DropLock          ; quit if no memory
; Now get information on the file
            move.l DosBase(a4),a6    ; set Dos library
            move.l FileLock(a4),d1   ; lock
            move.l d0,d2             ; file info block address
            jsr _LVOExamine(a6)      ; go for the data
; Assume Examine never fails.  Go for the size.
            move.l d2,a2             ; file info block address
            move.l fib_Size(a2),d0   ;file size
; This decimal conversion subroutine works correctly ONLY with
; word values (65535 and lower).  A more elaborate routine is
; needed for longword conversion (BIG files).
            movem.l a2/d2-d3,-(a7)
            link  a2,#-8
; clear output area to spaces
            moveq #7,d1
            lea   -8(a2),a0
ClearDig    move.b #$20,(a0)+
            dbf    d1,ClearDig
            move.b #$0a,-(a0)
; pull out digits from low end
NextDig     divu  #10,d0
            swap  d0
            ori.b #$30,d0
            move.b d0,-(a0)
            move.w #0,d0
            swap d0
            bne.s  NextDig
; print the eight bytes
            move.l OutHandle(a4),d1
            lea    -8(a2),a0         ;start address
            move.l a0,d2
            moveq  #8,d3             ;length
            jsr    _LVOWrite(a6)
            unlk a2
            movem.l (a7)+,a2/d2-d3
FreeInfo    move.l $4,a6             ; set Exec library
            move.l AllocBlock(a4),a1 ; memory block address
            move.l #fib_SIZEOF,d0    ; size of block
            jsr    _LVOFreeMem(a6)   ; release this memory
DropLock    move.l DosBase(a4),a6    ; set Dos Library
            move.l FileLock(a4),d1   ; take off the lock
            jsr    _LVOUnLock(a6)
DosQuit     rts                      ;   exit program.

dosname     dc.b   'dos.library',0
   end
