Results 1 to 4 of 4
Hello world!
I started coding in linux a few time ago and now i have a problem and i can;t fix it: i want to immitate the dos command 'tree' ...
- 12-12-2007 #1Just Joined!
- Join Date
- Dec 2007
- Posts
- 2
Problems codding in C
Hello world!
I started coding in linux a few time ago and now i have a problem and i can;t fix it: i want to immitate the dos command 'tree' for a folder(<DIR> )
pr4.c
#include<stdio.h>
#include"listdir.i"
int main(int argc, char** argv)
{
if (argc == 2) {if (listdir(argv[1]) == -1) perror(argv[1]);}
else
{
printf("error: No dir.\n");
return 0;
}
}
listdir.i
#include<stdio.h>
#include<sys/types.h>
#include<dirent.h>
#include<string.h>
#include"fileinfo.i"
int parcurg(const char* dir, DIR* pd, struct dirent* pde)
{
printf("%s\n", dir);
if ((pd = opendir(dir)) == NULL) return -1;
else
if ((pde=readdir(pd)) != NULL)
{
char cale[256], specificator[256];
strcpy(cale, dir); strcat(cale,"/");
strcpy(specificator, cale);
strcat(specificator, pde->d_name);
int ok = fileinfo(specificator);
if (ok)// inseamna ca este director
{
DIR* pc;
struct dirent* pdc;
parcurg(pde->d_name, pd, pde);
parcurg(dir, pd, pde);
}
else
parcurg(dir, pd, pde);
}
else
{
closedir(pd);
return 1;
}
}
int listdir(const char* dir)
{
DIR* pd;
struct dirent *pde;
if (parcurg(dir, pd, pde) == 1) return 0;
else return- 1;
}
fileinfo.i
#include<sys/types.h>
#include<string.h>
#include<stdio.h>
#include<sys/stat.h>
int afisare(int j, const char* nf)
{
if (nf[j] == '/') return 0;
else
{
afisare(j-1, nf);
printf("%c", nf[j]);
}
}
int fileinfo(const char* nf)
{
struct stat s;
if (stat(nf, &s) == -1) return -1;
afisare(strlen(nf)-1, nf);
if ((s.st_mode & S_IFMT) == S_IFDIR)
{ printf(" <DIR>\n"); return 1;}
else
{ printf("\n"); return 0;}
}
- 12-13-2007 #2
What is the problem, exactly? How does this program misbehave?
--
Bill
Old age and treachery will overcome youth and skill.
- 12-13-2007 #3Just Joined!
- Join Date
- Dec 2007
- Posts
- 2
Here is that this code do:
-fileinfo.i: checks if a file read is dir(return 1) else it is a common file (return 0)
-listdir.i: this is the core of the problem. here i intend to create a recursive function in order to navigate throw directories
Thanks for your reply!Code:int traverse(const char* dir, DIR* pd, struct dirent* pde) { printf("%s\n", dir); if ((pd = opendir(dir)) == NULL) return -1; else if ((pde=readdir(pd)) != NULL) { char parent_path[256]; strcpy(parent_path, dir); strcat(parent_path,"/"); strcat(parent_path, pde->d_name); int ok = fileinfo(specificator); if (ok)// if ok = 1 it means it is a directory { //the problem is here because every time i discover a new directory, i create a new DIR structure but it shows me the first directory that i found for an infinite time. i don't know where to declare a new DIR structure and read correctly it's sons DIR* pc; struct dirent* pdc; traverse(pde->d_name, pd, pde); traverse(dir, pd, pde); } else traverse(dir, pd, pde); } else { closedir(pd); return 1; }
- 12-13-2007 #4
I'm not sure the best way to fix how your program is structured, because you're the artist here. But I notice that the only time you do a readdir() is if you've just done an opendir(), so if you do opendir() for a total of 7 times successfully, you'll do readdir() for a total of only 7 times. If there are 500 entries in a directory, for example, it would seem to me that you would (at least eventually) do readdir() 500 times for that one call to opendir().
Hope this helps.--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote