From 14914f50c3de6c5444e13cf67db064b03c1c90ef Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 16 Jul 2019 12:58:13 +0200 Subject: c09 passed, c10 start, exam00 and exam01 --- c10/ex01/io.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 c10/ex01/io.c (limited to 'c10/ex01/io.c') diff --git a/c10/ex01/io.c b/c10/ex01/io.c new file mode 100644 index 0000000..a6c0b01 --- /dev/null +++ b/c10/ex01/io.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* io.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/15 19:05:32 by cacharle #+# #+# */ +/* Updated: 2019/07/16 10:52:03 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "include.h" + +int read_in_buf(int fildes, char buf[BUF_SIZE]) +{ + return (read(fildes, buf, BUF_SIZE - 1)); +} + +int write_buf(char buf[BUF_SIZE], int size) +{ + return (write(1, buf, size)); +} + +int read_write_fildes(int fildes) +{ + int size; + char buf[BUF_SIZE]; + + size = BUF_SIZE - 1; + while (size == BUF_SIZE - 1 || fildes == STDOUT_FILENO ) + { + if ((size = read_in_buf(fildes, buf)) == -1) + return (-1); + if (write_buf(buf, size) == -1) + return (-1); + } + return (0); +} + +int print_file(char *filename) +{ + int fildes; + + if (filename[0] == '-') + fildes = STDIN_FILENO; + else if ((fildes = open(filename, O_RDONLY)) < 0) + return (-1); + if (read_write_fildes(fildes) == -1) + return (-1); + if (close(fildes) == -1) + return (-1); + return (0); +} -- cgit