diff options
Diffstat (limited to 'common')
| -rw-r--r-- | common/Makefile | 38 | ||||
| -rw-r--r-- | common/args.c | 35 | ||||
| -rw-r--r-- | common/common.h | 67 | ||||
| -rw-r--r-- | common/helper.c | 50 | ||||
| -rw-r--r-- | common/io.c | 82 |
5 files changed, 0 insertions, 272 deletions
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/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 <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/common/common.h b/common/common.h deleted file mode 100644 index 617db1a..0000000 --- a/common/common.h +++ /dev/null @@ -1,67 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* 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/common/helper.c b/common/helper.c deleted file mode 100644 index e1cc1e2..0000000 --- a/common/helper.c +++ /dev/null @@ -1,50 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* 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/common/io.c b/common/io.c deleted file mode 100644 index 5d50ccc..0000000 --- a/common/io.c +++ /dev/null @@ -1,82 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* 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); -} |
