Problems using recursion while browsing directories

D

Dreamcatcher

Hello,

I'm trying to learn some C, reading my book, Beginning Linux Programming
I came across the following program. The program is supposed to walk
through directory's and print all its subdirectories. However the
program never seems to get out of the loop its in. I tried finding the
error but I don't see where it goes wrong, hopefully someone can shed a
light on this, source code is pasted below


/*
* This program should list the directory's + subdirs
* The search should begin on the location specief in
* main()
*
* This is a program taken from "Beginning Linux Programming"
*/

#include <unistd.h>
#include <stdio.h>
#include <dirent.h> /* All header files */
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>

/* Here we write the printdir function */
void printdir(char *dir, int depth)
{

DIR *dp;
struct dirent *entry;
struct stat statbuf;

/* We use opendir() to check wether the
directory from main() exists */
if((dp = opendir(dir)) == NULL) {
fprintf(stderr,"cannot open directory: %s\n", dir);
return;
}

/* If the directory exists we use chdir() to go into the
directory.
If the directory does not exist we printf above error

We use a recursive while loop to make sure it continues
aslong as
there are unlisted directory's */

chdir(dir);
while((entry = readdir(dp)) != NULL) {
lstat(entry -> d_name, &statbuf);
if(S_ISDIR(statbuf.st_mode)) {
if (strcmp(".", entry -> d_name) == 0)
continue;
printf("%*s%s/\n", depth,"", entry -> d_name);
printdir(entry -> d_name, depth + 4);
}
else printf("%*s%s/\n", depth,"", entry -> d_name);
}

/* When the program is doen crawling through the directory's
it use chdir()
to go back to the parent directory where it calls
closedir() to close the
opendir stram */

chdir("..");
closedir(dp);
}

int main()
{

printf("Directory scan of /home/roeland/programming:\n");
printdir("/home/roeland/programming",0); /* The
directory that needs to be listed */
printf("Done!\n");

return 0;
}
 
D

David Resnick

Dreamcatcher said:
Hello,

I'm trying to learn some C, reading my book, Beginning Linux Programming
I came across the following program. The program is supposed to walk
through directory's and print all its subdirectories. However the
program never seems to get out of the loop its in. I tried finding the
error but I don't see where it goes wrong, hopefully someone can shed a
light on this, source code is pasted below


/*
* This program should list the directory's + subdirs
* The search should begin on the location specief in
* main()
*
* This is a program taken from "Beginning Linux Programming"
*/

#include <unistd.h>
#include <stdio.h>
#include <dirent.h> /* All header files */
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>

/* Here we write the printdir function */
void printdir(char *dir, int depth)
{

DIR *dp;
struct dirent *entry;
struct stat statbuf;

/* We use opendir() to check wether the
directory from main() exists */
if((dp = opendir(dir)) == NULL) {
fprintf(stderr,"cannot open directory: %s\n", dir);
return;
}

/* If the directory exists we use chdir() to go into the
directory.
If the directory does not exist we printf above error

We use a recursive while loop to make sure it continues
aslong as
there are unlisted directory's */

chdir(dir);
while((entry = readdir(dp)) != NULL) {
lstat(entry -> d_name, &statbuf);
if(S_ISDIR(statbuf.st_mode)) {
if (strcmp(".", entry -> d_name) == 0)
continue;
printf("%*s%s/\n", depth,"", entry -> d_name);
printdir(entry -> d_name, depth + 4);
}
else printf("%*s%s/\n", depth,"", entry -> d_name);
}

/* When the program is doen crawling through the directory's
it use chdir()
to go back to the parent directory where it calls
closedir() to close the
opendir stram */

chdir("..");
closedir(dp);
}

int main()
{

printf("Directory scan of /home/roeland/programming:\n");
printdir("/home/roeland/programming",0); /* The
directory that needs to be listed */
printf("Done!\n");

return 0;
}

C doesn't have a portable way to handle directories. And the problem
is unix specific as well. Followups set to comp.unix.programmer.
<OT> consider what happens when you encounter the directory ".." --
handle that where you handle ".". If that doesn't make sense,
please ask in c.u.p </OT>

-David
 
D

Dreamcatcher

David said:
C doesn't have a portable way to handle directories. And the problem
is unix specific as well. Followups set to comp.unix.programmer.
<OT> consider what happens when you encounter the directory ".." --
handle that where you handle ".". If that doesn't make sense,
please ask in c.u.p </OT>

-David

Hmm, ok.

However, ive tried to do some debugging and found where the program goes
wrong (using printf statements in the code to test where it would go).
The line where it goes wrong is "printdir(entry -> d_name, depth + 4);"

I'll also post this to comp.unix.programmer.
 
F

Flash Gordon

Dreamcatcher said:
Hello,

I'm trying to learn some C, reading my book, Beginning Linux Programming
I came across the following program. The program is supposed to walk
through directory's and print all its subdirectories. However the
program never seems to get out of the loop its in. I tried finding the
error but I don't see where it goes wrong, hopefully someone can shed a
light on this, source code is pasted below

Standard C knows nothing of directories.
/*
* This program should list the directory's + subdirs
* The search should begin on the location specief in
* main()
*
* This is a program taken from "Beginning Linux Programming"
*/

#include <unistd.h>
#include <stdio.h>
#include <dirent.h> /* All header files */
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>

Only 3 of the above are standard C headers. They might be able to help
you over in comp.unix.programmer or one of the Linux groups. Here we
only deal with standard C, not Unix extension, nor Linux, nor Windows,
nor any of the other myriad systems.

<snip code heavily dependant on non-standard libraries>
 
D

David Resnick

Dreamcatcher said:
Hmm, ok.

However, ive tried to do some debugging and found where the program goes
wrong (using printf statements in the code to test where it would go).
The line where it goes wrong is "printdir(entry -> d_name, depth + 4);"

I'll also post this to comp.unix.programmer.

Consider the problems with this statement:

"Handle the directory above mine and all directories below it
recursively."

Where can it end? That is what is happening when you treat ".." as a
valid
directory to recurse to.

-David
 
I

Ingo Menger

Dreamcatcher said:
chdir(dir);

This can go wrong for many reasons.
if(S_ISDIR(statbuf.st_mode)) {
if (strcmp(".", entry -> d_name) == 0)
continue;
printf("%*s%s/\n", depth,"", entry -> d_name);

This might print, for example: ../

printdir(entry -> d_name, depth + 4);

Consequently, this may recurse (almost) forever.
 
K

Keith Thompson

Dreamcatcher said:
David Resnick wrote: [snip]
C doesn't have a portable way to handle directories. And the problem
is unix specific as well. Followups set to comp.unix.programmer.
<OT> consider what happens when you encounter the directory ".." --
handle that where you handle ".". If that doesn't make sense,
please ask in c.u.p </OT>
-David

Hmm, ok.

However, ive tried to do some debugging and found where the program
goes wrong (using printf statements in the code to test where it would
go). The line where it goes wrong is "printdir(entry -> d_name, depth
+ 4);"

I'll also post this to comp.unix.programmer.

The idea was to post to comp.unix.programmer *instead* of comp.lang.c.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top