diff --git a/mongoose.c b/mongoose.c index fd0e218b..873090cd 100644 --- a/mongoose.c +++ b/mongoose.c @@ -803,7 +803,7 @@ int mg_http_upload(struct mg_connection *c, struct mg_http_message *hm, snprintf(path, sizeof(path), "%s%c%s", dir, MG_DIRSEP, name); LOG(LL_DEBUG, ("%p %d bytes @ %d [%s]", c->fd, (int) hm->body.len, (int) oft, name)); - if ((fp = mg_fopen(path, oft == 0 ? "wb" : "ab")) == NULL) { + if ((fp = fopen(path, oft == 0 ? "wb" : "ab")) == NULL) { mg_http_reply(c, 400, "", "fopen(%s): %d", name, errno); return -2; } else { @@ -926,7 +926,7 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm, struct mg_str *inm = mg_http_get_header(hm, "If-None-Match"); mg_stat_t st; char etag[64]; - FILE *fp = mg_fopen(path, "rb"); + FILE *fp = fopen(path, "rb"); if (fp == NULL || mg_stat(path, &st) != 0 || mg_http_etag(etag, sizeof(etag), &st) != etag) { LOG(LL_DEBUG, ("404 [%.*s] [%s] %p", (int) hm->uri.len, hm->uri.ptr, path, @@ -1267,14 +1267,14 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm, mg_http_reply(c, 404, "", "Invalid URI [%.*s]\n", (int) hm->uri.len, hm->uri.ptr); } else { - FILE *fp = mg_fopen(t2, "r"); + FILE *fp = fopen(t2, "r"); #if MG_ENABLE_SSI if (is_index && fp == NULL) { char *p = t2 + strlen(t2); while (p > t2 && p[-1] != '/') p--; strncpy(p, "index.shtml", (size_t)(&t2[sizeof(t2)] - p - 2)); t2[sizeof(t2) - 1] = '\0'; - fp = mg_fopen(t2, "r"); + fp = fopen(t2, "r"); } #endif if (is_index && fp == NULL) { @@ -3209,7 +3209,7 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) { #if MG_ENABLE_SSI static char *mg_ssi(const char *path, const char *root, int depth) { struct mg_iobuf b = {NULL, 0, 0}; - FILE *fp = mg_fopen(path, "rb"); + FILE *fp = fopen(path, "rb"); if (fp != NULL) { char buf[BUFSIZ], arg[sizeof(buf)]; int ch, intag = 0; @@ -3921,17 +3921,6 @@ int mg_stat(const char *path, mg_stat_t *st) { #endif } -FILE *mg_fopen(const char *path, const char *mode) { -#ifdef _WIN32 - wchar_t b1[MG_PATH_MAX], b2[10]; - MultiByteToWideChar(CP_UTF8, 0, path, -1, b1, sizeof(b1) / sizeof(b1[0])); - MultiByteToWideChar(CP_UTF8, 0, mode, -1, b2, sizeof(b2) / sizeof(b2[0])); - return _wfopen(b1, b2); -#else - return fopen(path, mode); -#endif -} - int64_t mg_file_size(const char *path) { #if MG_ARCH == MG_ARCH_FREERTOS_TCP && defined(MG_ENABLE_FF) struct FF_STAT st; @@ -3946,7 +3935,7 @@ char *mg_file_read(const char *path, size_t *sizep) { FILE *fp; char *data = NULL; size_t size = (size_t) mg_file_size(path); - if ((fp = mg_fopen(path, "rb")) != NULL) { + if ((fp = fopen(path, "rb")) != NULL) { data = (char *) calloc(1, size + 1); if (data != NULL) { if (fread(data, 1, size, fp) != size) { @@ -3967,7 +3956,7 @@ bool mg_file_write(const char *path, const void *buf, size_t len) { FILE *fp; char tmp[MG_PATH_MAX]; snprintf(tmp, sizeof(tmp), "%s.%d", path, rand()); - fp = mg_fopen(tmp, "wb"); + fp = fopen(tmp, "wb"); if (fp != NULL) { result = fwrite(buf, 1, len, fp) == len; fclose(fp); @@ -4002,7 +3991,7 @@ void mg_random(void *buf, size_t len) { while (len--) *p++ = (unsigned char) (esp_random() & 255); #elif MG_ARCH == MG_ARCH_WIN32 #elif MG_ARCH_UNIX && MG_ENABLE_FS - FILE *fp = mg_fopen("/dev/urandom", "rb"); + FILE *fp = fopen("/dev/urandom", "rb"); if (fp != NULL) { if (fread(buf, 1, len, fp) == len) done = true; fclose(fp); diff --git a/mongoose.h b/mongoose.h index f3b2f43f..2fdc10fb 100644 --- a/mongoose.h +++ b/mongoose.h @@ -360,6 +360,7 @@ typedef int socklen_t; #define EWOULDBLOCK WSAEWOULDBLOCK #endif #define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX) +#define fopen(a, b) mg_fopen((a), (b)) #ifndef va_copy #ifdef __va_copy #define va_copy __va_copy @@ -373,6 +374,13 @@ typedef int socklen_t; #define MG_INT64_FMT "%I64d" +static __inline FILE *mg_fopen(const char *path, const char *mode) { + wchar_t b1[PATH_MAX], b2[10]; + MultiByteToWideChar(CP_UTF8, 0, path, -1, b1, sizeof(b1) / sizeof(b1[0])); + MultiByteToWideChar(CP_UTF8, 0, mode, -1, b2, sizeof(b2) / sizeof(b2[0])); + return _wfopen(b1, b2); +} + // https://lgtm.com/rules/2154840805/ -gmtime, localtime, ctime and asctime static __inline struct tm *gmtime_r(time_t *t, struct tm *tm) { (void) tm; @@ -560,7 +568,6 @@ typedef struct _stati64 mg_stat_t; typedef struct stat mg_stat_t; #endif int mg_stat(const char *path, mg_stat_t *); -FILE *mg_fopen(const char *fp, const char *mode); #endif #define mg_htons(x) mg_ntohs(x) diff --git a/src/arch_win32.h b/src/arch_win32.h index a95c1119..f7df817a 100644 --- a/src/arch_win32.h +++ b/src/arch_win32.h @@ -67,6 +67,7 @@ typedef int socklen_t; #define EWOULDBLOCK WSAEWOULDBLOCK #endif #define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX) +#define fopen(a, b) mg_fopen((a), (b)) #ifndef va_copy #ifdef __va_copy #define va_copy __va_copy @@ -80,6 +81,13 @@ typedef int socklen_t; #define MG_INT64_FMT "%I64d" +static __inline FILE *mg_fopen(const char *path, const char *mode) { + wchar_t b1[PATH_MAX], b2[10]; + MultiByteToWideChar(CP_UTF8, 0, path, -1, b1, sizeof(b1) / sizeof(b1[0])); + MultiByteToWideChar(CP_UTF8, 0, mode, -1, b2, sizeof(b2) / sizeof(b2[0])); + return _wfopen(b1, b2); +} + // https://lgtm.com/rules/2154840805/ -gmtime, localtime, ctime and asctime static __inline struct tm *gmtime_r(time_t *t, struct tm *tm) { (void) tm; diff --git a/src/http.c b/src/http.c index cb1d3aea..2f045971 100644 --- a/src/http.c +++ b/src/http.c @@ -392,7 +392,7 @@ int mg_http_upload(struct mg_connection *c, struct mg_http_message *hm, snprintf(path, sizeof(path), "%s%c%s", dir, MG_DIRSEP, name); LOG(LL_DEBUG, ("%p %d bytes @ %d [%s]", c->fd, (int) hm->body.len, (int) oft, name)); - if ((fp = mg_fopen(path, oft == 0 ? "wb" : "ab")) == NULL) { + if ((fp = fopen(path, oft == 0 ? "wb" : "ab")) == NULL) { mg_http_reply(c, 400, "", "fopen(%s): %d", name, errno); return -2; } else { @@ -515,7 +515,7 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm, struct mg_str *inm = mg_http_get_header(hm, "If-None-Match"); mg_stat_t st; char etag[64]; - FILE *fp = mg_fopen(path, "rb"); + FILE *fp = fopen(path, "rb"); if (fp == NULL || mg_stat(path, &st) != 0 || mg_http_etag(etag, sizeof(etag), &st) != etag) { LOG(LL_DEBUG, ("404 [%.*s] [%s] %p", (int) hm->uri.len, hm->uri.ptr, path, @@ -856,14 +856,14 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm, mg_http_reply(c, 404, "", "Invalid URI [%.*s]\n", (int) hm->uri.len, hm->uri.ptr); } else { - FILE *fp = mg_fopen(t2, "r"); + FILE *fp = fopen(t2, "r"); #if MG_ENABLE_SSI if (is_index && fp == NULL) { char *p = t2 + strlen(t2); while (p > t2 && p[-1] != '/') p--; strncpy(p, "index.shtml", (size_t)(&t2[sizeof(t2)] - p - 2)); t2[sizeof(t2) - 1] = '\0'; - fp = mg_fopen(t2, "r"); + fp = fopen(t2, "r"); } #endif if (is_index && fp == NULL) { diff --git a/src/ssi.c b/src/ssi.c index e5fb1ee1..7fe3505d 100644 --- a/src/ssi.c +++ b/src/ssi.c @@ -9,7 +9,7 @@ #if MG_ENABLE_SSI static char *mg_ssi(const char *path, const char *root, int depth) { struct mg_iobuf b = {NULL, 0, 0}; - FILE *fp = mg_fopen(path, "rb"); + FILE *fp = fopen(path, "rb"); if (fp != NULL) { char buf[BUFSIZ], arg[sizeof(buf)]; int ch, intag = 0; diff --git a/src/util.c b/src/util.c index 52755abb..2fe80de9 100644 --- a/src/util.c +++ b/src/util.c @@ -13,17 +13,6 @@ int mg_stat(const char *path, mg_stat_t *st) { #endif } -FILE *mg_fopen(const char *path, const char *mode) { -#ifdef _WIN32 - wchar_t b1[MG_PATH_MAX], b2[10]; - MultiByteToWideChar(CP_UTF8, 0, path, -1, b1, sizeof(b1) / sizeof(b1[0])); - MultiByteToWideChar(CP_UTF8, 0, mode, -1, b2, sizeof(b2) / sizeof(b2[0])); - return _wfopen(b1, b2); -#else - return fopen(path, mode); -#endif -} - int64_t mg_file_size(const char *path) { #if MG_ARCH == MG_ARCH_FREERTOS_TCP && defined(MG_ENABLE_FF) struct FF_STAT st; @@ -38,7 +27,7 @@ char *mg_file_read(const char *path, size_t *sizep) { FILE *fp; char *data = NULL; size_t size = (size_t) mg_file_size(path); - if ((fp = mg_fopen(path, "rb")) != NULL) { + if ((fp = fopen(path, "rb")) != NULL) { data = (char *) calloc(1, size + 1); if (data != NULL) { if (fread(data, 1, size, fp) != size) { @@ -59,7 +48,7 @@ bool mg_file_write(const char *path, const void *buf, size_t len) { FILE *fp; char tmp[MG_PATH_MAX]; snprintf(tmp, sizeof(tmp), "%s.%d", path, rand()); - fp = mg_fopen(tmp, "wb"); + fp = fopen(tmp, "wb"); if (fp != NULL) { result = fwrite(buf, 1, len, fp) == len; fclose(fp); @@ -94,7 +83,7 @@ void mg_random(void *buf, size_t len) { while (len--) *p++ = (unsigned char) (esp_random() & 255); #elif MG_ARCH == MG_ARCH_WIN32 #elif MG_ARCH_UNIX && MG_ENABLE_FS - FILE *fp = mg_fopen("/dev/urandom", "rb"); + FILE *fp = fopen("/dev/urandom", "rb"); if (fp != NULL) { if (fread(buf, 1, len, fp) == len) done = true; fclose(fp); diff --git a/src/util.h b/src/util.h index 3bbc5cee..b436310c 100644 --- a/src/util.h +++ b/src/util.h @@ -44,7 +44,6 @@ typedef struct _stati64 mg_stat_t; typedef struct stat mg_stat_t; #endif int mg_stat(const char *path, mg_stat_t *); -FILE *mg_fopen(const char *fp, const char *mode); #endif #define mg_htons(x) mg_ntohs(x)