aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-09-28 15:02:51 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-09-28 15:02:51 +0200
commitac4278405b7a258010219499cccc0dd978201caf (patch)
tree501b23b1874b6fa264c9b612d0fe5b5ebc9cd26a
parentf0a36076950bf0c3356ad73382ce3d341cdb0463 (diff)
downloadphilosophers-ac4278405b7a258010219499cccc0dd978201caf.tar.gz
philosophers-ac4278405b7a258010219499cccc0dd978201caf.tar.bz2
philosophers-ac4278405b7a258010219499cccc0dd978201caf.zip
Added argument error reporting
-rw-r--r--philo_one/Makefile4
-rw-r--r--philo_one/args.c (renamed from philo_one/common.c)6
-rw-r--r--philo_one/helper.c38
-rw-r--r--philo_one/main.c4
-rw-r--r--philo_one/philo_one.h4
5 files changed, 44 insertions, 12 deletions
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 <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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/common.c b/philo_one/args.c
index f67cd48..9eb6da5 100644
--- a/philo_one/common.c
+++ b/philo_one/args.c
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* common.c :+: :+: :+: */
+/* args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */
-/* Updated: 2020/09/27 10:36:31 by charles ### ########.fr */
+/* Updated: 2020/09/28 14:24:53 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,7 +15,7 @@
bool parse_args(t_philo_conf *philo_args, int argc, char **argv)
{
if (argc != 5 && argc != 6)
- return (false);
+ 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
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 <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <pthread.h>
# include <stdbool.h>
# include <limits.h>
+# include <stdarg.h>
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