summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWormHeamer2025-12-02 05:18:30 -0500
committerWormHeamer2025-12-02 05:18:30 -0500
commit5b6ee7e762b7b6db2e4979d740a104d7b8dc15fd (patch)
tree2f46d37913656d0b8cfe68e91ecc97e491c9eea9
parent7b85b8ae6162aace521fe1051c576f9889155350 (diff)
add fstat() implementation for read_all_file_size()
-rw-r--r--strio.h9
1 files changed, 9 insertions, 0 deletions
diff --git a/strio.h b/strio.h
index a99f8c3..a7df9ca 100644
--- a/strio.h
+++ b/strio.h
@@ -28,12 +28,21 @@ const char *cstr_fmt(Arena *arena, const char *fmt, ...);
#include <stdint.h>
#include <stdarg.h>
+#if defined(__linux__) || defined(_POSIX_C_SOURCE)
+#include <sys/stat.h>
+static inline long read_all_file_size(FILE *f) {
+ struct stat b;
+ if (fstat(fileno(f), &b)) return -1;
+ return b.st_size;
+}
+#else
static inline long read_all_file_size(FILE *f) {
fseek(f, 0, SEEK_END);
long t = ftell(f);
fseek(f, 0, SEEK_SET);
return t > 0 ? t : -1;
}
+#endif
int read_all(FILE *f, Str *buf, Arena *a) {
if (!f) return -1;