diff options
| author | Jack O'Connor <[email protected]> | 2021-08-24 12:09:32 -0400 |
|---|---|---|
| committer | Jack O'Connor <[email protected]> | 2021-08-24 12:15:46 -0400 |
| commit | 32758e34a412608e253535c69bde2e382d489caa (patch) | |
| tree | ce0437f32caa80e2a53c6bad9667a988c22f482c /c/README.md | |
| parent | 4032a51a328a448f14a2aff000309980ccc8c968 (diff) | |
handle IO errors in example.c
Diffstat (limited to 'c/README.md')
| -rw-r--r-- | c/README.md | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/c/README.md b/c/README.md index 5026b0d..5268818 100644 --- a/c/README.md +++ b/c/README.md @@ -7,7 +7,10 @@ result: ```c #include "blake3.h" +#include <errno.h> #include <stdio.h> +#include <stdlib.h> +#include <string.h> #include <unistd.h> int main() { @@ -17,9 +20,16 @@ int main() { // Read input bytes from stdin. unsigned char buf[65536]; - ssize_t n; - while ((n = read(STDIN_FILENO, buf, sizeof(buf))) > 0) { - blake3_hasher_update(&hasher, buf, n); + while (1) { + ssize_t n = read(STDIN_FILENO, buf, sizeof(buf)); + if (n > 0) { + blake3_hasher_update(&hasher, buf, n); + } else if (n == 0) { + break; // end of file + } else { + fprintf(stderr, "read failed: %s\n", strerror(errno)); + exit(1); + } } // Finalize the hash. BLAKE3_OUT_LEN is the default output length, 32 bytes. |
