aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack O'Connor <[email protected]>2025-03-15 22:15:59 -0700
committerJack O'Connor <[email protected]>2025-03-15 22:21:50 -0700
commita2516b78c2617d9ad289aeec91a02cb05d70cc17 (patch)
treef252d49f10c8a5612a8704dd693d9cf85c4254b8
parent2374b56df6be47d2865e512dc80c1f4f4c0b89d3 (diff)
close the fd in example_tbb.c
-rw-r--r--c/example_tbb.c9
1 files changed, 7 insertions, 2 deletions
diff --git a/c/example_tbb.c b/c/example_tbb.c
index 2b02fbe..88c4348 100644
--- a/c/example_tbb.c
+++ b/c/example_tbb.c
@@ -5,11 +5,12 @@
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
+#include <unistd.h>
int main(int argc, char **argv) {
// For each filepath argument, memory map it and hash it.
for (int i = 1; i < argc; i++) {
- // Memory map the file.
+ // Open and memory map the file.
int fd = open(argv[i], O_RDONLY);
if (fd == -1) {
fprintf(stderr, "open failed: %s\n", strerror(errno));
@@ -33,11 +34,15 @@ int main(int argc, char **argv) {
// Hash the mapped file using multiple threads.
blake3_hasher_update_tbb(&hasher, mapped, statbuf.st_size);
- // Unmap the file.
+ // Unmap and close the file.
if (munmap(mapped, statbuf.st_size) == -1) {
fprintf(stderr, "munmap failed: %s\n", strerror(errno));
return 1;
}
+ if (close(fd) == -1) {
+ fprintf(stderr, "close failed: %s\n", strerror(errno));
+ return 1;
+ }
// Finalize the hash. BLAKE3_OUT_LEN is the default output length, 32 bytes.
uint8_t output[BLAKE3_OUT_LEN];