aboutsummaryrefslogtreecommitdiff
path: root/philo_one/helper.c
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 /philo_one/helper.c
parentf0a36076950bf0c3356ad73382ce3d341cdb0463 (diff)
downloadphilosophers-ac4278405b7a258010219499cccc0dd978201caf.tar.gz
philosophers-ac4278405b7a258010219499cccc0dd978201caf.tar.bz2
philosophers-ac4278405b7a258010219499cccc0dd978201caf.zip
Added argument error reporting
Diffstat (limited to 'philo_one/helper.c')
-rw-r--r--philo_one/helper.c38
1 files changed, 33 insertions, 5 deletions
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;