Classic Computer Magazine Archive COMPUTE! ISSUE 29 / OCTOBER 1982 / PAGE 201

Is Anyone Open?

Elizabeth Deal

Malvern, PA

Mysterious and even disastrous effects can result from improperly CLOSEd PET/CBM files. This utility provides a convenient way to avoid these problems.

It is sometimes important to know which files on the PET have not been closed. The simplest way to find the file status is by asking the PET's machine language monitor, but you have to type error-prone inquiries. I got tired of this and adopted Mike Louder's "dynamic keyboard" routine to do the typing for me. The listing in lines 2000–2070 contains the routine. Users of BASIC 4 equipment must substitute SYS 54386 in lines 2050 and 2070. Line 2070 is important – it does the job.

Enter the code and execute by GOTO2000 or RUN. The program will print the desired inquiries, will "press" RETURN several times, and will display the data on the screen. There is one extra carriage return stuffed in to re-enable Power. [A BASIC-enhancement program sold by Professional Software.] On Powerless systems the cursor will land one line too low. If this bothers you, change J-loop index M from 5 to 4. The display looks like you are in the monitor, but you are not. When all is done, you land safely back in BASIC. If you choose to modify the monitor display, placing the cursor over the SYS command and hitting RETURN will re-enter the monitor.

The display consists of three parts:

  1. On the $00Dl line the PET recalls the file it worked with most recently. $D1 contains length of file name, $D2 contains file number, $D3 contains the secondary address or, in the case of tapes, the read/write flag, and $D4 contains the device number. You can also go after the file name in 4, but in Upgrade, PET's PRINT commands obliterate the data.
  2. On the $00AE line we see PET's count of the number of active files. If you typed RUN or CLR; if no files were open; if you modified the program; or if you did anything that makes your PET think you modified a program–this value will be zero, hence useless to us. If it is not a zero, it is meaningful.
  3. Locations $0251-026F contain the table of files. The first ten values are logical file numbers, the middle ten are device numbers, and the last ten are coded secondary addresses. If the secondary address is $FF, disregard it. Otherwise, subtract $60 (96 dec) to get the secondary address. These values usually remain in the PET. PET considers them irrelevant if $AE contains zero. (You may change $AE to re-enable access to the files.) Otherwise, these are our OPEN files. As you CLOSE them, $AE decreases by one and the display shifts to the left, always leaving a set of data in memory.

A CLOSE Option

For users who prefer not to read the information in hex, BASIC lines 2100–2250 do the same job. Additionally, this routine POKEs a count of "possibly" open files into 174, so that you may CLOSE them. Needless to say, if you don't plan to close anything, you make POKE location 174 with zero; otherwise, the PET will not let you open an already active file.

There is circularity built into the routine: even if you just did CLOSE5, 5 will still be displayed. Disregard it. The purpose of the routine is to provide as much information as possible; it is up to you to use it with some thought.

The key reason for this exercise is the fact that files must be closed. If they are not, the final piece of information cannot be written. In the case of tape files, it's inconvenient. In the case of floppy files, it could lead to the disaster of losing other information already on the disk (especially if you plan to use a scratch command). It's easy to have some unclosed files dangling around – a disk error, a program error, or use of the STOP key may not allow the files to be properly closed. In direct mode, of course, an aborted SAVE command leaves an asterisk behind, meaning unfinished writing, an invitation to trouble that should be corrected immediately.

Some kinds of trouble may not show up for some time. A directory can look pretty good (though blocks free may tell you something), but when you attempt to bring a program in, for instance, it may look pretty weird (the same way as when you write a disk with a non-unique ID).

In any case, the usual procedure for handling such problems is to VALIDATE (COLLECT in 4.0 BASIC) the disk. That's a time-consuming nuisance if a disk is pretty full. It must be used in case of unfinished SAVEing. But we can skip VALIDATE by using the data provided by the above routine(s). With such an amount of displayed information, you're bound to be able to recognize which files are really OPEN and which have been closed. It often makes no difference that you know it, since it is all right to CLOSE an already closed file (hence you can close them in a jiffy in a loop). But if you don't want to touch some device, a selective CLOSE is handy.

2010 PRINT" {05 DOWN} .M 00D1 00D1"
2020 PRINT" {DOWN} .M 00AE 00AE"
2030 PRINT" {DOWN}.M 0251 026F"
2040 PRINT" {04 D0WN} .X"
2050 PRINT" {15 UP} SYS64785"
2060 M = 5 : POKE 158, M : FOR J = 0 TOM
2070 POKE 623 + J, 13 : NEXT : SYS 64785
2090 :
2110 F1 = 174 : F2 = 210 : F3 = 593 : F5 = PEEK (F2)
2120 F4 = PEEK (F1) : IF F4 = 0 THEN F4 = 10
2130 PRINT"   F#  DN  SA"; : F6 = 0 : F7 = 0
2140 : FORI = F4 - 1 TO 0 STEP - 1 : F$ = " "
2150 F4 = PEEK (F3 + I) : REM FILE #
2160 IFF4 = F7 OR F4 = 255 GOTO 2220
2170 F6 = F6 + 1 : F7 = F4: IFF4 = F5 THENF$ = "*"
2180 PRINT : PRINTF$; : GOSUB2240
2190 F4 = PEEK (F3 + 10 + I) : GOSUB 2240 : REM DEVICE
2200 F4 = PEEK (F3 + 20 + I) : REM SEC ADDRESS
2210 IFF 4 < > 255 THEN F4 = F4 - 96 : GOSUB 2240
2220 : NEXTI : PRINT
2240 PRINTRIGHT$("    " + STR$(F4), 4);