#include
#include
#include
#include
enum EXE_OPT {
LS_ALL = 0,
FIND_FILE = 1,
};
int fschcnt;
int exeopt;
char ffile[256];
char flist[128][256];
void print_depth(int depth)
{
int i;
for(i=0;i<depth;i++) printf(" ");
}
void Find_file(int depth,char* basename,char* dname)
{
char newdir[256];
DIR *dir;
struct dirent *dp;
struct stat buf;
chdir(dname);
if((dir=opendir("."))==NULL) return;
while(dp = readdir(dir)){
if(stat(dp->d_name, &buf) < 0)
return;
if(strcmp(dp->d_name,".")!=0 && strcmp(dp->d_name,"..")!=0){
switch(buf.st_mode & S_IFMT){
case S_IFDIR:
if(exeopt == LS_ALL){
print_depth(depth+1);
printf("--> %s/\n",dp->d_name);
}
sprintf(newdir,"%s\\%s",basename,dp->d_name);
Find_file(depth+1,newdir,dp->d_name);
chdir("..");
break;
case S_IFREG:
if(strcmp(dp->d_name,ffile) == 0){
sprintf(flist[fschcnt],basename);
fschcnt++;
}
if(exeopt == LS_ALL){
print_depth(depth+1);
printf("[f] %s\n",dp->d_name);
}
break;
default:
print_depth(depth+1);
printf("\t[x] %s\n",dp->d_name);
break;
}
}
}
closedir(dir);
}
int main(int argc,char* argv[])
{
int i;
fschcnt = 0;
if(argc <= 1){
exeopt = LS_ALL;
} else {
exeopt = FIND_FILE;
sprintf(ffile,"%s",argv[1]);
}
Find_file(0,".",".");
if(exeopt == FIND_FILE){
printf("\nfile [%s]:",ffile);
printf(" %d file found\n\n",fschcnt);
for(i=0;i<fschcnt;i++){
printf(" %s\\\n",flist[i]);
}
printf("\n");
}
return 0;
}
|