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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| #include <stdio.h> #include <stdlib.h> #include <regex.h> #include <dirent.h> #include "projet.h" #include <link.h> #include <string.h>
int isDir(struct dirent* currentFile) { if ((strchr(currentFile->d_name, '.')) == NULL) /* Si le nom du fichier n'a pas de point (une extension). */ return 1; else return 0; }
char* concatpath(char *s1,char *s2) { char *s3=NULL; s3=(char *)malloc((strlen(s1)+strlen(s2))*sizeof(char)); strcpy(s3,s1); strcat(s3,"\"); strcat(s3,s2); return s3; }
int parcours_directory (DIR* rep, regex_t exp, char* path, struct link* l ) { int cpt = 0; //compteur pour debugger struct dirent* currentFile; int match; while ((currentFile = readdir(rep)) != NULL) { ++cpt; printf("toto parcours 1\n %d\n", cpt); if (isDir(currentFile)) { printf("toto parcours 2\n %d\n", cpt); path=concatpath(path, currentFile->d_name); // on met à jour le path rep = opendir(path); parcours_directory(rep, exp, path, l); closedir(rep); } if ((match = regexec (&exp, currentFile->d_name,0, NULL, 0)) == 1) { struct lelement* e; printf("toto parcours 3\n %d\n", cpt); e = create_lelement(currentFile->d_name, path); lnk__add_head(l,e); // on stock dans la link display_lnk(l); } } return 0; }
int main (int argc, char *argv[]) // argument 1 = chemin racine, argument 2 = expression régulière à chercher { DIR* rep; regex_t exp; const char* str_regex = argv[2]; char* path = argv[1]; struct link* l = lnk__empty(); printf("%d", argc); if (argc != 3) { printf(" les arguments doivent etre valides : argument 1 = chemin racin, argument = expression reguliere a chercher."); return 0; } rep = opendir(path); printf("toto\n"); regcomp (&exp, str_regex, REG_NOSUB | REG_EXTENDED); // compilation de l'expression régulière parcours_directory(rep, exp, path, l); printf("tot1"); display_lnk(l); regfree (&exp); lnk_free(l); free(path); return 0; } |