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;
} 6 3324
Dreamcatcher wrote: 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
David Resnick wrote: Dreamcatcher wrote: 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
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.
Dreamcatcher wrote: David Resnick wrote: Dreamcatcher wrote: 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
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
Dreamcatcher schrieb: 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.
Dreamcatcher wrote: 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>
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Dreamcatcher <ro***************@gmail.com> writes: 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.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: hokiegal99 |
last post by:
A few questions about the following code. How would I "wrap" this in a
function, and do I need to?
Also, how can I make the code smart enough to realize that when a file
has 2 or more bad...
|
by: John Caruthers |
last post by:
HELP!
This problem is driving me crazy. All other web sites
that are being served out through this server are working,
including sites and virtual directories that are under the
Default Web...
|
by: Dan Nash |
last post by:
Hi guys
I have a page that is *supposed* to list the directories on the server.
Here's the code...
folderspec = server.MapPath("./images/")
set fso =...
|
by: csx |
last post by:
Hi all,
Ive got a problem with recursion in Javascript.
For this tree:
http://www.pcm.uklinux.net/structure.jpg
If you input node 3 (i.e. C) which is represented as 'values' in the
array, it...
|
by: Mike |
last post by:
Sites using thumbnail preview for world wide web file navigation and
searching.
Below are list of sites that are either researching or providing thumbnail
preview images for online web...
| |
by: Ioannis Vranos |
last post by:
The following code is supposed to print all folder names of a folder but it does not work.
Why?
Change the folder in main() to a folder suitable for your system so as to test it.
I created...
|
by: Michael Sperlle |
last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems
to need changes to the kernel before it can be installed, and I have no
intention of going through whatever hell that would...
|
by: Tyrone Slothrop |
last post by:
I have created a script which recurses a display of directories like
so:
<?
$dir = "/path/to/base/directory";
function scan_dir_recurse ($dir,$tab)
{
global $fileArr;
$tab++;
if ($tab 4) {...
|
by: elventear |
last post by:
Hello everyone,
I am runing into recursion limit problems. I have found that the
culprit was related to the __hash__ function that I had assigned to
the objects that were added to a set.
...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |