1
0
Fork 0
mirror of https://github.com/BLAKE3-team/BLAKE3 synced 2024-04-25 14:55:10 +02:00

handle IO errors in example.c

This commit is contained in:
Jack O'Connor 2021-08-24 12:09:32 -04:00
parent 4032a51a32
commit 32758e34a4
2 changed files with 26 additions and 6 deletions

View File

@ -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.

View File

@ -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.