|
Home > Archive > Unix Programming > June 2004 > recursively listing directories
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
recursively listing directories
|
|
|
| Maybe the late hour and the 2lbs of kung pao chicken is taking its toll on
me, but I'm having some difficulty.
For an assignment, I'm writing a minimalist version (i.e., not 3500 lines)
of ls. This version only has one output format, and it always gives a
recursive output. I wanted to first try going through the root directory,
and recursing into subdirs to fill in the struct (for later sorting, etc)
for each of these things all in one shot. But I'm having trouble with my
recursion. Some code:
--------------------------------------------
void listFilesHere(char *root){
struct stat fstat;
struct direct *dstat;
DIR *dirp;
dirp = opendir(root);
printf("Files and dirs under %s\n",root);
for (dstat = readdir(dirp); dstat != NULL; dstat = readdir(dirp)) {
if( (strcmp(dstat->d_name,".") == 0) ||
(strcmp(dstat->d_name,"..") == 0) )
continue; /* ignore the . and .. */
stat(dstat->d_name, &fstat);
printf("%-15s%8d\n", dstat->d_name, fstat.st_size); /* print out name and
size */
/* if the thing i'm looking at right now is a directory, do a listFilesHere
for that path */
if( S_ISDIR (fstat.st_mode) ){
char *newroot=strcat(root,dstat->d_name);
listFilesHere(newroot);
}
}
closedir(dirp);
}
--------------------------------------------
Now, it /seems/ to work for immediate subdirs of the initial root. Sample
o/p (dirs are marked with ( / ) ).
Files and dirs under /public/hw5/
makefile 182
st1.o 2328
st1.c 1767
st1 13592
foo ( / ) 4096
Files and dirs under /public/hw5/foo
bar ( / ) 0
fooFile1.txt 0
fooFile2.txt 0
(continuing listing of hw5)
types.h 6658
stat.h 12366
stat.c 8381
st2.c 382
bak1 ( / ) 4096
Files and dirs under /public/hw5/foobak1
Segmentation fault
..... and that's it. I have to take a few minutes to clean up the hair i've
pulled out so far. I appreciate any pointers you folks can provide.
| |
|
| John wrote:
> Maybe the late hour and the 2lbs of kung pao chicken is taking its toll on
> me, but I'm having some difficulty.
>
> For an assignment, I'm writing a minimalist version (i.e., not 3500 lines)
> of ls. This version only has one output format, and it always gives a
> recursive output. I wanted to first try going through the root directory,
> and recursing into subdirs to fill in the struct (for later sorting, etc)
> for each of these things all in one shot. But I'm having trouble with my
> recursion. Some code:
>
> --------------------------------------------
> void listFilesHere(char *root){
> struct stat fstat;
> struct direct *dstat;
> DIR *dirp;
>
> dirp = opendir(root);
>
> printf("Files and dirs under %s\n",root);
> for (dstat = readdir(dirp); dstat != NULL; dstat = readdir(dirp)) {
> if( (strcmp(dstat->d_name,".") == 0) ||
> (strcmp(dstat->d_name,"..") == 0) )
> continue; /* ignore the . and .. */
>
> stat(dstat->d_name, &fstat);
> printf("%-15s%8d\n", dstat->d_name, fstat.st_size); /* print out name and
> size */
>
> /* if the thing i'm looking at right now is a directory, do a listFilesHere
> for that path */
> if( S_ISDIR (fstat.st_mode) ){
> char *newroot=strcat(root,dstat->d_name);
This line doesn't look right, at least not to me. ;-) How do you know
that the root has sufficient space for the new directory name you append?
Shouldn't you also append a / if the last char of root isn't a / ?
HTH
boa
[snip]
| |
| SM Ryan 2004-06-26, 10:11 am |
| # if( S_ISDIR (fstat.st_mode) ){
# char *newroot=strcat(root,dstat->d_name);
newroot is a new path, and so should get its own allocation.
char *newroot = malloc(strlen(root)+strlen(dstat->d_name)+2);
sprintf(newroot,"%s/%s",root,dstat->d_name);
# listFilesHere(newroot);
free(newroot);
# }
| |
| zwd@eudial.nosp.am.mine.nu 2004-06-26, 10:11 am |
| John <iamlevi3@hotmail.com> wrote:
> Maybe the late hour and the 2lbs of kung pao chicken is taking its toll on
> me, but I'm having some difficulty.
>
> For an assignment, I'm writing a minimalist version (i.e., not 3500 lines)
> of ls. This version only has one output format, and it always gives a
> recursive output. I wanted to first try going through the root directory,
> and recursing into subdirs to fill in the struct (for later sorting, etc)
> for each of these things all in one shot. But I'm having trouble with my
> recursion. Some code:
>
> --------------------------------------------
> void listFilesHere(char *root){
> struct stat fstat;
> struct direct *dstat;
> DIR *dirp;
>
> dirp = opendir(root);
>
> printf("Files and dirs under %s\n",root);
> for (dstat = readdir(dirp); dstat != NULL; dstat = readdir(dirp)) {
> if( (strcmp(dstat->d_name,".") == 0) ||
> (strcmp(dstat->d_name,"..") == 0) )
> continue; /* ignore the . and .. */
>
> stat(dstat->d_name, &fstat);
> printf("%-15s%8d\n", dstat->d_name, fstat.st_size); /* print out name and
> size */
>
> /* if the thing i'm looking at right now is a directory, do a listFilesHere
> for that path */
> if( S_ISDIR (fstat.st_mode) ){
> char *newroot=strcat(root,dstat->d_name);
> listFilesHere(newroot);
> }
> }
>
> closedir(dirp);
> }
> --------------------------------------------
>
> Now, it /seems/ to work for immediate subdirs of the initial root. Sample
> o/p (dirs are marked with ( / ) ).
>
> Files and dirs under /public/hw5/
> makefile 182
> st1.o 2328
> st1.c 1767
> st1 13592
> foo ( / ) 4096
>
> Files and dirs under /public/hw5/foo
> bar ( / ) 0
> fooFile1.txt 0
> fooFile2.txt 0
>
> (continuing listing of hw5)
> types.h 6658
> stat.h 12366
> stat.c 8381
> st2.c 382
> bak1 ( / ) 4096
> Files and dirs under /public/hw5/foobak1
> Segmentation fault
>
>
> .... and that's it. I have to take a few minutes to clean up the hair i've
> pulled out so far. I appreciate any pointers you folks can provide.
>
>
Have you tried debugging it with GDB or some other debugger?
| |
|
| No... I haven't. I suppose now is as good a time as any to learn gdb =)
<zwd@eudial.nosp.am.mine.nu> wrote in message
news:PcVCc.96910$dP1.319780@newsc.telia.net...
> John <iamlevi3@hotmail.com> wrote:
>
> Have you tried debugging it with GDB or some other debugger?
|
|
|
|
|