scotty@ziggy.UUCP (Scott Drysdale) (12/03/88)
can someone point out what's basically wrong with this program? it's
supposed to print the directory in a format similar to the "dir" command
but without any sorting. my problem seems to be that when dir_scan() is
given a directory as it's input, it will try to lock it twice - once for
the initial Lock() call, and again when the Examine() loop sees that it's
a directory and proceeds to dir_scan() it again... the obvious solution
to that problem is to skip processing once the first Examine() has been
done (ignoring the already examined directory name) - however, that
doesn't make the thing as general as i want - for instance, if dir_scan
is supplied with a file name (not a directory name) as it's initial input,
it should do the right thing.
i should've realized it was too clean looking on paper to really work...
either repair this thing or point me to some public domain sources where
i could see what to do - i have dillon's backup source, but that's a little
too messy for my taste - the directory scan isn't cleanly separated from
the rest of the processing.
--------------------------
#include <stdio.h>
#include <libraries/dos.h>
char indent[128];
void main()
{
int x;
strcpy(indent, " ");
x = scan_dir("dh1:");
exit(x);
}
void push_dir(name)
char *name;
{
printf("%s%s (dir)\n", indent, name);
strcat(indent, " ");
}
void pop_dir(name)
char *name;
{
indent[strlen(indent) - 4] = 0;
}
void add_file(name)
char *name;
{
printf("%s%s\n", indent, name);
}
int scan_dir(dirname)
char *dirname;
{
BPTR lock, prev_dir;
struct FileInfoBlock fib;
int x;
x = 0;
if (lock = Lock(dirname, ACCESS_READ)) {
printf("got lock for %s - about to cd/scan\n", dirname);
prev_dir = CurrentDir(lock);
push_dir(dirname);
if (Examine(lock, &fib)) {
do {
if (fib.fib_DirEntryType > 0) {
if (x = scan_dir(fib.fib_FileName))
break;
} else {
add_file(fib.fib_FileName);
}
} while (ExNext(lock, &fib));
} else {
printf("couldn't examine %s\n", dirname);
x = 2;
}
pop_dir(dirname);
lock = CurrentDir(prev_dir);
UnLock(lock);
return x;
} else {
printf("couldn't get lock for %s\n", dirname);
return 1;
}
}
-----------------------
--Scotty