!       SORTED DIRECT PROGRAM:
!
!       .SDIR <DISK>    NORMAL WIDE MODE
!
!       .SDIR <DISK>/OPTIONS    PRINT OUT OTHER DATA...
!
!       /H[CN]  PRINT OUT THE HEADER CLUSTER NUMBER
!       /N[LCNS]        PRINT OUT THE NUMBER OF CLUSTERS
!       /I[NIT] PRINT OUT THE HCSIC
!       /S[IZE] PRINT OUT THE FILE SIZE IN BYTES
!       /P[ROT] PRINT OUT THE PROTECTION
!       /D[ATE] PRINT OUT THE CREATION DATE
!       /A[LL]  PRINT OUT ALL OF THE ABOVE
!
!       3/27/79 CJM
!
        DIM DEVICE$[20],DISKID$[32]
        DIM ENTRY1$[32],ENTRY2$[32],ENTRYLEN/32/
        DIM HCSIC/19/,OUTPUT/1/,PROT/26/,FSIZE/21/,LCNS/20/,CDATE/27/
        DIM DIR$[15000],DIRENTRY$[32],DIR/1/,LISTING/2/,FIRSTENTRY/1/
        DIM CURPTR,LASTENTRY,SIZE,CURENTRY
        DIM     ENTRY$[32],READB$/:B,:E,1,0/,I/0/,TOTENTS/0/,TOTLCNS/0/
        DIM     HCN/0/,NLCNS/0/,INIT/0/,BYTES/0/,DATE/0/,WIDE/1/
        DIM     COMMAND$[100],OPTIONS$[100],J


        LEN(DIR$)=15000

        INPUT "" COMMAND$
        DEVICE$=LEFT$(COMMAND$,FIND(COMMAND$,":"))
        COMMAND$=RIGHT$(COMMAND$,LEN(DEVICE$)+1)
        IF DEVICE$="" THEN LET DEVICE$="DISK:"
        OPEN #DIR, DEVICE$ CAT 'DIRECTORY.SYS'

        CURPTR=FIRSTENTRY

10      READ #DIR, DIRENTRY$
        IF EOF(DIR) THEN 100
        IF DIRENTRY$[HCSIC]=0 THEN 10

        DIR$[CURPTR,ENTRYLEN]=DIRENTRY$
        CURPTR=CURPTR+ENTRYLEN
        GOTO 10

100     LASTENTRY=CURPTR-ENTRYLEN
        PRINT   "SORTING..."
        CURPTR=FIRSTENTRY

200     IF CURPTR=LASTENTRY THEN 1000

        ENTRY1$=DIR$[CURPTR,ENTRYLEN]
        ENTRY2$=DIR$[CURPTR+ENTRYLEN,ENTRYLEN]

        IF ENTRY1$<ENTRY2$ THEN 300

        DIR$[CURPTR,ENTRYLEN]=ENTRY2$
        DIR$[CURPTR+ENTRYLEN,ENTRYLEN]=ENTRY1$

        IF CURPTR=FIRSTENTRY THEN 300

        CURPTR=CURPTR-ENTRYLEN
        GOTO 200

300     CURPTR=CURPTR+ENTRYLEN
        GOTO 200

1000    CLOSE #DIR

        OPEN #DIR, DEVICE$
        RESTORE #DIR, 32
        READ #DIR, DISKID$
        CLOSE #DIR




        PROT=0
        COMMAND$=UPPERCASE$(COMMAND$)

        ENTRY$=COMMAND$
        IF 0=FIND(COMMAND$,"/") THEN 91
        OPTIONS$=RIGHT$(COMMAND$,FIND(COMMAND$,"/"))
        ENTRY$=LEFT$(COMMAND$,FIND(COMMAND$,"/")-1)

        WIDE=0

95      IF      OPTIONS$="" THEN 91
        IF      OPTIONS$[1,1]<>"/" THEN ...
&               LET OPTIONS$=OPTIONS$[2,LEN(OPTIONS$)-1]\GOTO 95

        OPTIONS$=OPTIONS$[2,LEN(OPTIONS$)-1]
        IF      OPTIONS$[1]= :48 THEN LET HCN=1\GOTO 95
        IF      OPTIONS$[1]= :4e THEN LET NLCNS=1\GOTO 95
        IF      OPTIONS$[1]= :49 THEN LET INIT=1\GOTO 95
        IF      OPTIONS$[1]= :53 THEN LET BYTES=1\GOTO 95
        IF      OPTIONS$[1]= :50 THEN LET PROT=1\GOTO 95
        IF      OPTIONS$[1]= :44 THEN LET DATE=1\GOTO 95
        IF      OPTIONS$[1]= :41 THEN ...
&       let HCN=1\NLCNS=1\INIT=1\BYTES=1\PROT=1\DATE=1\GOTO 95
        PRINT   "ILLEGAL OPTION:"
        PRINT   COMMAND$
        OPTIONS$="/" CAT OPTIONS$
        PRINT   TAB(FIND(COMMAND$,OPTIONS$));"^"
        STOP


91      PRINT   "FILES ON...    ";DISKID$
        IF WIDE THEN 910
        PRINT "File name";TAB(18);
        IF HCN THEN PRINT "hcn    ";
        IF NLCNS THEN PRINT "  nlcns ";
        IF INIT THEN PRINT " hcsic ";
        IF BYTES THEN PRINT "file size ";
        IF PROT THEN PRINT "protection ";
        IF DATE THEN PRINT "  date";
        PRINT
910     FOR CURENTRY=1 TO LASTENTRY STEP ENTRYLEN
        ENTRY$=DIR$[CURENTRY,ENTRYLEN]
        PRINT   ENTRY$[1,16];' ';
        TOTENTS=TOTENTS+1
        TOTLCNS=TOTLCNS+ENTRY$[20]*256+ENTRY$[21]
        IF NOT(WIDE) THEN 50
        I=I+1
        IF      I=4 THEN PRINT\LET I=0
        GOTO    920
50      IF NOT(HCN) THEN 51
        PRINT HEX$(ENTRY$[17]**8+ENTRY$[18]);"  ";

51      IF NOT(NLCNS) THEN 52
        J=ENTRY$[20]*256+ENTRY$[21]
        PRINT USING "####### ",J;

52      IF NOT(INIT) THEN 53
        PRINT USING "###### ",ENTRY$[19];

53      IF NOT(BYTES) THEN 54
        PRINT USING "######### ",(ENTRY$[22]*256^3)+(ENTRY$[23]*65536)+ ...
&               (ENTRY$[24]*256)+ENTRY$[25];

54      IF NOT(PROT) THEN 55
        IF ENTRY$[26]&:40 THEN PRINT "wrt "; ELSE PRINT "    ";
        IF ENTRY$[26]&:80 THEN PRINT "delete "; ELSE PRINT "       ";

55      IF NOT(DATE) THEN 56

        COMMAND$=HEX$(ENTRY$[28])
        OPTIONS$=COMMAND$[4,2] CAT "/"
        COMMAND$=HEX$(ENTRY$[27])
        OPTIONS$=OPTIONS$ CAT COMMAND$[4,2] CAT "/"
        COMMAND$=HEX$(ENTRY$[29])
        PRINT OPTIONS$;COMMAND$[4,2];

56      PRINT
920     NEXT    CURENTRY


9100    ON ERROR GOTO 0
        PRINT
        PRINT   "A total of";TOTLCNS;"clusters in";TOTENTS;"files"
        CHAIN   'DEFAULTPROGRAM'
        END
