;***********************************************************************
; CLRAND - CLEARs a RANDOM FILE
;
; by Dave Heyliger - AMUS Staff
;
; Usage: L OPR:
;        CLRAND {filespec}
;
; Purpose: great after you use CREATE and you want the file clean
;***********************************************************************

       SEARCH  SYS                             ;search the regulars
       SEARCH  SYSSYM
       SEARCH  TRM

       PHDR    -1,0,PH$REE!PH$REU!PH$OPR       ;must be in OPR: (note PH$OPR)

       .OFINI
       .OFDEF  IDDB,D.DDB                      ;DDB for the file
       .OFSIZ  IMPSIZ

       ;get the inpure space
       GETIMP  IMPSIZ,A3                       ;A3 points to varibles

       ;process input line
       BYP                                     ;bypass bs
       LIN                                     ;just a CR?
       BNE     INOK
       TYPECR  <Usage: CLRAND {filespec}, where filespec is a random file.>
       TYPECR  <               (default extension is .DAT)>
       EXIT

       ;looks good, see if random file
INOK:   LEA     A1,IDDB(A3)                     ;point to DDB
       FSPEC   @A1,DAT                         ;create filename in DDB
       INIT    @A1                             ;initialize
       LOOKUP  @A1                             ;find the file
       BNE     NOFILE                          ;nope - error
       CMPW    IDDB+D.WRK+6(A3),#-1            ;random file?
       BEQ     AOK                             ;yup, continue
       TYPE    <?>                             ;nope, start error msg
       PFILE   @A1                             ;type out filename
       TYPECR  < is not a random file.>        ;and rest of message
       EXIT                                    ;then quit

NOFILE: TYPECR  <?This file does not exist.>    ;couldn't find the puppy
       EXIT                                    ;so quit

       ;double check the users intentions
AOK:    CRLF
       TYPE    <You are about to zero out all data in this file. Enter Y to confirm: >
       KBD
       CTRLC   END                             ;quit on ^C
       CMPB    @A2,#'Y                         ;yes?
       JNE     END                             ;nope

       ;now get the number of blocks and clear each one
       MOVW    IDDB+D.WRK+2(A3),D2             ;D2 holds the number of blocks
       OPENR   @A1                             ;open the file
READIT: READ    @A1                             ;get a block

LOOP:   MOV     #128.,D3                        ;number of longword clears
       MOV     IDDB+D.BUF(A3),A4               ;A4 points to block
10$:    CLR     (A4)+                           ;zero it out
       DEC     D3                              ;one less to do
       BNE     10$                             ;still more to do
       WRITE   @A1                             ;write the block
       DECW    D2                              ;one less block to do
       BEQ     END                             ;if zero, we are done
       INCW    IDDB+D.REC+2(A3)                ;point to next block
       BR      READIT                          ;and go read it and clear it

END:    EXIT
       END