recursively listing directories
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > recursively listing directories




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    recursively listing directories  
John


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:11 PM

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.







[ Post a follow-up to this message ]



    Re: recursively listing directories  
boa


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:11 PM

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 a
nd
> size */
>
> /* if the thing i'm looking at right now is a directory, do a listFilesHer
e
> 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]







[ Post a follow-up to this message ]



    Re: recursively listing directories  
SM Ryan


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:11 PM

#   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);

#   }





[ Post a follow-up to this message ]



    Re: recursively listing directories  
zwd@eudial.nosp.am.mine.nu


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:11 PM

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 an
d
> size */
>
> /* if the thing i'm looking at right now is a directory, do a listFilesHer
e
> 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'v
e
> pulled out so far.  I appreciate any pointers you folks can provide.
>
>

Have you tried debugging it with GDB or some other debugger?





[ Post a follow-up to this message ]



    Re: recursively listing directories  
John


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:11 PM

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?







[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 08:34 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register