aboutsummaryrefslogtreecommitdiff
path: root/c10
diff options
context:
space:
mode:
Diffstat (limited to 'c10')
-rw-r--r--c10/ex00/Makefile17
-rw-r--r--c10/ex00/bonjour4
-rw-r--r--c10/ex00/main.c34
-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
8 files changed, 194 insertions, 21 deletions
diff --git a/c10/ex00/Makefile b/c10/ex00/Makefile
index e69de29..6ee330b 100644
--- a/c10/ex00/Makefile
+++ b/c10/ex00/Makefile
@@ -0,0 +1,17 @@
+OUT = ft_display_file
+CC = gcc
+CCFLAGS = -Wall -Wextra -Werror
+
+.PHONY: all
+all: $(OUT)
+
+$(OUT): main.c
+ $(CC) $(CCFLAGS) -o $(OUT) main.c
+
+.PHONY: clean
+clean:
+
+.PHONY: clean
+fclean: clean
+ rm -f $(OUT)
+
diff --git a/c10/ex00/bonjour b/c10/ex00/bonjour
deleted file mode 100644
index 918a5ef..0000000
--- a/c10/ex00/bonjour
+++ /dev/null
@@ -1,4 +0,0 @@
-bonjour
-je
-suis
-charles
diff --git a/c10/ex00/main.c b/c10/ex00/main.c
index 8fee64a..bcf9e68 100644
--- a/c10/ex00/main.c
+++ b/c10/ex00/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/11 18:06:43 by cacharle #+# #+# */
-/* Updated: 2019/07/11 18:45:01 by cacharle ### ########.fr */
+/* Updated: 2019/07/15 13:45:33 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,26 +15,31 @@
#define BUFFER_SIZE 1
#include <stdio.h>
-int write_buffer(char *buf)
+void read_write_file(int fildes)
{
int i;
+ int writing;
+ char buf[BUFFER_SIZE];
- i = 0;
- while (buf[i])
+ writing = 1;
+ while (writing)
{
- write(STDOUT_FILENO, &buf[i], 1);
- /*printf("%d %c\n", buf[i], buf[i]);*/
- if (buf[i] == -1)
- return (1);
- i++;
+ if (read(fildes, buf, BUFFER_SIZE) == 0)
+ break;
+ i = 0;
+ while (buf[i])
+ {
+ write(STDOUT_FILENO, &buf[i], 1);
+ if (buf[i] == -1)
+ writing = 0;
+ i++;
+ }
}
- return (0);
}
int main(int argc, char **argv)
{
int fildes;
- char buf[BUFFER_SIZE];
int reading;
if (argc == 1)
@@ -52,12 +57,7 @@ int main(int argc, char **argv)
write(STDERR_FILENO, "Cannot read file.\n", 18);
return (1);
}
- while (1)
- {
- if (read(fildes, buf, BUFFER_SIZE) == 0)
- break;
- write_buffer(buf);
- }
+ read_write_file(fildes);
close(fildes);
return (0);
}
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);
+}