From 877526b5df3c73310f1029e56c9dff1c0374c7a2 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 12 Sep 2020 20:05:55 -0700 Subject: Fixes for CLI flags --- utils.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'utils.c') diff --git a/utils.c b/utils.c index 3a50abd..95fe0f0 100644 --- a/utils.c +++ b/utils.c @@ -154,19 +154,25 @@ size_t unescape_string(char *dest, const char *src, size_t bufsize) } /* - * Read an entire file into memory. + * Read an entire file into memory. (Guaranteeing that ret[-1] == '\0') */ char *readfile(int fd) { size_t capacity = 1000, len = 0; char *buf = calloc(sizeof(char), capacity+1); + buf[len++] = '\0'; ssize_t just_read; while ((just_read=read(fd, &buf[len], capacity-len)) > 0) { len += (size_t)just_read; if (len >= capacity) buf = realloc(buf, sizeof(char)*(capacity *= 2)); } - return buf; + return &buf[1]; +} + +void freefile(char *f) +{ + free(&f[-1]); } // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1 -- cgit v1.2.3