aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-01-03 14:42:46 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-01-03 14:47:11 +0100
commitcc2adbf09541c0d9309d66f719c86e7fe5d351d7 (patch)
treef9d61b11e57dcdfebc9092554df1a64d0d4958c5
parente874ad94a31a8259b8eb7ad2865767c081bcd279 (diff)
downloadphilosophers-cc2adbf09541c0d9309d66f719c86e7fe5d351d7.tar.gz
philosophers-cc2adbf09541c0d9309d66f719c86e7fe5d351d7.tar.bz2
philosophers-cc2adbf09541c0d9309d66f719c86e7fe5d351d7.zip
Updated philo directories for correctionrendu
-rw-r--r--.gitignore9
-rw-r--r--Makefile33
-rw-r--r--README.md3
-rw-r--r--common/Makefile38
-rw-r--r--correction.pngbin180674 -> 0 bytes
-rw-r--r--philo_one/Makefile27
-rw-r--r--philo_one/args.c (renamed from common/args.c)0
-rw-r--r--philo_one/common.h (renamed from common/common.h)0
-rw-r--r--philo_one/helper.c (renamed from common/helper.c)0
-rw-r--r--philo_one/io.c (renamed from common/io.c)0
-rw-r--r--philo_three/Makefile30
-rw-r--r--philo_three/args.c35
-rw-r--r--philo_three/common.h67
-rw-r--r--philo_three/helper.c50
-rw-r--r--philo_three/io.c82
-rw-r--r--philo_two/Makefile30
-rw-r--r--philo_two/args.c35
-rw-r--r--philo_two/common.h67
-rw-r--r--philo_two/helper.c50
-rw-r--r--philo_two/io.c82
-rw-r--r--subject.pdfbin1271153 -> 0 bytes
21 files changed, 502 insertions, 136 deletions
diff --git a/.gitignore b/.gitignore
deleted file mode 100644
index 56a2394..0000000
--- a/.gitignore
+++ /dev/null
@@ -1,9 +0,0 @@
-*.o
-*.ghc
-*.a
-*.dSYM
-a.out
-philo_one/philo_one
-philo_two/philo_two
-philo_three/philo_three
-tags
diff --git a/Makefile b/Makefile
deleted file mode 100644
index 34f114d..0000000
--- a/Makefile
+++ /dev/null
@@ -1,33 +0,0 @@
-# **************************************************************************** #
-# #
-# ::: :::::::: #
-# Makefile :+: :+: :+: #
-# +:+ +:+ +:+ #
-# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
-# +#+#+#+#+#+ +#+ #
-# Created: 2020/02/09 03:31:28 by cacharle #+# #+# #
-# Updated: 2020/12/30 13:48:29 by charles ### ########.fr #
-# #
-# **************************************************************************** #
-
-MAKE = make --no-print-directory
-
-PHILO_ONE_DIR = philo_one
-PHILO_TWO_DIR = philo_two
-PHILO_THREE_DIR = philo_three
-
-help:
- @echo "make philo_one - compile philo_one"
- @echo "make philo_two - compile philo_two"
- @echo "make philo_three - compile philo_three"
-
-philo_one:
- $(MAKE) -C $(PHILO_ONE_DIR)
-
-philo_two:
- $(MAKE) -C $(PHILO_TWO_DIR)
-
-philo_three:
- $(MAKE) -C $(PHILO_THREE_DIR)
-
-.PHONY: philo_one philo_two philo_three help
diff --git a/README.md b/README.md
deleted file mode 100644
index 7b9d21d..0000000
--- a/README.md
+++ /dev/null
@@ -1,3 +0,0 @@
-# philosophers
-
-philosophers project of school 42
diff --git a/common/Makefile b/common/Makefile
deleted file mode 100644
index d23cac9..0000000
--- a/common/Makefile
+++ /dev/null
@@ -1,38 +0,0 @@
-# **************************************************************************** #
-# #
-# ::: :::::::: #
-# Makefile :+: :+: :+: #
-# +:+ +:+ +:+ #
-# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
-# +#+#+#+#+#+ +#+ #
-# Created: 2020/02/09 22:39:08 by cacharle #+# #+# #
-# Updated: 2021/01/02 10:47:14 by cacharle ### ########.fr #
-# #
-# **************************************************************************** #
-
-LIB = ar rcs
-RM = rm -rf
-
-CC = gcc
-CCFLAGS = -std=c99 -O2 -Wall -Wextra -Werror
-
-NAME = libphilocommon.a
-
-SRC = $(shell find . -type f -name "*.c")
-OBJ = $(SRC:.c=.o)
-
-all: $(NAME)
-
-$(NAME): $(OBJ)
- $(LIB) $(NAME) $(OBJ)
-
-%.o: %.c
- $(CC) $(CCFLAGS) -c -o $@ $<
-
-clean:
- $(RM) $(OBJ)
- $(RM) $(NAME)
-
-re: clean all
-
-.PHONY: all clean re
diff --git a/correction.png b/correction.png
deleted file mode 100644
index 3b1558e..0000000
--- a/correction.png
+++ /dev/null
Binary files differ
diff --git a/philo_one/Makefile b/philo_one/Makefile
index 0026516..44638f5 100644
--- a/philo_one/Makefile
+++ b/philo_one/Makefile
@@ -6,39 +6,38 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/11/24 05:50:15 by cacharle #+# #+# #
-# Updated: 2021/01/01 15:18:11 by charles ### ########.fr #
+# Updated: 2021/01/03 14:38:19 by cacharle ### ########.fr #
# #
# **************************************************************************** #
RM = rm -f
MAKE = make --no-print-directory
-COMMONDIR = ../common
-
CC = gcc
-CCFLAGS = -I$(COMMONDIR) -O2 -std=c99 -Wall -Wextra -Werror
-LDFLAGS = -lpthread -L$(COMMONDIR) -lphilocommon
+CCFLAGS = -I. -O2 -std=c99 -Wall -Wextra -Werror
+LDFLAGS = -lpthread
NAME = philo_one
SRCDIR = src
OBJDIR = obj
-SRC = $(shell find $(SRCDIR) -type f -name '*.c')
-OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
+SRC = args.c helper.c io.c \
+ src/event.c src/forks.c src/main.c src/philo.c src/routine.c
+OBJ = $(SRC:%.c=%.o)
all: prebuild $(NAME)
prebuild:
@mkdir -pv $(OBJDIR)
-$(NAME): common_all $(OBJ)
+$(NAME): $(OBJ)
$(CC) -o $@ $(OBJ) $(LDFLAGS)
-$(OBJDIR)/%.o: $(SRCDIR)/%.c
+%.o: %.c
$(CC) $(CCFLAGS) -c -o $@ $<
-clean: common_clean
+clean:
$(RM) $(OBJ)
fclean: clean
@@ -46,10 +45,4 @@ fclean: clean
re: fclean all
-common_all:
- $(MAKE) -C $(COMMONDIR) all
-
-common_clean:
- $(MAKE) -C $(COMMONDIR) clean
-
-.PHONY: all prebuild clean fclean re common_all common_clean
+.PHONY: all prebuild clean fclean re
diff --git a/common/args.c b/philo_one/args.c
index 2070e46..2070e46 100644
--- a/common/args.c
+++ b/philo_one/args.c
diff --git a/common/common.h b/philo_one/common.h
index 617db1a..617db1a 100644
--- a/common/common.h
+++ b/philo_one/common.h
diff --git a/common/helper.c b/philo_one/helper.c
index e1cc1e2..e1cc1e2 100644
--- a/common/helper.c
+++ b/philo_one/helper.c
diff --git a/common/io.c b/philo_one/io.c
index 5d50ccc..5d50ccc 100644
--- a/common/io.c
+++ b/philo_one/io.c
diff --git a/philo_three/Makefile b/philo_three/Makefile
index 17c5b46..050e2d5 100644
--- a/philo_three/Makefile
+++ b/philo_three/Makefile
@@ -6,39 +6,39 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/11/24 05:50:15 by cacharle #+# #+# #
-# Updated: 2021/01/01 16:28:33 by charles ### ########.fr #
+# Updated: 2021/01/03 14:40:14 by cacharle ### ########.fr #
# #
# **************************************************************************** #
RM = rm -f
MAKE = make --no-print-directory
-COMMON_DIR = ../common
-
CC = gcc
-CCFLAGS = -I$(COMMON_DIR) -O2 -std=c99 -Wall -Wextra #-Werror
-LDFLAGS = -lpthread -L$(COMMON_DIR) -lphilocommon
+CCFLAGS = -I. -O2 -std=c99 -Wall -Wextra -Werror
+LDFLAGS = -lpthread
NAME = philo_three
SRCDIR = src
OBJDIR = obj
-SRC = $(shell find $(SRCDIR) -type f -name '*.c')
-OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
+SRC = args.c helper.c io.c \
+ src/child.c src/event.c src/main.c
+
+OBJ = $(SRC:%.c=%.o)
all: prebuild $(NAME)
prebuild:
- @mkdir -p $(OBJDIR)
+ @mkdir -pv $(OBJDIR)
-$(NAME): common_all $(OBJ)
+$(NAME): $(OBJ)
$(CC) -o $@ $(OBJ) $(LDFLAGS)
-$(OBJDIR)/%.o: $(SRCDIR)/%.c
+%.o: %.c
$(CC) $(CCFLAGS) -c -o $@ $<
-clean: common_clean
+clean:
$(RM) $(OBJ)
fclean: clean
@@ -46,10 +46,4 @@ fclean: clean
re: fclean all
-common_all:
- $(MAKE) -C $(COMMON_DIR) all
-
-common_clean:
- $(MAKE) -C $(COMMON_DIR) clean
-
-.PHONY: all prebuild clean fclean re common_all common_clean
+.PHONY: all prebuild clean fclean re
diff --git a/philo_three/args.c b/philo_three/args.c
new file mode 100644
index 0000000..2070e46
--- /dev/null
+++ b/philo_three/args.c
@@ -0,0 +1,35 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* args.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */
+/* Updated: 2020/12/30 13:44:54 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "common.h"
+
+bool parse_args(t_philo_args *args, int argc, char **argv)
+{
+ if (argc != 5 && argc != 6)
+ {
+ return (h_err(false, "Usage: %s philosophers_num death_timeout"
+ "eat_timeout sleep_timeout [meal_num]", argv[0]));
+ }
+ if ((args->philo_num = h_atou_strict(argv[1])) == -1
+ || (args->timeout_death = h_atou_strict(argv[2])) == -1
+ || (args->timeout_eat = h_atou_strict(argv[3])) == -1
+ || (args->timeout_sleep = h_atou_strict(argv[4])) == -1)
+ return (false);
+ if (argc == 6)
+ {
+ if ((args->meal_num = h_atou_strict(argv[5])) == -1)
+ return (false);
+ }
+ else
+ args->meal_num = -1;
+ return (true);
+}
diff --git a/philo_three/common.h b/philo_three/common.h
new file mode 100644
index 0000000..617db1a
--- /dev/null
+++ b/philo_three/common.h
@@ -0,0 +1,67 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* common.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */
+/* Updated: 2021/01/03 14:02:38 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef COMMON_H
+# define COMMON_H
+
+# include <unistd.h>
+# include <stdlib.h>
+# include <stdbool.h>
+# include <pthread.h>
+# include <sys/time.h>
+# include <limits.h>
+
+typedef long int t_time;
+
+typedef enum e_philo_event
+{
+ EVENT_FORK,
+ EVENT_EAT,
+ EVENT_SLEEP,
+ EVENT_THINK,
+ EVENT_DIE
+} t_philo_event;
+
+typedef struct
+{
+ long int philo_num;
+ t_time timeout_death;
+ t_time timeout_eat;
+ t_time timeout_sleep;
+ long int meal_num;
+} t_philo_args;
+
+typedef void *(*t_routine)(void *arg);
+
+/*
+** args.c
+*/
+
+bool parse_args(t_philo_args *args, int argc, char **argv);
+
+/*
+** helper.c
+*/
+
+long int h_atou_strict(char *s);
+t_time h_time_now(void);
+int h_err(int ret, const char *format, char *str);
+
+/*
+** io.c
+*/
+
+void philo_put_set_initial_time(void);
+void philo_put(
+ size_t id, t_philo_event event, t_time initial_time);
+
+#endif
diff --git a/philo_three/helper.c b/philo_three/helper.c
new file mode 100644
index 0000000..e1cc1e2
--- /dev/null
+++ b/philo_three/helper.c
@@ -0,0 +1,50 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* helper.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */
+/* Updated: 2021/01/02 12:07:49 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "common.h"
+
+long int h_atou_strict(char *s)
+{
+ long int num;
+ char *origin;
+
+ origin = s;
+ if (*s < '0' || *s > '9')
+ return (h_err(-1, "Error: %s: is not a number", origin));
+ num = 0;
+ while (*s >= '0' && *s <= '9')
+ {
+ num *= 10;
+ if (num > UINT_MAX)
+ return (h_err(-1, "Error: %s: is too big", origin));
+ num += *s - '0';
+ if (num > UINT_MAX)
+ return (h_err(-1, "Error: %s: is too big", origin));
+ s++;
+ }
+ if (*s != '\0')
+ return (h_err(-1, "Error: %s: is not a number", origin));
+ return (num);
+}
+
+/*
+** No need to check error of gettimeofday
+** (only for settimeofday and passed pointer validity)
+*/
+
+t_time h_time_now(void)
+{
+ struct timeval tv;
+
+ gettimeofday(&tv, NULL);
+ return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
+}
diff --git a/philo_three/io.c b/philo_three/io.c
new file mode 100644
index 0000000..5d50ccc
--- /dev/null
+++ b/philo_three/io.c
@@ -0,0 +1,82 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* io.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */
+/* Updated: 2021/01/03 14:02:21 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "common.h"
+
+static size_t st_strlen(char *s)
+{
+ int counter;
+
+ counter = 0;
+ while (s[counter])
+ counter++;
+ return (counter);
+}
+
+static char *st_nbrcpy(char *dst, long long int num)
+{
+ if (num > 9)
+ dst = st_nbrcpy(dst, num / 10);
+ dst[0] = num % 10 + '0';
+ dst[1] = '\0';
+ return (dst + 1);
+}
+
+static void st_strcat(char *dst, char *str)
+{
+ while (*dst != '\0')
+ dst++;
+ while (*str != '\0')
+ *dst++ = *str++;
+ *dst = '\0';
+}
+
+void philo_put(size_t id, t_philo_event event, t_time initial_time)
+{
+ static char buf[2048];
+
+ buf[0] = '\0';
+ st_nbrcpy(buf, h_time_now() - initial_time);
+ st_strcat(buf, " ");
+ st_nbrcpy(buf + st_strlen(buf), id);
+ if (event == EVENT_FORK)
+ st_strcat(buf, " has taken fork\n");
+ else if (event == EVENT_EAT)
+ st_strcat(buf, " is eating\n");
+ else if (event == EVENT_SLEEP)
+ st_strcat(buf, " is sleeping\n");
+ else if (event == EVENT_THINK)
+ st_strcat(buf, " is thinking\n");
+ else if (event == EVENT_DIE)
+ st_strcat(buf, " died\n");
+ write(STDOUT_FILENO, buf, st_strlen(buf));
+}
+
+int h_err(int ret, const char *format, char *str)
+{
+ while (*format != '\0')
+ {
+ if (format[0] == '%' && format[1] == 's')
+ {
+ if (str != NULL)
+ write(STDERR_FILENO, str, st_strlen(str));
+ format += 2;
+ }
+ else
+ {
+ write(STDERR_FILENO, format, 1);
+ format++;
+ }
+ }
+ write(STDERR_FILENO, "\n", 1);
+ return (ret);
+}
diff --git a/philo_two/Makefile b/philo_two/Makefile
index 88c0f8b..224809b 100644
--- a/philo_two/Makefile
+++ b/philo_two/Makefile
@@ -6,39 +6,39 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/11/24 05:50:15 by cacharle #+# #+# #
-# Updated: 2021/01/02 11:27:33 by cacharle ### ########.fr #
+# Updated: 2021/01/03 14:39:09 by cacharle ### ########.fr #
# #
# **************************************************************************** #
RM = rm -f
MAKE = make --no-print-directory
-COMMONDIR = ../common
-
CC = gcc
-CCFLAGS = -g -I$(COMMONDIR) -O2 -std=c99 -Wall -Wextra #-Werror
-LDFLAGS = -lpthread -L$(COMMONDIR) -lphilocommon
+CCFLAGS = -I. -O2 -std=c99 -Wall -Wextra -Werror
+LDFLAGS = -lpthread
NAME = philo_two
SRCDIR = src
OBJDIR = obj
-SRC = $(shell find $(SRCDIR) -type f -name '*.c')
-OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
+SRC = args.c helper.c io.c \
+ src/event.c src/main.c src/routine.c
+
+OBJ = $(SRC:%.c=%.o)
all: prebuild $(NAME)
prebuild:
- @mkdir -p $(OBJDIR)
+ @mkdir -pv $(OBJDIR)
-$(NAME): common_all $(OBJ)
+$(NAME): $(OBJ)
$(CC) -o $@ $(OBJ) $(LDFLAGS)
-$(OBJDIR)/%.o: $(SRCDIR)/%.c
+%.o: %.c
$(CC) $(CCFLAGS) -c -o $@ $<
-clean: common_clean
+clean:
$(RM) $(OBJ)
fclean: clean
@@ -46,10 +46,4 @@ fclean: clean
re: fclean all
-common_all:
- $(MAKE) -C $(COMMONDIR) all
-
-common_clean:
- $(MAKE) -C $(COMMONDIR) clean
-
-.PHONY: all prebuild clean fclean re common_all common_clean
+.PHONY: all prebuild clean fclean re
diff --git a/philo_two/args.c b/philo_two/args.c
new file mode 100644
index 0000000..2070e46
--- /dev/null
+++ b/philo_two/args.c
@@ -0,0 +1,35 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* args.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */
+/* Updated: 2020/12/30 13:44:54 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "common.h"
+
+bool parse_args(t_philo_args *args, int argc, char **argv)
+{
+ if (argc != 5 && argc != 6)
+ {
+ return (h_err(false, "Usage: %s philosophers_num death_timeout"
+ "eat_timeout sleep_timeout [meal_num]", argv[0]));
+ }
+ if ((args->philo_num = h_atou_strict(argv[1])) == -1
+ || (args->timeout_death = h_atou_strict(argv[2])) == -1
+ || (args->timeout_eat = h_atou_strict(argv[3])) == -1
+ || (args->timeout_sleep = h_atou_strict(argv[4])) == -1)
+ return (false);
+ if (argc == 6)
+ {
+ if ((args->meal_num = h_atou_strict(argv[5])) == -1)
+ return (false);
+ }
+ else
+ args->meal_num = -1;
+ return (true);
+}
diff --git a/philo_two/common.h b/philo_two/common.h
new file mode 100644
index 0000000..617db1a
--- /dev/null
+++ b/philo_two/common.h
@@ -0,0 +1,67 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* common.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */
+/* Updated: 2021/01/03 14:02:38 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef COMMON_H
+# define COMMON_H
+
+# include <unistd.h>
+# include <stdlib.h>
+# include <stdbool.h>
+# include <pthread.h>
+# include <sys/time.h>
+# include <limits.h>
+
+typedef long int t_time;
+
+typedef enum e_philo_event
+{
+ EVENT_FORK,
+ EVENT_EAT,
+ EVENT_SLEEP,
+ EVENT_THINK,
+ EVENT_DIE
+} t_philo_event;
+
+typedef struct
+{
+ long int philo_num;
+ t_time timeout_death;
+ t_time timeout_eat;
+ t_time timeout_sleep;
+ long int meal_num;
+} t_philo_args;
+
+typedef void *(*t_routine)(void *arg);
+
+/*
+** args.c
+*/
+
+bool parse_args(t_philo_args *args, int argc, char **argv);
+
+/*
+** helper.c
+*/
+
+long int h_atou_strict(char *s);
+t_time h_time_now(void);
+int h_err(int ret, const char *format, char *str);
+
+/*
+** io.c
+*/
+
+void philo_put_set_initial_time(void);
+void philo_put(
+ size_t id, t_philo_event event, t_time initial_time);
+
+#endif
diff --git a/philo_two/helper.c b/philo_two/helper.c
new file mode 100644
index 0000000..e1cc1e2
--- /dev/null
+++ b/philo_two/helper.c
@@ -0,0 +1,50 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* helper.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */
+/* Updated: 2021/01/02 12:07:49 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "common.h"
+
+long int h_atou_strict(char *s)
+{
+ long int num;
+ char *origin;
+
+ origin = s;
+ if (*s < '0' || *s > '9')
+ return (h_err(-1, "Error: %s: is not a number", origin));
+ num = 0;
+ while (*s >= '0' && *s <= '9')
+ {
+ num *= 10;
+ if (num > UINT_MAX)
+ return (h_err(-1, "Error: %s: is too big", origin));
+ num += *s - '0';
+ if (num > UINT_MAX)
+ return (h_err(-1, "Error: %s: is too big", origin));
+ s++;
+ }
+ if (*s != '\0')
+ return (h_err(-1, "Error: %s: is not a number", origin));
+ return (num);
+}
+
+/*
+** No need to check error of gettimeofday
+** (only for settimeofday and passed pointer validity)
+*/
+
+t_time h_time_now(void)
+{
+ struct timeval tv;
+
+ gettimeofday(&tv, NULL);
+ return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
+}
diff --git a/philo_two/io.c b/philo_two/io.c
new file mode 100644
index 0000000..5d50ccc
--- /dev/null
+++ b/philo_two/io.c
@@ -0,0 +1,82 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* io.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */
+/* Updated: 2021/01/03 14:02:21 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "common.h"
+
+static size_t st_strlen(char *s)
+{
+ int counter;
+
+ counter = 0;
+ while (s[counter])
+ counter++;
+ return (counter);
+}
+
+static char *st_nbrcpy(char *dst, long long int num)
+{
+ if (num > 9)
+ dst = st_nbrcpy(dst, num / 10);
+ dst[0] = num % 10 + '0';
+ dst[1] = '\0';
+ return (dst + 1);
+}
+
+static void st_strcat(char *dst, char *str)
+{
+ while (*dst != '\0')
+ dst++;
+ while (*str != '\0')
+ *dst++ = *str++;
+ *dst = '\0';
+}
+
+void philo_put(size_t id, t_philo_event event, t_time initial_time)
+{
+ static char buf[2048];
+
+ buf[0] = '\0';
+ st_nbrcpy(buf, h_time_now() - initial_time);
+ st_strcat(buf, " ");
+ st_nbrcpy(buf + st_strlen(buf), id);
+ if (event == EVENT_FORK)
+ st_strcat(buf, " has taken fork\n");
+ else if (event == EVENT_EAT)
+ st_strcat(buf, " is eating\n");
+ else if (event == EVENT_SLEEP)
+ st_strcat(buf, " is sleeping\n");
+ else if (event == EVENT_THINK)
+ st_strcat(buf, " is thinking\n");
+ else if (event == EVENT_DIE)
+ st_strcat(buf, " died\n");
+ write(STDOUT_FILENO, buf, st_strlen(buf));
+}
+
+int h_err(int ret, const char *format, char *str)
+{
+ while (*format != '\0')
+ {
+ if (format[0] == '%' && format[1] == 's')
+ {
+ if (str != NULL)
+ write(STDERR_FILENO, str, st_strlen(str));
+ format += 2;
+ }
+ else
+ {
+ write(STDERR_FILENO, format, 1);
+ format++;
+ }
+ }
+ write(STDERR_FILENO, "\n", 1);
+ return (ret);
+}
diff --git a/subject.pdf b/subject.pdf
deleted file mode 100644
index d80790b..0000000
--- a/subject.pdf
+++ /dev/null
Binary files differ