diff options
author | June McEnroe <june@causal.agency> | 2019-02-12 03:34:00 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2019-02-12 03:34:00 -0500 |
commit | 68d5b021f20d8e07731f237acf452be1a636858f (patch) | |
tree | bfdbc63e76f06b2ebf22a8c1cd8872a52325b3ed | |
parent | Don't match Rust raw strings inside other strings (diff) | |
download | src-68d5b021f20d8e07731f237acf452be1a636858f.tar.gz src-68d5b021f20d8e07731f237acf452be1a636858f.zip |
Use a proper fread-realloc loop in hi
-rw-r--r-- | bin/hi.c | 22 | ||||
-rw-r--r-- | bin/man1/hi.1 | 4 |
2 files changed, 14 insertions, 12 deletions
diff --git a/bin/hi.c b/bin/hi.c index de3da231..9ac3851e 100644 --- a/bin/hi.c +++ b/bin/hi.c @@ -682,18 +682,22 @@ int main(int argc, char *argv[]) { } if (!opts[Title]) opts[Title] = name; - size_t len = 32 * 1024; - if (file != stdin) { - struct stat stat; - int error = fstat(fileno(file), &stat); - if (error) err(EX_IOERR, "fstat"); - len = stat.st_size; - } + struct stat stat; + int error = fstat(fileno(file), &stat); + if (error) err(EX_IOERR, "fstat"); - char *str = malloc(len + 1); + size_t cap = (stat.st_mode & S_IFREG ? stat.st_size + 1 : 4096); + char *str = malloc(cap); if (!str) err(EX_OSERR, "malloc"); - len = fread(str, 1, len, file); + size_t len = 0, read; + while (0 < (read = fread(&str[len], 1, cap - len - 1, file))) { + len += read; + if (len + 1 < cap) continue; + cap *= 2; + str = realloc(str, cap); + if (!str) err(EX_OSERR, "realloc"); + } if (ferror(file)) err(EX_IOERR, "fread"); str[len] = '\0'; diff --git a/bin/man1/hi.1 b/bin/man1/hi.1 index 537acef5..c48fa796 100644 --- a/bin/man1/hi.1 +++ b/bin/man1/hi.1 @@ -1,4 +1,4 @@ -.Dd February 11, 2019 +.Dd February 12, 2019 .Dt HI 1 .Os . @@ -23,8 +23,6 @@ highlights the contents of a or standard input and formats it on standard output. -A maximum of 32K -is read from standard input. . .Pp The arguments are as follows: |