Bonsoir tout le monde!

Je suis venue avec une version de Get Next Line que j'aimerais comprendre
Voici le gnl.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
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
#include "get_next_line.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
 
int		stock(char **overflow, char **line, int *compteur)
{
	int		i;
	char	*save;
 
	i = 0;
	while ((*overflow)[i])
		if ((*overflow)[i] == '\n')
		{
			save = memalloc((strlen(*overflow) - i) * sizeof(char));
			if (!save)
				return (-1);
			save = strcpy(save, &((*overflow)[i + 1]));
			*line = strncpy(*line, *overflow, i);
			(*line)[i++] = '\0';
			free(*overflow);
			*overflow = save;
			return (1);
		}
		else
			i++;
	*compteur += i;
	*line = strncpy(*line, *overflow, i);
	*line = (char *)realloc(*line, *compteur, *compteur + BUFF_SIZE + 1);
	free(*overflow);
	*overflow = NULL;
	return (0);
}
 
int		line_split(char **overflow, char **line, int *compteur)
{
	int	i;
 
	(*line)[*compteur] = '\0';
	i = 0;
	while ((*line)[i])
	{
		if ((*line)[i] == '\n')
		{
			if ((*line)[i + 1])
			{
				*overflow = (char *)memalloc((*compteur - i) * sizeof(char));
				if (!*overflow)
					return (-1);
				*overflow = strncpy(*overflow, (*line + i + 1), *compteur - i);
				(*overflow)[*compteur - i - 1] = '\0';
			}
			(*line)[i] = '\0';
			return (1);
		}
		i++;
	}
	return (0);
}
 
int		initialisation(const int fd, char ***line, char ***overflow)
{
	if (fd < 0 || fd > 5000 || !*line || FDS <= 0 || BUFF_SIZE <= 0)
		return (-1);
	if (!*overflow)
	{
		if (!(*overflow = (char **)memalloc(FDS * sizeof(char *))))
			return (-1);
	}
	if (!(**line = (char *)memalloc((BUFF_SIZE + 1) * sizeof(char))))
	{
		free(*overflow);
		return (-1);
	}
	return (0);
}
 
int		get_next_line(const int fd, char **line)
{
	static	char	**overflow;
	int				r;
	int				compteur;
	int				resultat;
 
	compteur = 0;
	if (initialisation(fd, &line, &overflow))
		return (-1);
	if (overflow[fd] && (resultat = stock(&(overflow[fd]), line, &compteur)))
		return (resultat);
	while ((r = read(fd, (*line + compteur), BUFF_SIZE)) > 0 && (compteur += r))
		if ((resultat = line_split(&(overflow[fd]), line, &compteur)))
			return (resultat);
		else if (r == BUFF_SIZE)
		{
			*line = (char *)realloc(*line, compteur * sizeof(char),
					(compteur + BUFF_SIZE + 1) * sizeof(char));
			if (!*line)
				return (-1);
		}
	if (r < 0 || ((*line)[compteur] = '\0'))
		return (-1);
	return (**line ? 1 : 0);
}
Voici le gnl.h:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
 
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# include <strings.h>
# define BUFF_SIZE 1
# define FDS 3000
 
int				get_next_line(const int fd, char **line);
 
#endif
Voici le main.c que j'ai réussi à faire:

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
 
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include "get_next_line.h"
#include <string.h>
 
int		main(int argc, char **argv)
{
	int		fd;
	char	*line;
	int		i;
	int		ret;
 
	i = 0;
	(void)argc;
	fd = open((argv[1]), O_RDONLY);
	while (i < 120)
	{
		line = (char *)malloc(sizeof(*line) * 1);
		ret = get_next_line(fd, &line);
		printf("--|%s\n", line);
		i++;
	}
}
Le problème est que je n'arrive pas à comprendre comment fonctionnent les fonctions du fichier gnl.c. Je sais quelle fonction fait quoi grâce aux noms de chaque fonction qui est assez explicite (Je suis assez débrouillarde.), mais je ne comprends pas ce qu'elle fait en détail.
Puis, je n'ai toujours pas réussi à assimiler la méthode du BUFF_SIZE comment un programme se sert de la mémoire.
Donc si certaines personnes ont du temps à me consacrer et à me supporter, je suis preneuse!

Merci!

- Julia