diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-07-17 19:30:52 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-07-17 19:30:52 +0200 |
| commit | 880102ae9358db130ef67cc9a7177a1e1de76875 (patch) | |
| tree | b5929722cf123e7c1701f4bc164a905f79510bf5 /c10/ex01/io.c | |
| parent | dcf89e9366538c70fc08ceaabd5b770f5d5b890d (diff) | |
| download | piscine-880102ae9358db130ef67cc9a7177a1e1de76875.tar.gz piscine-880102ae9358db130ef67cc9a7177a1e1de76875.tar.bz2 piscine-880102ae9358db130ef67cc9a7177a1e1de76875.zip | |
c10 tail stuff, c11 start, c12/c13 files
Diffstat (limited to 'c10/ex01/io.c')
| -rw-r--r-- | c10/ex01/io.c | 29 |
1 files changed, 21 insertions, 8 deletions
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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 <fcntl.h> #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; |
