Bonjour,
Je suis entrain d'apprendre à me servir de certaines fonctions sytèmes (GNU/Linux) et je rencontre un problème assez étrange. En fait, j'ouvre le répertoire courant, je récupère le nom de tous les fichiers dans un tableau à deux dimensions, j'essaye de trier ce tableau à l'aide de la fonction qsort pour en fin, l'afficher.
Premier problème : le fichier .. pour revenir en arrière apparait sous forme de caractères spéciaux (pas vraiment gênant pour al suite vu que je souhaite l'ignorer une fois la liste triée, en revanche j'aimerais bien savoir pourquoi).
Second problème : il me signale une erreur dans la fonction strcmp() (or celle là je ne l'ai pas codée) et je ne la comprends pas. Surtout qu'elle entraine un segfault...
Je vous montre mon code :
Une bibliothèque de fonctions et le programme :
librep.c
librep.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
83
84
85
86
87
88 #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <dirent.h> #include "librep.h" DIR* rep_ouvrir(char* nom_rep) { DIR* rep = NULL; if (!(rep = opendir(nom_rep))) { fprintf(stderr, "Repertoire introuvable\n"); } return rep; } void rep_fermer(DIR* rep) { closedir(rep); } int rep_nb_fichiers(DIR* rep) { int i = 0; struct dirent* fichier = NULL; rewinddir(rep); while ((fichier = readdir(rep))) { i++; } rewinddir(rep); return i; } char** rep_recuperer_nom_fichiers(DIR* rep) { int nb_fichiers = 0; int i = 0; char** liste_fichiers = NULL; struct dirent* fichier = NULL; nb_fichiers = rep_nb_fichiers(rep); if ((liste_fichiers = malloc(sizeof(char) * nb_fichiers))) { while ((fichier = readdir(rep))) { if ((liste_fichiers[i] = malloc(sizeof(char) * (strlen(fichier->d_name) + 1)))) { liste_fichiers[i] = fichier->d_name; } i++; } } return liste_fichiers; } char* rep_recuperer_courant(void) { return get_current_dir_name(); } static int compare(const void* a, const void* b) { return strcmp((const char*) a, (const char*) b); } void rep_tri_fichiers(char** liste_fichiers, int nb_fichiers) { qsort(liste_fichiers, nb_fichiers, sizeof(char), compare); }
ls.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 #ifndef LIBREP_H #define LIBREP_H DIR* rep_ouvrir(char* nom_rep); void rep_fermer(DIR* rep); int rep_nb_fichiers(DIR* rep); char** rep_recuperer_nom_fichiers(DIR* rep); char* rep_recuperer_courant(void); void rep_tri_fichiers(char** liste_fichiers, int nb_fichiers); #endif
Voilà, merci à vous.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <dirent.h> #include "librep.h" void ecrire(char** liste_fichiers, int nb_fichiers) { int i = 0; for (; i < nb_fichiers; i++) { printf("%s\n", liste_fichiers[i]); } } int my_ls(char* nom) { int nb_fichiers = 0; DIR* rep_courant = NULL; char** liste_fichiers = NULL; if ((rep_courant = rep_ouvrir(nom))) { liste_fichiers = rep_recuperer_nom_fichiers(rep_courant); nb_fichiers = rep_nb_fichiers(rep_courant); rep_tri_fichiers(liste_fichiers, nb_fichiers); ecrire(liste_fichiers, nb_fichiers); } else { return 0; } return 1; } int main(void) { char* nom_rep_courant = NULL; nom_rep_courant = rep_recuperer_courant(); my_ls(nom_rep_courant); return 0; }
Bye.
EDIT : le problème se situe au niveau du tri du tableau de chaine de caractères, mais je ne comprends pas pourquoi (utilisation de qsort ?).
Partager