I'm having some trouble with one of the FAQ examples
http://faq.cprogramming.com/cgi-bin/...&id=1044780608
I have adapted the code as below yet all I get is directory tree with 3 sub folders searched over 1000 times and still not finding the file!
Code:
#include <dirent.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int dn = 0;
int walker( char *searching, char *result ) {
DIR *d;
struct dirent *dir;
d = opendir( "/home/s/Pictures" );
if( d == NULL ) {
return 1;
}
while( ( dir = readdir( d ) ) ) {
if( strcmp( dir->d_name, "." ) == 0 ||
strcmp( dir->d_name, ".." ) == 0 ) {
continue;
}
if( dir->d_type == DT_DIR ) {
chdir( dir->d_name );
dn++;
printf("Searching......%s\n",dir->d_name);
printf("Directory number %i\n",dn);
walker( searching, result );
chdir( ".." );
} else {
if( strcmp( dir->d_name, searching ) == 0 ) {
int len;
getcwd( result, MAXPATHLEN );
len = strlen( result );
snprintf( result + len, MAXPATHLEN - len, "/%s", dir->d_name );
break;
}
}
}
closedir( d );
return *result == 0;
}
int main( ) {
char buf[MAXPATHLEN] = { 0 };
if( walker( "Fenestra.png", buf ) == 0 ) {
printf( "Found: %s\n", buf );
} else {
puts( "Not found" );
}
return 0;
}