1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
char recherche(char *rep, char *fichier, DIR *new_dir);
void *xmalloc_mem(int longueur_chaine, unsigned char size);
int main(int argc, char **argv)
{
char *chemin = NULL;
char *fichier = NULL;
chemin=argv[1];
recherche(chemin, fichier, NULL);
return 0;
}
char recherche(char *chemin, char *fichier, DIR *new_dir)
{
struct dirent *info_d;
DIR *dir;
char *path;
int longueur_chaine;
if ((dir = opendir(chemin)) == NULL)
{
printf("find: %s: No such file or directory\n", chemin);
return ;
}
while (info_d = readdir(dir))
if ((strcmp(info_d->d_name, ".") != 0) && (strcmp(info_d->d_name, "..") != 0))
{
longueur_chaine=(strlen(chemin)+strlen(info_d->d_name)+2);
path = xmalloc_mem(longueur_chaine,sizeof(*path));
strcat(path, chemin);
strcat(path, info_d->d_name);
/* recherche tous les fichiers dans le repertoire */
if (strstr(info_d->d_name,"") != NULL) printf("%s\n",path);
strcat(path, "/");
if ((new_dir = opendir(path)) != NULL)
recherche(path, fichier, new_dir);
free(path);
}
closedir(dir);
return 1;
}
/**********************************************************************/
//FONCTION ALLOUER MEMOIRE
void *xmalloc_mem(int longueur_chaine, unsigned char size)
{
char *ALLOCATION_2;
if ((ALLOCATION_2=malloc(longueur_chaine * size)) == NULL){
printf("Erreur allocation\n");
exit(-1);
}
memset(ALLOCATION_2, '\0',size);
return (ALLOCATION_2);
} |
Partager