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
| #include
#include
#include
#include
<dirent.h>
<regex.h>
<stdio.h>
<stdlib.h>
regex_t motif_recherche;
int
selection(const struct dirent * entree)
{
if (regexec(& motif_recherche, entree -> d_name, 0, NULL, 0) == 0)
return 1;
return 0;
}
int
main (int argc, char * argv[])
{
struct dirent ** liste;
int
nb_entrees;
int
i;
if (argc != 3) {
fprintf(stderr, "Syntaxe : %s répertoire motif\n", argv [0]);
exit(EXIT_FAILURE);
}
if (regcomp(& motif_recherche, argv[2], REG_NOSUB) !=0) {
fprintf(stderr, "Motif illégal\n");
exit(EXIT_FAILURE);
}
nb_entrees = scandir(argv[1], & liste, selection, alphasort);
if (nb_entrees <= 0)
exit(EXIT_SUCCESS);
for (i = 0; i < nb_entrees; i ++) {
fprintf(stdout, " %s\n", liste[i]->d_name);
free(liste[i]);
}
fprintf(stdout, "\n");
free(liste);
return EXIT_SUCCESS;
} |
Partager