diff options
-rw-r--r-- | Makefile.in | 4 | ||||
-rw-r--r-- | random-seed.c | 19 | ||||
-rw-r--r-- | readall.c | 82 | ||||
-rw-r--r-- | readall.h | 14 |
4 files changed, 10 insertions, 109 deletions
diff --git a/Makefile.in b/Makefile.in index a86c2f9..45593d3 100644 --- a/Makefile.in +++ b/Makefile.in @@ -5,12 +5,12 @@ sbindir = @sbindir@ sysconfdir = @sysconfdir@ localstatedir = @localstatedir@ mandir = @mandir@ -CPPFLAGS = @CPPFLAGS@ +CPPFLAGS = @CPPFLAGS@ -DSYSCONFDIR='"$(sysconfdir)"' -DLOCALSTATEDIR='"$(localstatedir)"' CFLAGS = -Wall -Wextra -pedantic @SYSTEMD_CFLAGS@ @CFLAGS@ -MD -MP -UNDEBUG -include @abs_top_builddir@/config.h LDFLAGS = @LDFLAGS@ LDLIBS = @SYSTEMD_LIBS@ -SRC ::= random-seed.c sha2.c util.c readall.c +SRC ::= random-seed.c sha2.c util.c OBJ ::= $(SRC:.c=.o) DEP ::= $(SRC:.c=.d) TEST_FILE ::= random-seed.test diff --git a/random-seed.c b/random-seed.c index 7a0527a..bc0952f 100644 --- a/random-seed.c +++ b/random-seed.c @@ -40,7 +40,6 @@ #endif #include "musl-libgen-c.h" -#include "readall.h" #include "util.h" // musl forbids include/linux @@ -65,14 +64,14 @@ static inline void usage() { puts("see random-seed(8) for more information."); } -static size_t get_machine_id(unsigned char **machine_id) { +static bool get_machine_id(unsigned char **machine_id) { #ifdef HAVE_SYSTEMD sd_id128_t raw_machine_id; int r = sd_id128_get_machine(&raw_machine_id); if (r) { fprintf(stderr, "error getting machine id: %s\n", strerror(-r)); *machine_id = NULL; - return 0; + return false; } *machine_id = malloc(33); /* convert to string to be compatible with non-hex machine IDs */ @@ -80,11 +79,8 @@ static size_t get_machine_id(unsigned char **machine_id) { /* match /etc/machine-id format */ (*machine_id)[32] = '\n'; - return 33; + return true; #else - char *machine_id; - ssize_t machine_id_len; - const char *etc_machine_id = SYSCONFDIR "/machine-id"; const char *var_lib_dbus_machine_id = LOCALSTATEDIR "/lib/dbus/machine-id"; FILE *machine_id_file = fopen(etc_machine_id, "r"); @@ -98,17 +94,18 @@ static size_t get_machine_id(unsigned char **machine_id) { "%s; tried: %s, %s\n", strerror(errno), etc_machine_id, var_lib_dbus_machine_id); *machine_id = NULL; - return 0; + return false; } } - if (readall(machine_id_file, machine_id, &machine_id_len) != READALL_OK) { + size_t machine_id_len = 0; + if (getdelim((char **)machine_id, &machine_id_len, '\0', machine_id_file) != -1) { fputs("error reading machine id file\n", stderr); *machine_id = NULL; - return 0; + return false; } - return machine_id_len; + return true; #endif } diff --git a/readall.c b/readall.c deleted file mode 100644 index 9f55697..0000000 --- a/readall.c +++ /dev/null @@ -1,82 +0,0 @@ -/* by Nominal Animal, 2017: https://stackoverflow.com/a/44894946 */ - -#include <stdlib.h> -#include <stdio.h> -#include <errno.h> - -#include "readall.h" - -/* Size of each input chunk to be - read and allocate for. */ -#ifndef READALL_CHUNK -#define READALL_CHUNK 262144 -#endif - -/* This function returns one of the READALL_ constants above. - If the return value is zero == READALL_OK, then: - (*dataptr) points to a dynamically allocated buffer, with - (*sizeptr) chars read from the file. - The buffer is allocated for one extra char, which is NUL, - and automatically appended after the data. - Initial values of (*dataptr) and (*sizeptr) are ignored. -*/ -int readall(FILE *in, char **dataptr, size_t *sizeptr) -{ - char *data = NULL, *temp; - size_t size = 0; - size_t used = 0; - size_t n; - - /* None of the parameters can be NULL. */ - if (in == NULL || dataptr == NULL || sizeptr == NULL) - return READALL_INVALID; - - /* A read error already occurred? */ - if (ferror(in)) - return READALL_ERROR; - - while (1) { - - if (used + READALL_CHUNK + 1 > size) { - size = used + READALL_CHUNK + 1; - - /* Overflow check. Some ANSI C compilers - may optimize this away, though. */ - if (size <= used) { - free(data); - return READALL_TOOMUCH; - } - - temp = realloc(data, size); - if (temp == NULL) { - free(data); - return READALL_NOMEM; - } - data = temp; - } - - n = fread(data + used, 1, READALL_CHUNK, in); - if (n == 0) - break; - - used += n; - } - - if (ferror(in)) { - free(data); - return READALL_ERROR; - } - - temp = realloc(data, used + 1); - if (temp == NULL) { - free(data); - return READALL_NOMEM; - } - data = temp; - data[used] = '\0'; - - *dataptr = data; - *sizeptr = used; - - return READALL_OK; -} diff --git a/readall.h b/readall.h deleted file mode 100644 index 94c2bfe..0000000 --- a/readall.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef READALL_H -#define READALL_H - -#include <stdio.h> - -#define READALL_OK 0 /* Success */ -#define READALL_INVALID -1 /* Invalid parameters */ -#define READALL_ERROR -2 /* Stream error */ -#define READALL_TOOMUCH -3 /* Too much input */ -#define READALL_NOMEM -4 /* Out of memory */ - -int readall(FILE *in, char **dataptr, size_t *sizeptr); - -#endif |