diff options
Diffstat (limited to 'philo_one/helper.c')
| -rw-r--r-- | philo_one/helper.c | 38 |
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; |
