diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-10-24 11:03:09 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-10-24 11:03:09 +0200 |
| commit | d9b69c88cc2105351e230dd7da6ff77045318b00 (patch) | |
| tree | fb1e3008df2ddcc1e7995073df0aac61798d698a | |
| parent | 9d66e3d084f575187b334d171a8bed40c476aa08 (diff) | |
| download | get_next_line-d9b69c88cc2105351e230dd7da6ff77045318b00.tar.gz get_next_line-d9b69c88cc2105351e230dd7da6ff77045318b00.tar.bz2 get_next_line-d9b69c88cc2105351e230dd7da6ff77045318b00.zip | |
normed and protect against BUFFER_SIZE < 0
| -rw-r--r-- | get_next_line.c | 18 | ||||
| -rw-r--r-- | get_next_line.h | 8 | ||||
| -rw-r--r-- | main.c | 9 |
3 files changed, 22 insertions, 13 deletions
diff --git a/get_next_line.c b/get_next_line.c index d5c4293..879dd0a 100644 --- a/get_next_line.c +++ b/get_next_line.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/19 09:08:36 by cacharle #+# #+# */ -/* Updated: 2019/10/20 09:04:18 by cacharle ### ########.fr */ +/* Updated: 2019/10/24 10:55:30 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,15 +33,13 @@ ** return END_OF_FILE */ -int get_next_line(int fd, char **line) +int real_get_next_line(int fd, char **line, int ret, int split_at) { t_bool had_rest; - int ret; - int split_at; char buf[BUFFER_SIZE + 1]; static char rest[OPEN_MAX][BUFFER_SIZE + 1] = {{0}}; - if (fd < 0 || fd > OPEN_MAX || line == NULL) + if (fd < 0 || fd > OPEN_MAX || line == NULL || BUFFER_SIZE < 0) return (ERROR); if ((had_rest = put_rest(line, rest[fd])) == -1) return (LINE_READ); @@ -63,6 +61,16 @@ int get_next_line(int fd, char **line) return (ret == -1 ? ERROR : END_OF_FILE); } +int get_next_line(int fd, char **line) +{ + int ret; + int split_at; + + split_at = -1; + ret = 0; + return (real_get_next_line(fd, line, ret, split_at)); +} + int put_rest(char **line, char *rest) { int split_at; diff --git a/get_next_line.h b/get_next_line.h index 6a08080..f1f45aa 100644 --- a/get_next_line.h +++ b/get_next_line.h @@ -6,15 +6,17 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/09 13:52:59 by cacharle #+# #+# */ -/* Updated: 2019/10/20 08:28:34 by cacharle ### ########.fr */ +/* Updated: 2019/10/24 10:56:13 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef GET_NEXT_LINE_H # define GET_NEXT_LINE_H +# include <limits.h> + # ifndef BUFFER_SIZE -# define BUFFER_SIZE 66 +# define BUFFER_SIZE 1 # endif # define LINE_READ 1 @@ -26,11 +28,11 @@ typedef int t_bool; -#include <stdio.h> /* ** get_next_line.c */ +int real_get_next_line(int fd, char **line, int ret, int split_at); int get_next_line(int fd, char **line); int put_rest(char **line, char *rest); int find_newline(char *str); @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/10 09:27:41 by cacharle #+# #+# */ -/* Updated: 2019/10/20 09:03:18 by cacharle ### ########.fr */ +/* Updated: 2019/10/24 10:56:42 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,7 @@ int main(int argc, char **argv) char *line; int ret; - /* printf("limit fdmax %d\n", OPEN_MAX); */ + printf("limit fdmax %d\n", OPEN_MAX); if (argc != 2) { printf("You forgot the filename"); @@ -32,12 +32,11 @@ int main(int argc, char **argv) } printf("BUFFER_SIZE = %d\n", BUFFER_SIZE); fd = open(argv[1], O_RDONLY); - i = 1; + i = 0; while ((ret = get_next_line(fd, &line)) == LINE_READ) { - printf("%3d [%s]\n", i, line); + printf("%3d [%s]\n", ++i, line); free(line); - i++; } if (ret == -1) printf("error\n"); |
