[comp.sys.mac] Linking LSC stdio into the Mac Interface

chuq%plaid@Sun.COM (Chuq Von Rospach) (03/07/87)

I thought others might find this interesting... I've been trying to take a
program I hacked together in the LSC glass tty environment (we're talking
brute force hack, here) and put a reasonable front end for it.  I was tired
of typing "<foo > foo.out" to get things done.

Anyway, the Mac interface was simple, thanks to TransSkel and TransDiplay
(if you use LSC and you DON'T use these, you're brain damaged).  But all of
the code used stdio for input, and I didn't want to either write a front end
to stdio to hook Mac calls to getc() and putc().

Unfortunately, you can't directly mix SFGetFile() and fopen() because
fopen() uses the default volume environment information to locate files,
so if you move up and down folders with SFGetFile() the fopen() will fail.

Fortunately, there is an easy way around this without rewriing LSC's stdio.
You can use SetVol() to change the applications idea of the default volume.
To do this, use the following code fragment:

	Stdio_MacInit(TRUE);		/* keep away the glass tty */
	[...]
	SFGetFile([...],&reply);	/* get the information */
	SetVol(&reply.vRefNum);		/* move the application into the 
						right folder (like
						Unix chdir() */
	fopen(&reply.fName);		/* now works)


And the stdio stuff works great now.  This makes it much easier (for me, at
least) to do string hacking, since I don't need to worry about the
Paramblock stuff or buffering.  

Oh, one warning on this:  it will only work if you're doing a single
SFGetFile().  Why?  because if you call it twice, the user might change
folders on you, and then you have to worry about which folder to do the
SetVol to.  The file in the other folder would not be found.  You COULD,
I guess, do a series of SFGetFile()/SetVol()/fopen() calls and get away with
it, because once the file is open, you don't lose that state.

Anyway, hope this helps!

chuq (thank GOD for TransSkel)

Chuq Von Rospach	chuq@sun.COM		[I don't read flames]

There is no statute of limitations on stupidity