aboutsummaryrefslogtreecommitdiff
path: root/c10/ex01
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-07-16 12:58:13 +0200
committerCharles <sircharlesaze@gmail.com>2019-07-16 12:58:13 +0200
commit14914f50c3de6c5444e13cf67db064b03c1c90ef (patch)
tree3ecf5a41b4541980ba0287a431681cd5ae25ad84 /c10/ex01
parent217bcb0d4e3ba60604921cb40d5a11a64f93cfc7 (diff)
downloadpiscine-14914f50c3de6c5444e13cf67db064b03c1c90ef.tar.gz
piscine-14914f50c3de6c5444e13cf67db064b03c1c90ef.tar.bz2
piscine-14914f50c3de6c5444e13cf67db064b03c1c90ef.zip
c09 passed, c10 start, exam00 and exam01
Diffstat (limited to 'c10/ex01')
-rw-r--r--c10/ex01/Makefile1
-rw-r--r--c10/ex01/helper.c35
-rw-r--r--c10/ex01/include.h34
-rw-r--r--c10/ex01/io.c56
-rw-r--r--c10/ex01/main.c34
5 files changed, 160 insertions, 0 deletions
diff --git a/c10/ex01/Makefile b/c10/ex01/Makefile
new file mode 100644
index 0000000..362d434
--- /dev/null
+++ b/c10/ex01/Makefile
@@ -0,0 +1 @@
+OUT = ft_cat
diff --git a/c10/ex01/helper.c b/c10/ex01/helper.c
new file mode 100644
index 0000000..8a1c13e
--- /dev/null
+++ b/c10/ex01/helper.c
@@ -0,0 +1,35 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* helper.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/07/16 10:14:16 by cacharle #+# #+# */
+/* Updated: 2019/07/16 10:26:44 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include "include.h"
+
+void ft_putstr_err(char *str)
+{
+ while (*str)
+ write(STDERR_FILENO, str++, 1);
+}
+
+void handle_error(char *program_name, char *arg)
+{
+ char *error_msg;
+
+ ft_putstr_err(program_name);
+ ft_putstr_err(": ");
+ ft_putstr_err(arg);
+ ft_putstr_err(": ");
+ error_msg = strerror(errno);
+ ft_putstr_err(error_msg);
+ ft_putstr_err("\n");
+}
diff --git a/c10/ex01/include.h b/c10/ex01/include.h
new file mode 100644
index 0000000..fed49e8
--- /dev/null
+++ b/c10/ex01/include.h
@@ -0,0 +1,34 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* include.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/07/15 16:49:28 by cacharle #+# #+# */
+/* Updated: 2019/07/16 10:23:45 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef INCLUDE_H
+# define INCLUDE_H
+
+# define BUF_SIZE 64
+
+/*
+** io.c
+*/
+
+int read_in_buf(int fildes, char buf[BUF_SIZE]);
+int write_buf(char buf[BUF_SIZE], int size);
+int read_write_fildes(int fildes);
+int print_file(char *filename);
+
+/*
+** helper.c
+*/
+
+void handle_error(char *program_name, char *arg);
+void ft_putstr_err(char *str);
+
+#endif
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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/07/15 19:05:32 by cacharle #+# #+# */
+/* Updated: 2019/07/16 10:52:03 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <unistd.h>
+#include <fcntl.h>
+#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);
+}
diff --git a/c10/ex01/main.c b/c10/ex01/main.c
new file mode 100644
index 0000000..b5fb416
--- /dev/null
+++ b/c10/ex01/main.c
@@ -0,0 +1,34 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* main.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/07/15 16:45:20 by cacharle #+# #+# */
+/* Updated: 2019/07/16 10:20:39 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <unistd.h>
+#include "include.h"
+
+int main(int argc, char **argv)
+{
+ int i;
+ int fildes;
+ int status;
+
+ status = 0;
+ i = 1;
+ while (i < argc)
+ {
+ if (print_file(argv[i]) == -1)
+ {
+ handle_error(argv[0], argv[i]);
+ status = 1;
+ }
+ i++;
+ }
+ return (status);
+}