Solution to Ex. 00, 01.
authorLukas Jiriste <ljiriste@student.42prague.com>
Wed, 28 Jun 2023 19:16:50 +0000 (21:16 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Wed, 28 Jun 2023 19:16:50 +0000 (21:16 +0200)
ex00/Makefile [new file with mode: 0644]
ex00/sources/main.c [new file with mode: 0644]
ex01/Makefile [new file with mode: 0644]
ex01/ft_cat [new file with mode: 0755]
ex01/sources/main.c [new file with mode: 0644]
ex01/sources/main.o [new file with mode: 0644]
ex01/tests/subtests/subtest1 [new file with mode: 0644]
ex01/tests/test1 [new file with mode: 0644]
ex01/tests/test2 [new file with mode: 0644]

diff --git a/ex00/Makefile b/ex00/Makefile
new file mode 100644 (file)
index 0000000..73d5d98
--- /dev/null
@@ -0,0 +1,27 @@
+CC = cc
+CFLAGS = -Wall -Wextra -Werror
+
+RM = rm -f
+
+SRCDIR = ./sources
+SOURCES = $(shell find $(SRCDIR) -name "*.c")
+OBJECTS = $(SOURCES:.c=.o)
+
+NAME = ft_display_file
+
+all : $(NAME)
+
+$(NAME) : $(OBJECTS)
+       $(CC) $(CFLAGS) -o $(NAME) $(OBJECTS)
+
+%.o : %.c
+       $(CC) $(CFLAGS) -c -o $@ $<
+
+clean :
+       $(RM) $(OBJECTS)
+
+fclean : clean 
+       $(RM) $(NAME)
+
+re : fclean
+       $(MAKE) all
diff --git a/ex00/sources/main.c b/ex00/sources/main.c
new file mode 100644 (file)
index 0000000..d26afb4
--- /dev/null
@@ -0,0 +1,45 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   main.c                                             :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/06/27 10:39:21 by ljiriste          #+#    #+#             */
+/*   Updated: 2023/06/27 10:47:07 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <unistd.h>
+#include <fcntl.h>
+
+void   print_error(char *str)
+{
+       while (*str)
+               write(2, str++, 1);
+       return ;
+}
+
+int    main(int argc, char **argv)
+{
+       int             fd;
+       char    c;
+
+       if (argc == 1)
+               print_error("File name missing.\n");
+       else if (argc > 2)
+               print_error("Too many arguments.\n");
+       else
+       {
+               fd = open(argv[1], O_RDONLY);
+               if (fd < 0)
+                       print_error("Cannot read file.\n");
+               else
+               {
+                       while (read(fd, &c, 1) > 0)
+                               write(1, &c, 1);
+                       close(fd);
+               }
+       }
+       return (0);
+}
diff --git a/ex01/Makefile b/ex01/Makefile
new file mode 100644 (file)
index 0000000..0f83ffb
--- /dev/null
@@ -0,0 +1,27 @@
+CC = cc
+CFLAGS = -Wall -Wextra -Werror -g
+
+RM = rm -f
+
+SRCDIR = ./sources
+SOURCES = $(shell find $(SRCDIR) -name "*.c")
+OBJECTS = $(SOURCES:.c=.o)
+
+NAME = ft_cat
+
+all : $(NAME)
+
+$(NAME) : $(OBJECTS)
+       $(CC) $(CFLAGS) -o $(NAME) $(OBJECTS)
+
+%.o : %.c
+       $(CC) $(CFLAGS) -c -o $@ $<
+
+clean :
+       $(RM) $(OBJECTS)
+
+fclean : clean 
+       $(RM) $(NAME)
+
+re : fclean
+       $(MAKE) all
diff --git a/ex01/ft_cat b/ex01/ft_cat
new file mode 100755 (executable)
index 0000000..ee85a51
Binary files /dev/null and b/ex01/ft_cat differ
diff --git a/ex01/sources/main.c b/ex01/sources/main.c
new file mode 100644 (file)
index 0000000..ac02628
--- /dev/null
@@ -0,0 +1,76 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   main.c                                             :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/06/27 11:03:04 by ljiriste          #+#    #+#             */
+/*   Updated: 2023/06/28 16:33:20 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <errno.h>
+
+void   print_error(char *str)
+{
+       while (*str)
+               write(2, str++, 1);
+       return ;
+}
+
+void   std_input_mode(void)
+{
+       char    c;
+
+       while (read(0, &c, 1) > 0)
+               write(1, &c, 1);
+       return ;
+}
+
+void   write_file(char *file_name, char *proc_name)
+{
+       int             fd;
+       char    c;
+
+       fd = open(file_name, O_RDWR);
+       if (fd < 0)
+       {
+               print_error(proc_name);
+               print_error(": ");
+               print_error(file_name);
+               print_error(": ");
+               print_error(strerror(errno));
+               print_error("\n");
+       }
+       else
+       {
+               while (read(fd, &c, 1) > 0)
+                       write(1, &c, 1);
+               close(fd);
+       }
+}
+
+int    main(int argc, char **argv)
+{
+       int     i;
+
+       if (argc == 1)
+               std_input_mode();
+       else
+       {
+               i = 1;
+               while (i < argc)
+               {
+                       if (*argv[i] == '-')
+                               std_input_mode();
+                       else
+                               write_file(argv[i], argv[0]);
+                       ++i;
+               }
+       }
+       return (0);
+}
diff --git a/ex01/sources/main.o b/ex01/sources/main.o
new file mode 100644 (file)
index 0000000..808b41d
Binary files /dev/null and b/ex01/sources/main.o differ
diff --git a/ex01/tests/subtests/subtest1 b/ex01/tests/subtests/subtest1
new file mode 100644 (file)
index 0000000..b2c9962
--- /dev/null
@@ -0,0 +1 @@
+I am a subtest
diff --git a/ex01/tests/test1 b/ex01/tests/test1
new file mode 100644 (file)
index 0000000..4f47db7
--- /dev/null
@@ -0,0 +1,4 @@
+thjarjn
+atebna
+aioetunboathba
+aiarg
diff --git a/ex01/tests/test2 b/ex01/tests/test2
new file mode 100644 (file)
index 0000000..802992c
--- /dev/null
@@ -0,0 +1 @@
+Hello world