Skip to content

Instantly share code, notes, and snippets.

@Silva97
Last active October 4, 2021 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Silva97/bf845f89cd6666296c2f435febf6e369 to your computer and use it in GitHub Desktop.
Save Silva97/bf845f89cd6666296c2f435febf6e369 to your computer and use it in GitHub Desktop.
/********************
* Exemplo por Luiz Felipe.
* https://github.com/Silva97
********************/
#include <stdio.h>
#include <string.h>
#include <unistd.h> // Para usar chdir()
#include <dirent.h>
#include <sys/types.h>
_Bool fsearch(char *path, char *fname, char *folder);
int main(int argc, char **argv){
char destiny[256];
if(!fsearch(destiny, argv[1], ".")){
puts("Arquivo não encontrado");
} else {
printf("Endereço: %s\n", destiny);
}
return 0;
}
_Bool fsearch(char *path, char *fname, char *folder){
_Bool r = 0;
struct dirent *f;
DIR *d = opendir(folder);
chdir(folder);
while(f = readdir(d)){
switch(f->d_type){
case DT_REG:
if(!strcmp(f->d_name, fname)){
getcwd(path, 255);
sprintf(path, "%s/%s", path, f->d_name);
r = 1;
goto fend;
}
break;
case DT_DIR:
if(!strcmp(f->d_name, ".") || !strcmp(f->d_name, ".."))
break;
r = fsearch(path, fname, f->d_name);
if(r)
goto fend;
break;
}
}
fend:
closedir(d);
chdir("..");
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment