From 416f042fc0e9f34655e3930f4baf7d6d0dbe4a08 Mon Sep 17 00:00:00 2001 From: Rafael Ravedutti Date: Wed, 30 Nov 2022 17:43:35 +0100 Subject: [PATCH] Fix readline() when fgets returns NULL even on success Signed-off-by: Rafael Ravedutti --- common/util.c | 59 +++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/common/util.c b/common/util.c index 0641dc7..b0f0c76 100644 --- a/common/util.c +++ b/common/util.c @@ -4,6 +4,7 @@ * Use of this source code is governed by a LGPL-3.0 * license that can be found in the LICENSE file. */ +#include #include #include #include @@ -28,39 +29,39 @@ double myrandom(int* seed) { } void random_reset(int *seed, int ibase, double *coord) { - int i; - char *str = (char *) &ibase; - int n = sizeof(int); - unsigned int hash = 0; + int i; + char *str = (char *) &ibase; + int n = sizeof(int); + unsigned int hash = 0; - for (i = 0; i < n; i++) { - hash += str[i]; - hash += (hash << 10); - hash ^= (hash >> 6); - } + for (i = 0; i < n; i++) { + hash += str[i]; + hash += (hash << 10); + hash ^= (hash >> 6); + } - str = (char *) coord; - n = 3 * sizeof(double); - for (i = 0; i < n; i++) { - hash += str[i]; - hash += (hash << 10); - hash ^= (hash >> 6); - } + str = (char *) coord; + n = 3 * sizeof(double); + for (i = 0; i < n; i++) { + hash += str[i]; + hash += (hash << 10); + hash ^= (hash >> 6); + } - hash += (hash << 3); - hash ^= (hash >> 11); - hash += (hash << 15); + hash += (hash << 3); + hash ^= (hash >> 11); + hash += (hash << 15); - // keep 31 bits of unsigned int as new seed - // do not allow seed = 0, since will cause hang in gaussian() + // keep 31 bits of unsigned int as new seed + // do not allow seed = 0, since will cause hang in gaussian() - *seed = hash & 0x7ffffff; - if (!(*seed)) *seed = 1; + *seed = hash & 0x7ffffff; + if (!(*seed)) *seed = 1; - // warm up the RNG + // warm up the RNG - for (i = 0; i < 5; i++) myrandom(seed); - //save = 0; + for (i = 0; i < 5; i++) myrandom(seed); + //save = 0; } int str2ff(const char *string) { @@ -84,7 +85,9 @@ int get_num_threads() { void readline(char *line, FILE *fp) { if(fgets(line, MAXLINE, fp) == NULL) { - fprintf(stderr, "readline(): Error: could not read line!\n"); - exit(-1); + if(errno != 0) { + perror("readline()"); + exit(-1); + } } }