[comp.sys.mac.programmer] How do you tell if a file variable is in use under MPW Pascal.

8325499@elecvax.eecs.unsw.oz (Gilbert Taylor-Wood) (07/13/88)

   Is there any way to tell if a file variable is in use ?. I need this
because MPW gets upset if you try to open a file using a variable that
is already in use. My current solution is to always close the file variable
before opening a file. What I would like is a function to check for this.
Can anyone supply the missing function "file_is_open" for me ?.

    program junk;
    var
        foo : text;
    begin 

        ...lots of good stuff...

        if ( file_is_open( foo ) then
            close ( foo );
        open ( foo, 'bar' )

        ... more good stuff...

   end.

  As usual please post to me and I will sumarize the replys

thanks
    Gilbert
---
Gilbert Taylor-Wood
P.O. Box 448
Kensington
NSW                                       ACSnet : 8325499@elecvax.eecs.unsw.oz
AUSTRALIA 2033                 Bitnet : 8325499@elecvax.eecs.unsw.oz@munnari.oz

bob@eecs.nwu.edu (Bob Hablutzel) (07/19/88)

I hope I am answering the question you are asking...

The file reference number returned by the file manager is an offset into
a system table maintained in a pointer. The pointer to this table is
contained in the low level global FCBSPtr ( $34E ). Thus, if you retrieve
the pointer in FCBSPtr, add the offset returned by the file manager, you
get a pointer to the file control block for the particular file.

Now, within the file control block for a particular file is the file number,
at offset fcbFlNm ( $0000. It's the first longword of the file control
block. This is, of course subject to change without notice, but probably
never will change). If the fcbFlNm for a given file refnum is zero, the
file refnum is for a closed file. In otherwords, non-zero file numbers
are open files.

WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING

If the file number is either 3 or 4, never close the file. These are the 
extents and catalog files used by HFS.

Some other trivia: if you want to know if a given word is a valid file 
refnum, subtract 2 from it, and divide by the constant held in FSFCBLen
( $3F6 ). If the remainder is zero, the refnum is valid. This is because
the size of each file control block entry is contained in FSFCBLen, and
the first valid file refnum is at offset 2 from the FCBSPtr.

If this does NOT answer the question you asked, then you just read a ton
of techno-talk for nothing. Sorry.

Bob Hablutzel 
Northwestern University, ACNS Development