From ad7ed73c124c8fcda6629350307f0f4f41b87fe3 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Fri, 8 Jan 2021 14:35:53 +0100 Subject: Updated Makefile, restructuring common files --- common/Makefile | 24 +++++++++----- common/args.c | 35 --------------------- common/common.h | 68 ---------------------------------------- common/helper.c | 59 ----------------------------------- common/inc/common.h | 69 ++++++++++++++++++++++++++++++++++++++++ common/io.c | 90 ----------------------------------------------------- common/src/args.c | 35 +++++++++++++++++++++ common/src/helper.c | 59 +++++++++++++++++++++++++++++++++++ common/src/io.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 269 insertions(+), 260 deletions(-) delete mode 100644 common/args.c delete mode 100644 common/common.h delete mode 100644 common/helper.c create mode 100644 common/inc/common.h delete mode 100644 common/io.c create mode 100644 common/src/args.c create mode 100644 common/src/helper.c create mode 100644 common/src/io.c (limited to 'common') diff --git a/common/Makefile b/common/Makefile index d23cac9..e40fb30 100644 --- a/common/Makefile +++ b/common/Makefile @@ -6,27 +6,35 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/02/09 22:39:08 by cacharle #+# #+# # -# Updated: 2021/01/02 10:47:14 by cacharle ### ########.fr # +# Updated: 2021/01/08 14:32:37 by charles ### ########.fr # # # # **************************************************************************** # LIB = ar rcs -RM = rm -rf +RM = rm -f +MKDIR = mkdir -pv + +SRCDIR = src +OBJDIR = obj +INCDIR = inc CC = gcc -CCFLAGS = -std=c99 -O2 -Wall -Wextra -Werror +CCFLAGS = -std=c99 -Wall -Wextra -Werror -O2 -I$(INCDIR) NAME = libphilocommon.a -SRC = $(shell find . -type f -name "*.c") -OBJ = $(SRC:.c=.o) +SRC = $(shell find $(SRCDIR) -type f -name "*.c") +OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o) + +all: prebuild $(NAME) -all: $(NAME) +prebuild: + @$(MKDIR) $(OBJDIR) $(NAME): $(OBJ) $(LIB) $(NAME) $(OBJ) -%.o: %.c +$(OBJDIR)/%.o: $(SRCDIR)/%.c $(CC) $(CCFLAGS) -c -o $@ $< clean: @@ -35,4 +43,4 @@ clean: re: clean all -.PHONY: all clean re +.PHONY: all prebuild clean re diff --git a/common/args.c b/common/args.c deleted file mode 100644 index 2070e46..0000000 --- a/common/args.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* args.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* 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/common/common.h b/common/common.h deleted file mode 100644 index 09fdf79..0000000 --- a/common/common.h +++ /dev/null @@ -1,68 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* common.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */ -/* Updated: 2021/01/04 11:52:30 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef COMMON_H -# define COMMON_H - -# include -# include -# include -# include -# include -# include - -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); -void h_sleep(t_time sleep_time); - -/* -** io.c -*/ - -void philo_put( - size_t id, t_philo_event event, t_time initial_time); -void philo_put_flush(void); -int h_err(int ret, const char *format, char *str); - -#endif diff --git a/common/helper.c b/common/helper.c deleted file mode 100644 index a9d0652..0000000 --- a/common/helper.c +++ /dev/null @@ -1,59 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* helper.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ -/* Updated: 2021/01/03 16:55:42 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); -} - -void h_sleep(t_time sleep_time) -{ - t_time start; - - start = h_time_now(); - while (h_time_now() - start < sleep_time) - usleep(500); -} diff --git a/common/inc/common.h b/common/inc/common.h new file mode 100644 index 0000000..6d36e35 --- /dev/null +++ b/common/inc/common.h @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* common.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */ +/* Updated: 2021/01/04 13:06:52 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef COMMON_H +# define COMMON_H + +# define _XOPEN_SOURCE 500 +# include +# include +# include +# include +# include +# include + +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); +void h_sleep(t_time sleep_time); + +/* +** io.c +*/ + +void philo_put( + size_t id, t_philo_event event, t_time initial_time); +void philo_put_flush(void); +int h_err(int ret, const char *format, char *str); + +#endif diff --git a/common/io.c b/common/io.c deleted file mode 100644 index c01accd..0000000 --- a/common/io.c +++ /dev/null @@ -1,90 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* io.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */ -/* Updated: 2021/01/04 12:10:14 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "common.h" - -static size_t st_strlen(char *s) -{ - size_t 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'; - return (dst + 1); -} - -static char *st_strcpy_end(char *dst, char *str) -{ - while (*str != '\0') - *dst++ = *str++; - return (dst); -} - -#define PHILO_PUT_BUF_SIZE 4048 - -static char g_buf[PHILO_PUT_BUF_SIZE + 256] = {'\0'}; -static char *g_curr = g_buf; - -void philo_put(size_t id, t_philo_event event, t_time initial_time) -{ - - g_curr = st_nbrcpy(g_curr, h_time_now() - initial_time); - g_curr = st_strcpy_end(g_curr, " "); - g_curr = st_nbrcpy(g_curr, id); - if (event == EVENT_FORK) - g_curr = st_strcpy_end(g_curr, " has taken fork\n"); - else if (event == EVENT_EAT) - g_curr = st_strcpy_end(g_curr, " is eating\n"); - else if (event == EVENT_SLEEP) - g_curr = st_strcpy_end(g_curr, " is sleeping\n"); - else if (event == EVENT_THINK) - g_curr = st_strcpy_end(g_curr, " is thinking\n"); - else if (event == EVENT_DIE) - g_curr = st_strcpy_end(g_curr, " died\n"); - if (g_curr - g_buf > PHILO_PUT_BUF_SIZE) - philo_put_flush(); -} - -void philo_put_flush(void) -{ - write(STDOUT_FILENO, g_buf, g_curr - g_buf); - g_curr = g_buf; - g_buf[0] = '\0'; -} - -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/common/src/args.c b/common/src/args.c new file mode 100644 index 0000000..2070e46 --- /dev/null +++ b/common/src/args.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* args.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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/common/src/helper.c b/common/src/helper.c new file mode 100644 index 0000000..a9d0652 --- /dev/null +++ b/common/src/helper.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ +/* Updated: 2021/01/03 16:55:42 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); +} + +void h_sleep(t_time sleep_time) +{ + t_time start; + + start = h_time_now(); + while (h_time_now() - start < sleep_time) + usleep(500); +} diff --git a/common/src/io.c b/common/src/io.c new file mode 100644 index 0000000..c01accd --- /dev/null +++ b/common/src/io.c @@ -0,0 +1,90 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* io.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */ +/* Updated: 2021/01/04 12:10:14 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +static size_t st_strlen(char *s) +{ + size_t 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'; + return (dst + 1); +} + +static char *st_strcpy_end(char *dst, char *str) +{ + while (*str != '\0') + *dst++ = *str++; + return (dst); +} + +#define PHILO_PUT_BUF_SIZE 4048 + +static char g_buf[PHILO_PUT_BUF_SIZE + 256] = {'\0'}; +static char *g_curr = g_buf; + +void philo_put(size_t id, t_philo_event event, t_time initial_time) +{ + + g_curr = st_nbrcpy(g_curr, h_time_now() - initial_time); + g_curr = st_strcpy_end(g_curr, " "); + g_curr = st_nbrcpy(g_curr, id); + if (event == EVENT_FORK) + g_curr = st_strcpy_end(g_curr, " has taken fork\n"); + else if (event == EVENT_EAT) + g_curr = st_strcpy_end(g_curr, " is eating\n"); + else if (event == EVENT_SLEEP) + g_curr = st_strcpy_end(g_curr, " is sleeping\n"); + else if (event == EVENT_THINK) + g_curr = st_strcpy_end(g_curr, " is thinking\n"); + else if (event == EVENT_DIE) + g_curr = st_strcpy_end(g_curr, " died\n"); + if (g_curr - g_buf > PHILO_PUT_BUF_SIZE) + philo_put_flush(); +} + +void philo_put_flush(void) +{ + write(STDOUT_FILENO, g_buf, g_curr - g_buf); + g_curr = g_buf; + g_buf[0] = '\0'; +} + +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); +} -- cgit