From ac4278405b7a258010219499cccc0dd978201caf Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Mon, 28 Sep 2020 15:02:51 +0200 Subject: Added argument error reporting --- philo_one/Makefile | 4 ++-- philo_one/args.c | 32 ++++++++++++++++++++++++++++++++ philo_one/common.c | 32 -------------------------------- philo_one/helper.c | 38 +++++++++++++++++++++++++++++++++----- philo_one/main.c | 4 +++- philo_one/philo_one.h | 4 +++- 6 files changed, 73 insertions(+), 41 deletions(-) create mode 100644 philo_one/args.c delete mode 100644 philo_one/common.c diff --git a/philo_one/Makefile b/philo_one/Makefile index 5fe57d5..de41214 100644 --- a/philo_one/Makefile +++ b/philo_one/Makefile @@ -6,7 +6,7 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2020/09/27 10:35:32 by charles ### ########.fr # +# Updated: 2020/09/28 14:12:28 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -23,7 +23,7 @@ SRC = main.c \ forks.c \ routine.c \ io.c \ - common.c \ + args.c \ helper.c OBJDIR = obj diff --git a/philo_one/args.c b/philo_one/args.c new file mode 100644 index 0000000..9eb6da5 --- /dev/null +++ b/philo_one/args.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* args.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */ +/* Updated: 2020/09/28 14:24:53 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo_one.h" + +bool parse_args(t_philo_conf *philo_args, int argc, char **argv) +{ + if (argc != 5 && argc != 6) + return h_err(false, "Usage: %s philosophers_num death_timeout eat_timeout sleep_timeout", argv[0]); + if ((philo_args->philo_num = h_atou_strict(argv[1])) == -1 + || (philo_args->timeout_death = h_atou_strict(argv[2])) == -1 + || (philo_args->timeout_eat = h_atou_strict(argv[3])) == -1 + || (philo_args->timeout_sleep = h_atou_strict(argv[4])) == -1) + return (false); + if (argc == 6) + { + if ((philo_args->meal_num = h_atou_strict(argv[5])) == -1) + return (false); + } + else + philo_args->meal_num = 1; + return (true); +} diff --git a/philo_one/common.c b/philo_one/common.c deleted file mode 100644 index f67cd48..0000000 --- a/philo_one/common.c +++ /dev/null @@ -1,32 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* common.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */ -/* Updated: 2020/09/27 10:36:31 by charles ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "philo_one.h" - -bool parse_args(t_philo_conf *philo_args, int argc, char **argv) -{ - if (argc != 5 && argc != 6) - return (false); - if ((philo_args->philo_num = h_atou_strict(argv[1])) == -1 - || (philo_args->timeout_death = h_atou_strict(argv[2])) == -1 - || (philo_args->timeout_eat = h_atou_strict(argv[3])) == -1 - || (philo_args->timeout_sleep = h_atou_strict(argv[4])) == -1) - return (false); - if (argc == 6) - { - if ((philo_args->meal_num = h_atou_strict(argv[5])) == -1) - return (false); - } - else - philo_args->meal_num = 1; - return (true); -} diff --git a/philo_one/helper.c b/philo_one/helper.c index 2c3db3d..1caec00 100644 --- a/philo_one/helper.c +++ b/philo_one/helper.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ -/* Updated: 2020/09/27 10:43:04 by charles ### ########.fr */ +/* Updated: 2020/09/28 14:54:17 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,22 +25,24 @@ static int h_strlen(char *s) long int h_atou_strict(char *s) { long int num; + char *origin; + origin = s; if (*s < '0' || *s > '9') - return (-1); + 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 (-1); + return (h_err(-1, "Error: %s: is too big", origin)); num += *s - '0'; if (num > UINT_MAX) - return (-1); + return (h_err(-1, "Error: %s: is too big", origin)); s++; } if (*s != '\0') - return (-1); + return (h_err(-1, "Error: %s: is not a number", origin)); return (num); } @@ -61,6 +63,32 @@ void h_putstr(char *s) write(STDOUT_FILENO, s, h_strlen(s)); } +int h_err(int ret, const char *format, ...) +{ + char *str; + va_list ap; + + va_start(ap, format); + while (*format != '\0') + { + if (format[0] == '%' && format[1] == 's') + { + str = va_arg(ap, char*); + if (str != NULL) + write(STDERR_FILENO, str, h_strlen(str)); + format += 2; + } + else + { + write(STDERR_FILENO, format, 1); + format++; + } + } + va_end(ap); + write(STDERR_FILENO, "\n", 1); + return (ret); +} + t_time h_time_now(void) { struct timeval tv; diff --git a/philo_one/main.c b/philo_one/main.c index b43c0fe..83b5564 100644 --- a/philo_one/main.c +++ b/philo_one/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */ -/* Updated: 2020/09/27 10:36:40 by charles ### ########.fr */ +/* Updated: 2020/09/28 14:59:59 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,8 @@ int main(int argc, char **argv) if (!parse_args(&philo_args, argc, argv)) return (1); + if (philo_args.philo_num < 2) + return (h_err(1, "Error: there should be at least 2 philosophers")); if ((forks = forks_new(philo_args.philo_num)) == NULL) return (1); if ((philos = philos_new(philo_args.philo_num)) == NULL) diff --git a/philo_one/philo_one.h b/philo_one/philo_one.h index 331db2f..0d6844c 100644 --- a/philo_one/philo_one.h +++ b/philo_one/philo_one.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */ -/* Updated: 2020/09/27 10:42:58 by charles ### ########.fr */ +/* Updated: 2020/09/28 14:22:57 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ # include # include # include +# include typedef long int t_time; @@ -116,6 +117,7 @@ long int h_atou_strict(char *s); void h_putnbr(unsigned long num); void h_putchar(char c); void h_putstr(char *s); +int h_err(int ret, const char *format, ...); t_time h_time_now(void); #endif -- cgit