From cffe7f69cf28ab0889bee54a18c3f39101b33f16 Mon Sep 17 00:00:00 2001 From: Cabergs Charles Date: Wed, 17 Jul 2019 19:30:52 +0200 Subject: c10 tail stuff, c11 start, c12/c13 files --- c10/ex01/io.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'c10/ex01/io.c') diff --git a/c10/ex01/io.c b/c10/ex01/io.c index a6c0b01..b753902 100644 --- a/c10/ex01/io.c +++ b/c10/ex01/io.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/15 19:05:32 by cacharle #+# #+# */ -/* Updated: 2019/07/16 10:52:03 by cacharle ### ########.fr */ +/* Updated: 2019/07/17 11:19:51 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,12 +14,12 @@ #include #include "include.h" -int read_in_buf(int fildes, char buf[BUF_SIZE]) +int read_in_buf(int fildes, char buf[BUF_SIZE]) { - return (read(fildes, buf, BUF_SIZE - 1)); + return (read(fildes, buf, BUF_SIZE)); } -int write_buf(char buf[BUF_SIZE], int size) +int write_buf(char buf[BUF_SIZE], int size) { return (write(1, buf, size)); } @@ -29,10 +29,15 @@ int read_write_fildes(int fildes) int size; char buf[BUF_SIZE]; - size = BUF_SIZE - 1; - while (size == BUF_SIZE - 1 || fildes == STDOUT_FILENO ) + size = BUF_SIZE; + if (fildes == STDIN_FILENO) { - if ((size = read_in_buf(fildes, buf)) == -1) + read_stdin(); + return (0); + } + while (size != 0) + { + if ((size = read_in_buf(fildes, buf)) < 0) return (-1); if (write_buf(buf, size) == -1) return (-1); @@ -40,7 +45,15 @@ int read_write_fildes(int fildes) return (0); } -int print_file(char *filename) +void read_stdin(void) +{ + char buf[32]; + + while (read(STDIN_FILENO, buf, 1)) + write(STDOUT_FILENO, buf, 1); +} + +int print_file(char *filename) { int fildes; -- cgit