diff --git a/docs/README.md b/docs/README.md index 9867c1f7..a4ed287d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1115,14 +1115,6 @@ Read file contents into a nul-terminated malloc-ed string. It is a caller's responsibility to free() a returned pointer. If `sizep` is not NULL, it will return a file size in bytes. -### mg\_file\_size() - -```c -size_t mg_file_size(const char *path); -``` - -Return file size, or 0 on failure. Empty files also report 0 length. - ### mg\_file\_write() ```c diff --git a/mongoose.c b/mongoose.c index 96ac19ef..3c31db83 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3921,21 +3921,14 @@ int mg_stat(const char *path, mg_stat_t *st) { #endif } -int64_t mg_file_size(const char *path) { -#if MG_ARCH == MG_ARCH_FREERTOS_TCP && defined(MG_ENABLE_FF) - struct FF_STAT st; - return ff_stat(path, &st) == 0 ? st.st_size : 0; -#else - mg_stat_t st; - return mg_stat(path, &st) == 0 ? st.st_size : 0; -#endif -} - char *mg_file_read(const char *path, size_t *sizep) { FILE *fp; char *data = NULL; - size_t size = (size_t) mg_file_size(path); + size_t size = 0; if ((fp = fopen(path, "rb")) != NULL) { + fseek(fp, 0, SEEK_END); + size = (size_t) ftell(fp); + rewind(fp); data = (char *) calloc(1, size + 1); if (data != NULL) { if (fread(data, 1, size, fp) != size) { diff --git a/mongoose.h b/mongoose.h index ba03763e..d5dcca86 100644 --- a/mongoose.h +++ b/mongoose.h @@ -541,7 +541,6 @@ void mg_timer_poll(unsigned long uptime_ms); #endif char *mg_file_read(const char *path, size_t *size); -int64_t mg_file_size(const char *path); bool mg_file_write(const char *path, const void *buf, size_t len); bool mg_file_printf(const char *path, const char *fmt, ...); void mg_random(void *buf, size_t len) WEAK; diff --git a/src/util.c b/src/util.c index 54fb4a4e..0e3af39d 100644 --- a/src/util.c +++ b/src/util.c @@ -13,21 +13,14 @@ int mg_stat(const char *path, mg_stat_t *st) { #endif } -int64_t mg_file_size(const char *path) { -#if MG_ARCH == MG_ARCH_FREERTOS_TCP && defined(MG_ENABLE_FF) - struct FF_STAT st; - return ff_stat(path, &st) == 0 ? st.st_size : 0; -#else - mg_stat_t st; - return mg_stat(path, &st) == 0 ? st.st_size : 0; -#endif -} - char *mg_file_read(const char *path, size_t *sizep) { FILE *fp; char *data = NULL; - size_t size = (size_t) mg_file_size(path); + size_t size = 0; if ((fp = fopen(path, "rb")) != NULL) { + fseek(fp, 0, SEEK_END); + size = (size_t) ftell(fp); + rewind(fp); data = (char *) calloc(1, size + 1); if (data != NULL) { if (fread(data, 1, size, fp) != size) { diff --git a/src/util.h b/src/util.h index f530f74d..a261813f 100644 --- a/src/util.h +++ b/src/util.h @@ -17,7 +17,6 @@ #endif char *mg_file_read(const char *path, size_t *size); -int64_t mg_file_size(const char *path); bool mg_file_write(const char *path, const void *buf, size_t len); bool mg_file_printf(const char *path, const char *fmt, ...); void mg_random(void *buf, size_t len) WEAK;