diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2021-01-08 14:35:53 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2021-01-08 14:35:53 +0100 |
| commit | ad7ed73c124c8fcda6629350307f0f4f41b87fe3 (patch) | |
| tree | 5faf9809401b64fecffe709f20cc9bb980aa7406 /common/src/helper.c | |
| parent | 907debbb7d1e7ccc4914805cfe4acbed92b82bcc (diff) | |
| download | philosophers-ad7ed73c124c8fcda6629350307f0f4f41b87fe3.tar.gz philosophers-ad7ed73c124c8fcda6629350307f0f4f41b87fe3.tar.bz2 philosophers-ad7ed73c124c8fcda6629350307f0f4f41b87fe3.zip | |
Updated Makefile, restructuring common files
Diffstat (limited to 'common/src/helper.c')
| -rw-r--r-- | common/src/helper.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/common/src/helper.c b/common/src/helper.c new file mode 100644 index 0000000..a9d0652 --- /dev/null +++ b/common/src/helper.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ +/* Updated: 2021/01/03 16:55:42 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); +} + +void h_sleep(t_time sleep_time) +{ + t_time start; + + start = h_time_now(); + while (h_time_now() - start < sleep_time) + usleep(500); +} |
