diff --git a/examples/complete/main.c b/examples/complete/main.c index 28b5250e..3d948380 100644 --- a/examples/complete/main.c +++ b/examples/complete/main.c @@ -143,8 +143,8 @@ static void broadcast_mjpeg_frame(struct mg_mgr *mgr) { size_t nfiles = sizeof(files) / sizeof(files[0]); static size_t i; const char *path = files[i++ % nfiles]; - size_t size = mg_file_size(path); - char *data = mg_file_read(path); // Read next file + size_t size = 0; + char *data = mg_file_read(path, &size); // Read next file struct mg_connection *c; for (c = mgr->conns; c != NULL; c = c->next) { if (c->label[0] != 'S') continue; // Skip non-stream connections diff --git a/examples/video-stream/main.c b/examples/video-stream/main.c index 991c7f3b..e98f2f44 100644 --- a/examples/video-stream/main.c +++ b/examples/video-stream/main.c @@ -32,8 +32,8 @@ static void broadcast_mjpeg_frame(struct mg_mgr *mgr) { size_t nfiles = sizeof(files) / sizeof(files[0]); static size_t i; const char *path = files[i++ % nfiles]; - size_t size = mg_file_size(path); - char *data = mg_file_read(path); // Read next file + size_t size = 0; + char *data = mg_file_read(path, &size); // Read next file struct mg_connection *c; for (c = mgr->conns; c != NULL; c = c->next) { if (c->label[0] != 'S') continue; // Skip non-stream connections diff --git a/mongoose.c b/mongoose.c index d5d6a3fd..44bf6d51 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3902,7 +3902,7 @@ int64_t mg_file_size(const char *path) { #endif } -char *mg_file_read(const char *path) { +char *mg_file_read(const char *path, size_t *sizep) { FILE *fp; char *data = NULL; size_t size = (size_t) mg_file_size(path); @@ -3914,6 +3914,7 @@ char *mg_file_read(const char *path) { data = NULL; } else { data[size] = '\0'; + if (sizep != NULL) *sizep = size; } } fclose(fp); diff --git a/mongoose.h b/mongoose.h index 95f3937f..d60f91ab 100644 --- a/mongoose.h +++ b/mongoose.h @@ -477,7 +477,7 @@ void mg_timer_poll(unsigned long uptime_ms); -char *mg_file_read(const char *path); +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, ...); diff --git a/src/util.c b/src/util.c index 8ea74ccd..2943df73 100644 --- a/src/util.c +++ b/src/util.c @@ -33,7 +33,7 @@ int64_t mg_file_size(const char *path) { #endif } -char *mg_file_read(const char *path) { +char *mg_file_read(const char *path, size_t *sizep) { FILE *fp; char *data = NULL; size_t size = (size_t) mg_file_size(path); @@ -45,6 +45,7 @@ char *mg_file_read(const char *path) { data = NULL; } else { data[size] = '\0'; + if (sizep != NULL) *sizep = size; } } fclose(fp); diff --git a/src/util.h b/src/util.h index 1e172cf1..08f16b0f 100644 --- a/src/util.h +++ b/src/util.h @@ -3,7 +3,7 @@ #include "arch.h" #include "str.h" -char *mg_file_read(const char *path); +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, ...); diff --git a/test/unit_test.c b/test/unit_test.c index c703679b..b5e88d3f 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -503,7 +503,7 @@ static void test_http_server(void) { #endif { - char *data = mg_file_read("./test/data/ca.pem"); + char *data = mg_file_read("./test/data/ca.pem", NULL); ASSERT(fetch(&mgr, buf, url, "GET /ca.pem HTTP/1.0\r\n\n") == 200); ASSERT(cmpbody(data, buf) == 0); free(data); @@ -564,7 +564,7 @@ static void test_http_server(void) { // Test upload char *p; remove("uploaded.txt"); - ASSERT((p = mg_file_read("uploaded.txt")) == NULL); + ASSERT((p = mg_file_read("uploaded.txt", NULL)) == NULL); ASSERT(fetch(&mgr, buf, url, "POST /upload HTTP/1.0\n" @@ -578,7 +578,7 @@ static void test_http_server(void) { "POST /upload?name=uploaded.txt&offset=5 HTTP/1.0\r\n" "Content-Length: 6\r\n" "\r\n\nworld") == 200); - ASSERT((p = mg_file_read("uploaded.txt")) != NULL); + ASSERT((p = mg_file_read("uploaded.txt", NULL)) != NULL); ASSERT(strcmp(p, "hello\nworld") == 0); free(p); remove("uploaded.txt"); @@ -1069,7 +1069,7 @@ static void test_util(void) { free(s); memset(&a, 0, sizeof(a)); ASSERT(mg_file_printf("data.txt", "%s", "hi") == true); - ASSERT((p = mg_file_read("data.txt")) != NULL); + ASSERT((p = mg_file_read("data.txt", NULL)) != NULL); ASSERT(strcmp(p, "hi") == 0); free(p); remove("data.txt");