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;
}
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;
}