	DIM DUMPBUFFERS$/:E,4,1,1/
	DIM UNPROTECT$/:E,14,:1,:10/
	DIM SCGETTYPE$/:F,14,1,:4/
	DIM SCGETERRORSTATS$/:F,14,1,:11/
	DIM YES$(5),DUMMY$(1)
	DIM RETRYCOUNT/50/
	DIM READSECTORSIZE$/:F,14,1,:5/,SECTORSIZE
	DIM SECTOR/0/,SECTOR$(4096)
	DIM DISK$(50)

	PRINT "SCAN DISK v1.0b   Copyright (C) 1980 Software Dynamics"

	INPUT "Scan which disk? " DISK$
	OPEN #1,DISK$
	SYSCALL(READSECTORSIZE$,DUMMY$,SECTOR$)
	LET SECTORSIZE=SECTOR$(1)*256+SECTOR$(2)
	PRINT "Sector size is... ";SECTORSIZE

	Input "Enter starting sector (<CR> defaults to 0): " Sector$
	If len(Sector$)>0
	Then
		Let Sector=val(Sector$)
		RESTORE #1,SECTOR*SECTORSIZE
	Else	Let Sector=0
	let len(Sector$)=Sectorsize

110	ON ERROR GOTO 1000
	REPEAT
121		READ #1,SECTOR$[1,SECTORSIZE]
		IF EOF(1) THEN
			PRINT "Last sector of media is ";hex$(sector-1)
			EXIT
		FI
		LET SECTOR=SECTOR+1
		IF SECTOR&:3F=0 THEN PRINT HEX$(SECTOR)
	END

1000	IF ERR<>1045 AND ERR<>1042 THEN ERROR
	PRINT  "I got error";ERR;"on sector ";HEX$(SECTOR)
	SYSCALL #1,SCGETTYPE$,"",YES$
	IF YES$[1]=1
	THEN
		If error when Syscall #1,SCGETERRORSTATS$,"",SECTOR$[1,18]
		Then Print "No error statistics are available."
		Else
			Print "Seek error count ";Sector$[1]*256+Sector$[2];...
&				"Last seek status ";Hex$(Sector$[3]*256+Sector$[4])
			Print "Read error count ";Sector$[9]*256+Sector$[10];...
&				"Last read status ";Hex$(Sector$[11]*256+Sector$[12])
		Fi
	FI
	INPUT "How many times to retry? " RETRYCOUNT
	IF RETRYCOUNT=0 THEN 5000
1100	REM TRY TO GET IT AGAIN!
	! TO MAKE SURE WE DON'T GET LULLED BY DISK BUFFER POOL
	ON ERROR GOTO 0
	RESTORE #1,SECTOR*SECTORSIZE
	ON ERROR GOTO 2000
1105	READ #1,SECTOR$[1,SECTORSIZE]
	PRINT "I got the sector!"
	INPUT "Write it back? " YES$
	GOTO 2100

2000	IF ERR<>1045 AND ERR<>1042 THEN ERROR
	LET RETRYCOUNT=RETRYCOUNT-1
	IF RETRYCOUNT<>0 THEN 1100
	PRINT "I can't seem to get it..."
5000	INPUT "Stomp on it? " YES$
	LET LEN(SECTOR$)=SECTORSIZE
	FOR I=1 TO LEN(SECTOR$) DO LET SECTOR$(I)=ASC "%"
2100	IF UPPERCASE$(YES$)<>"YES" THEN 2200
	PRINT "STOMP!"
	ON ERROR GOTO 0
	! WRITE IT BACK OUT (HOPEFULLY IN A MORE EASY TO READ FASHION)
1500	RESTORE #1,SECTOR*SECTORSIZE
1510	ON ERROR GOTO 1530
	WRITE #1,SECTOR$
	SYSCALL (DUMPBUFFERS$,DUMMY$,DUMMY$)
	ON ERROR GOTO 0
	IF SECTOR>100 THEN SECTOR=SECTOR-100 ELSE SECTOR=0
	RESTORE #1,SECTOR*SECTORSIZE
	GOTO 110

1530	IF ERR=1042 OR ERR=1046 THEN
		PRINT "I got error";ERR;"on sector ";HEX$(SECTOR);".  ";
		PRINT "I am skipping that sector."
		GOTO 2200
	FI
	IF ERR<>1049 THEN ERROR
	PRINT "Unlocking the disk device..."
	SYSCALL(UNPROTECT$,DUMMY$,DUMMY$)
	GOTO 1500

2200	LET SECTOR=SECTOR+1
	RESTORE #1,SECTOR*SECTORSIZE
	GOTO 110

END
