Moze :o)
recimo listanje direktorijuma sam ja radio rekurzijom...cek sacu da ti izvucem primer sa svim komentarima :
Code:
[b]
/* This function is mother of all :o) *
* if file is directory is is called *
* This one use recursion function call */
int yes_dir(const char *path){ //funny name yes_dir :o))))
struct DIR *tmp;
struct dirent *tmp_dir;
/* Opening our dir path */
tmp=(struct DIR *)opendir(path);
/* No chdir() to that dir */
chdir(path);
/* Show may begin */
while ((tmp_dir=readdir((DIR *)tmp))!=NULL){
/* Now chceck if file is dir */
if (!is_dir(tmp_dir->d_name)){
/* If it isn't call cleanlog() */
cleanlog(tmp_dir->d_name);
}else{
/* if dir is '.' or '..' ignore it *
* I got SIGSEGV with this :o) */
if (strcmp(tmp_dir->d_name,".")!=0 && strcmp(tmp_dir->d_name,"..")!=0){
/* Call again this function */
yes_dir(tmp_dir->d_name);
}
}
}
/* When we clean our subdir we must go one dir up */
chdir("..");
return 0;
}
[/b]
Naime ovu funkciju sam koristio za ciscenje logova stim sto je cleanlog() fja koji dati fajl symlinkuje na /dev/null dok je is_dir() obicna funkcija koja poziva stat() i onda na stat.st_mode & S_IFDIR ako je dir vraca 1 ako nije onda 0...
Naime sta radi ova fja...
Svaki put kad je pozoves ona trazi dirove kad najde jedan ulazi u njega i trazi dalje tako sto poziva samu sebe...posle toga kad zavrsi sa petljanjem po diru izlazi iz njega i nastavlja tamo gde je stala...u prethodnom diru...(tako sto na kraju same sebe poziva chdir("..");