Skip to content

Commit 89b8c17

Browse files
committed
create
0 parents  commit 89b8c17

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

get_next_line.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "get_next_line.h"
2+
3+
static char *ft_joinchr(char const *line, char c, size_t len)
4+
{
5+
char *s;
6+
size_t i;
7+
8+
if (!(s = (char*)malloc((len + 1) * sizeof(char))))
9+
return (NULL);
10+
i = 0;
11+
while (i < len)
12+
s[i++] = (*line) ? *line++ : c;
13+
s[i] = '\0';
14+
return (s);
15+
}
16+
17+
int get_next_line(char **line)
18+
{
19+
char buffer;
20+
int res;
21+
char *tmp;
22+
size_t len;
23+
24+
if (!line || !(*line = (char *)malloc(1)))
25+
return (-1);
26+
**line = '\0';
27+
len = 0;
28+
while ((res = read(0, &buffer, 1)) > 0 && buffer != '\n')
29+
{
30+
if (!(tmp = ft_joinchr(*line, buffer, ++len)))
31+
return -1;
32+
free(*line);
33+
*line = tmp;
34+
}
35+
return (res);
36+
}

get_next_line.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef GET_NEXT_LINE_H
2+
# define GET_NEXT_LINE_H
3+
4+
# include <unistd.h>
5+
# include <stdlib.h>
6+
7+
int get_next_line(char **line);
8+
9+
#endif

main.c

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* main.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: ncolomer <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2019/11/11 16:29:19 by ncolomer #+# #+# */
9+
/* Updated: 2019/11/14 19:20:21 by ncolomer ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include <stdio.h>
14+
#include <unistd.h>
15+
#include <stdlib.h>
16+
#include "get_next_line.h"
17+
18+
int
19+
main(void)
20+
{
21+
int r;
22+
char *line;
23+
24+
line = NULL;
25+
while ((r = get_next_line(&line)) > 0)
26+
{
27+
printf("%s\n", line);
28+
free(line);
29+
line = NULL;
30+
}
31+
printf("%s", line);
32+
free(line);
33+
line = NULL;
34+
}

subject.en.txt

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Assignment name : get_next_line
2+
Expected files : get_next_line.c get_next_line.h
3+
Allowed functions: read, free, malloc
4+
--------------------------------------------------------------------------------
5+
6+
Write a function will store, in the parameter "line", a line that has been read from the file descriptor 0.
7+
8+
Your function must be prototyped as follows: int get_next_line(char **line);
9+
10+
Your function should be memory leak free.
11+
12+
What we call a "line that has been read" is a succession of 0 to n characters that end with '\n' (ascii code 0x0a) or with End Of File (EOF).
13+
14+
The string stored in the parameter "line" should not contained any '\n'.
15+
16+
The parameter is the address of a pointer to a character that will be used to store the line read.
17+
18+
The return value can be 1, 0 or -1 depending on whether a line has been read, when the reading has been completed (meaning read has returned 0), or if an error has happened respectively.
19+
20+
When you've reached the End Of File, you must store the current buffer in "line". If the buffer is empty you must store an empty string in "line".
21+
22+
When you've reached the End Of File, your function should keep 0 memory allocated with malloc except the last buffer that you should have stored in "line".
23+
24+
What you've stored in "line" should be free-able.
25+
26+
Calling your function get_next_line in a loop will therefore allow you to read the text available on a file descriptor one line at a time until the end of the text, no matter the size of either the text or one of its lines.
27+
28+
Make sure that your function behaves well when it reads from a file, from the standard output, from a redirection etc.
29+
30+
No call to another function will be done on the file descriptor between 2 calls of get_next_line.
31+
32+
Finally we consider that get_next_line has an undefined behavior when reading from a binary file.
33+
34+
You should use the test.sh to help you test your get_next_line.

test.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
gcc -Wall -Werror -Wextra get_next_line.c main.c -o get_next_line
2+
./get_next_line < get_next_line.c > yours_.res
3+
cat -e yours_.res > yours.res
4+
cat -e < get_next_line.c > original.res
5+
diff -y --suppress-common-line original.res yours.res
6+
rm -rf original.res yours_.res yours.res get_next_line

0 commit comments

Comments
 (0)