Bonsoir a tous,

J'ai un petit soucis avec une fonction récursive qui est censé mettre dans une liste les fichiers et dossiers d'un répertoire le problème c'est qu'il n'ajoute pas tout les éléments.

Un coup de main ?

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
 
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
 
typedef struct element {
	int isDir;
	char* name;
}Element;
 
typedef struct cell {
	Element elem;
	struct cell *next;
}Cell;
 
typedef Cell *List;
 
void error(char *s)
{
	fprintf(stderr, "%s", s);
	exit(EXIT_FAILURE);
}
 
List newEmptyList()
{
	return (List)NULL;
}
 
int isEmpty(List l)
{
	return (l == NULL);
}
 
List createElement(Element e)
{
	List l;
 
	if((l = (Cell *) malloc(sizeof(Cell))) == NULL)
		error("Erreur : allocation echoue\n");
 
	l->elem.isDir = e.isDir;
	l->elem.name = e.name;
	l->next = NULL;
 
	return l;
}
 
void addElement(List *l, Element e)
{
	if(*l == NULL)
		*l = createElement(e);
	else
		addElement(&((*l)->next), e);
}
 
Element getFirst(List l)
{
	if(isEmpty(l))
		error("Erreur : Aucun element\n");
 
	return l->elem;
}
 
List getNext(List l)
{
	if(isEmpty(l))
		error("Erreur : Aucun element\n");
 
	return l->next;
}
 
Element setFirst(List *l, Element e)
{
	(*l)->elem.isDir = e.isDir;
	(*l)->elem.name = e.name;
}
 
List setNext(List *l, List nxt)
{
	(*l)->next = nxt;
}
 
void freeCell(Cell *c)
{
	free(c);
}
 
void printList(List l)
{
	List tmp = l;
 
	while(!isEmpty(tmp))
	{
		printf("%d ", tmp->elem.isDir);
		printf("%s\n", tmp->elem.name);
 
		tmp = tmp->next;
	}
}
 
void printElem(Element e)
{
	printf("%d ", e.isDir);
	printf("%s\n", e.name);
}
 
List arborescence(char *dir)
{
	DIR *dp;
	struct dirent *entry;
	struct stat statbuf;
 
	List l = newEmptyList();
	Element e;
 
	if((dp = opendir(dir)) == NULL)
	{
		fprintf(stderr, "cannot open directory: %s\n", dir);
 
		return NULL;
	}
 
	chdir(dir);
 
	while((entry = readdir(dp)) != NULL)
	{
		lstat(entry->d_name, &statbuf);
 
		if(S_ISDIR(statbuf.st_mode))
		{
			if(strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0)
				continue;
 
			e.isDir = 1;
			e.name = entry->d_name;
			addElement(&l, e);
			//printElem(e);
 
			arborescence(entry->d_name);
		}
		else
		{
			e.isDir = 0;
			e.name = entry->d_name;
			addElement(&l, e);
			//printElem(e);
		}
	}
 
	chdir("..");
	closedir(dp);
 
	return l;
}
 
int main (int argc, char *argv[])
{
	List l = newEmptyList();
 
	l = arborescence("/home/jey/Bureau/DocMyCode");
 
	printList(l);
 
	//system("firefox docmycode.html");
 
	return 0;
}