[comp.sys.mac.programmer] Help with directories?

jsl@cup.portal.com (John S Labovitz) (03/19/89)

Hello.

I've got a C program that was written (by someone else) for a Unix machine.
I'm trying to get it to work on the Mac, but without doing any major changes
in terms of file I/O, user interface, etc.  This program will be used on
various machines including the Mac, so I must keep the code system
independent.  I can, however, change filenames and add routines (to put
included in a separate system-dependent file).

The user interface and basic file operations work well (due to the
LightSpeed C stdio library).  However, I'm having a big problem with
directories.  I've read through the File Manager chapter in Inside Mac
(both the original and the one in Volume 5), and it's all *so* confusing.

I'm going to explain this with a abstract version of the original code;
I think that will make things clearer.

Say I've got a directory named "x" in the root directory of the current
disk.  Within "x", there are two more directories, "y" and "z".  There
are four things that need to be done in the program:

1. Open and read file "a" in directory "z".
2. Change to directory "y".
3. Create and write files "b" and "c" in directory "y".
4. Create and write file "d" in directory "z".

On a Unix system, I would probably code this as follows (slight meta code
here):

        fopen("/x/y/a", "r");
        /* do stuff with file "a" */

        chdir("/x/z");
        fopen("b", "w");
        fopen("c", "w");
        /* do stuff with files "b" and "c" */

        fopen("/x/y/d", "w");
        /* do stuff with file "d" */

So my question is, how do I do the above on the Mac?  Obviously, the file
names have to be changed, at least substituting ":" for "/".  And "chdir"
has to be implemented, at least for LightSpeed C.

In looking at the source for "fopen", it appears to use a volume reference
number of zero, meaning the current directory.  Trying to open ":x:y:a"
doesn't seem to work.  Prepending the actual volume name to the filenames
works, but I obviously don't want to force people to have particular names
for their disks.

I think I've been able to do a "chdir" by making a working directory from
a combination of the current volume reference number and the directory
name, but this only works with the current directory; I can't figure out
how to do this from the root directory.

As I said, I'm doing this in LightSpeed C, but examples in any other C
(or Pascal, since I can read it) would be extremely helpful.

Thanks for any help.
--
John Labovitz
jsl@cup.portal.com