mirror of
https://github.com/BLAKE3-team/BLAKE3
synced 2024-05-05 03:36:12 +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
|
```c
|
||||||
#include "blake3.h"
|
#include "blake3.h"
|
||||||
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
@ -17,9 +20,16 @@ int main() {
|
||||||
|
|
||||||
// Read input bytes from stdin.
|
// Read input bytes from stdin.
|
||||||
unsigned char buf[65536];
|
unsigned char buf[65536];
|
||||||
ssize_t n;
|
while (1) {
|
||||||
while ((n = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
|
ssize_t n = read(STDIN_FILENO, buf, sizeof(buf));
|
||||||
blake3_hasher_update(&hasher, buf, n);
|
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.
|
// 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 "blake3.h"
|
||||||
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
@ -9,9 +12,16 @@ int main() {
|
||||||
|
|
||||||
// Read input bytes from stdin.
|
// Read input bytes from stdin.
|
||||||
unsigned char buf[65536];
|
unsigned char buf[65536];
|
||||||
ssize_t n;
|
while (1) {
|
||||||
while ((n = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
|
ssize_t n = read(STDIN_FILENO, buf, sizeof(buf));
|
||||||
blake3_hasher_update(&hasher, buf, n);
|
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.
|
// Finalize the hash. BLAKE3_OUT_LEN is the default output length, 32 bytes.
|
||||||
|
|
Loading…
Reference in New Issue