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:
parent
4032a51a32
commit
32758e34a4
16
c/README.md
16
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.
|
||||
|
|
16
c/example.c
16
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.
|
||||
|
|
Loading…
Reference in New Issue