aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--c/README.md16
-rw-r--r--c/example.c16
2 files changed, 26 insertions, 6 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.
diff --git a/c/example.c b/c/example.c
index 02fe3c3..0537548 100644
--- a/c/example.c
+++ b/c/example.c
@@ -1,5 +1,8 @@
#include "blake3.h"
+#include <errno.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
int main() {
@@ -9,9 +12,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.