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
|
char *get_next_line(const int fd)
{
static char buffer[READ_SIZE];
static int i = 0;
char *str;
int j;
j = 0;
if ((str = malloc(sizeof(char) * READ_SIZE)) == NULL)
return (NULL);
if (read(fd, buffer, READ_SIZE) > 0)
{
while (1)
{
if (buffer[i] == '\n')
{
i++;
return (str);
}
if (buffer[i] == '\0')
{
i = 0;
str = my_realloc(str, READ_SIZE);
if (read(fd, buffer, READ_SIZE) == 0)
{
if (str[0] != '\0')
return (str);
return (NULL);
}
}
str[j] = buffer[i];
j = j + 1;
i = i + 1;
}
free(str);
}
}
int main(int argc, char **argv)
{
int fd;
if (READ_SIZE == 0)
return (-1);
fd = open(argv[1], O_RDONLY);
printf("%d\n", fd);
printf("%s\n", get_next_line(fd));
printf("%s\n", get_next_line(fd));
printf("%s\n", get_next_line(fd));
} |
Partager