diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2020-09-12 20:05:55 -0700 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2020-09-12 20:05:55 -0700 |
| commit | 877526b5df3c73310f1029e56c9dff1c0374c7a2 (patch) | |
| tree | 856a93dc8d372e66ca240c50cc5fb0e4e0c84ca1 /utils.c | |
| parent | c18eb4c9968289c4808d70f7124c0b6bed5eb022 (diff) | |
Fixes for CLI flags
Diffstat (limited to 'utils.c')
| -rw-r--r-- | utils.c | 10 |
1 files changed, 8 insertions, 2 deletions
@@ -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 |
