diff --git a/docs/README.md b/docs/README.md index 058048db..3e6e0622 100644 --- a/docs/README.md +++ b/docs/README.md @@ -241,7 +241,6 @@ Here is a list of build constants and their default values: |MG_ENABLE_LOG | 1 | Enable `LOG()` macro | |MG_ENABLE_MD5 | 0 | Use native MD5 implementation | |MG_ENABLE_SSI | 1 | Enable serving SSI files by `mg_http_serve_dir()` | -|MG_ENABLE_DIRLIST | 0 | Enable directory listing | |MG_ENABLE_CUSTOM_RANDOM | 0 | Provide custom RNG function `mg_random()` | |MG_ENABLE_PACKED_FS | 0 | Enable embedded FS support | |MG_ENABLE_FATFS | 0 | Enable embedded FAT FS support | diff --git a/mongoose.c b/mongoose.c index 3b13219a..31a40dfd 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3900,9 +3900,17 @@ const char *mg_strstr(const struct mg_str haystack, return NULL; } +static bool is_digit(int c) { + return c >= '0' && c <= '9'; +} + +static bool is_space(int c) { + return c == ' ' || c == '\r' || c == '\n' || c == '\t'; +} + struct mg_str mg_strstrip(struct mg_str s) { - while (s.len > 0 && isspace((int) *s.ptr)) s.ptr++, s.len--; - while (s.len > 0 && isspace((int) *(s.ptr + s.len - 1))) s.len--; + while (s.len > 0 && is_space((int) *s.ptr)) s.ptr++, s.len--; + while (s.len > 0 && is_space((int) *(s.ptr + s.len - 1))) s.len--; return s; } @@ -4115,14 +4123,14 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) { if (c == '#') x++, c = fmt[++i]; if (c == '-') minus++, c = fmt[++i]; if (c == '0') pad = '0', c = fmt[++i]; - while (isdigit(c)) w *= 10, w += (size_t) (c - '0'), c = fmt[++i]; + while (is_digit(c)) w *= 10, w += (size_t) (c - '0'), c = fmt[++i]; if (c == '.') { c = fmt[++i]; if (c == '*') { pr = (size_t) va_arg(ap, int); c = fmt[++i]; } else { - while (isdigit(c)) pr *= 10, pr += (size_t) (c - '0'), c = fmt[++i]; + while (is_digit(c)) pr *= 10, pr += (size_t) (c - '0'), c = fmt[++i]; } } while (c == 'h') c = fmt[++i]; // Treat h and hh as int @@ -4170,7 +4178,10 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) { if (n < len) buf[n] = '%'; n++; } else { - abort(); // Unsupported format specifier + if (n < len) buf[n] = '%'; + n++; + if (n < len) buf[n] = c; + n++; } i++; } else { diff --git a/mongoose.h b/mongoose.h index b2a658db..9679b003 100644 --- a/mongoose.h +++ b/mongoose.h @@ -121,7 +121,6 @@ extern "C" { #include #define MG_PATH_MAX 128 -#define MG_ENABLE_DIRLIST 1 #endif @@ -148,7 +147,6 @@ extern "C" { #include #define MG_PATH_MAX 128 -#define MG_ENABLE_DIRLIST 1 #endif @@ -316,10 +314,6 @@ struct timeval { #include #include -#ifndef MG_ENABLE_DIRLIST -#define MG_ENABLE_DIRLIST 1 -#endif - #endif @@ -409,10 +403,6 @@ typedef int socklen_t; #define S_ISDIR(x) (((x) &_S_IFMT) == _S_IFDIR) #endif -#ifndef MG_ENABLE_DIRLIST -#define MG_ENABLE_DIRLIST 1 -#endif - #endif @@ -466,7 +456,7 @@ typedef int socklen_t; #endif #ifndef MG_ENABLE_DIRLIST -#define MG_ENABLE_DIRLIST 0 +#define MG_ENABLE_DIRLIST 1 #endif #ifndef MG_ENABLE_CUSTOM_RANDOM diff --git a/src/arch_esp32.h b/src/arch_esp32.h index a6925a3c..e5d8b6dc 100644 --- a/src/arch_esp32.h +++ b/src/arch_esp32.h @@ -18,6 +18,5 @@ #include #define MG_PATH_MAX 128 -#define MG_ENABLE_DIRLIST 1 #endif diff --git a/src/arch_esp8266.h b/src/arch_esp8266.h index 8c00ec94..f906a29f 100644 --- a/src/arch_esp8266.h +++ b/src/arch_esp8266.h @@ -22,6 +22,5 @@ #include #define MG_PATH_MAX 128 -#define MG_ENABLE_DIRLIST 1 #endif diff --git a/src/arch_unix.h b/src/arch_unix.h index dd696e22..9af31f60 100644 --- a/src/arch_unix.h +++ b/src/arch_unix.h @@ -30,8 +30,4 @@ #include #include -#ifndef MG_ENABLE_DIRLIST -#define MG_ENABLE_DIRLIST 1 -#endif - #endif diff --git a/src/arch_win32.h b/src/arch_win32.h index deada3ec..325e2836 100644 --- a/src/arch_win32.h +++ b/src/arch_win32.h @@ -86,8 +86,4 @@ typedef int socklen_t; #define S_ISDIR(x) (((x) &_S_IFMT) == _S_IFDIR) #endif -#ifndef MG_ENABLE_DIRLIST -#define MG_ENABLE_DIRLIST 1 -#endif - #endif diff --git a/src/config.h b/src/config.h index 51d2a00e..19d3b566 100644 --- a/src/config.h +++ b/src/config.h @@ -50,7 +50,7 @@ #endif #ifndef MG_ENABLE_DIRLIST -#define MG_ENABLE_DIRLIST 0 +#define MG_ENABLE_DIRLIST 1 #endif #ifndef MG_ENABLE_CUSTOM_RANDOM diff --git a/src/str.c b/src/str.c index cc9d41d0..3cac13cc 100644 --- a/src/str.c +++ b/src/str.c @@ -81,9 +81,17 @@ const char *mg_strstr(const struct mg_str haystack, return NULL; } +static bool is_digit(int c) { + return c >= '0' && c <= '9'; +} + +static bool is_space(int c) { + return c == ' ' || c == '\r' || c == '\n' || c == '\t'; +} + struct mg_str mg_strstrip(struct mg_str s) { - while (s.len > 0 && isspace((int) *s.ptr)) s.ptr++, s.len--; - while (s.len > 0 && isspace((int) *(s.ptr + s.len - 1))) s.len--; + while (s.len > 0 && is_space((int) *s.ptr)) s.ptr++, s.len--; + while (s.len > 0 && is_space((int) *(s.ptr + s.len - 1))) s.len--; return s; } @@ -296,14 +304,14 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) { if (c == '#') x++, c = fmt[++i]; if (c == '-') minus++, c = fmt[++i]; if (c == '0') pad = '0', c = fmt[++i]; - while (isdigit(c)) w *= 10, w += (size_t) (c - '0'), c = fmt[++i]; + while (is_digit(c)) w *= 10, w += (size_t) (c - '0'), c = fmt[++i]; if (c == '.') { c = fmt[++i]; if (c == '*') { pr = (size_t) va_arg(ap, int); c = fmt[++i]; } else { - while (isdigit(c)) pr *= 10, pr += (size_t) (c - '0'), c = fmt[++i]; + while (is_digit(c)) pr *= 10, pr += (size_t) (c - '0'), c = fmt[++i]; } } while (c == 'h') c = fmt[++i]; // Treat h and hh as int @@ -351,7 +359,10 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) { if (n < len) buf[n] = '%'; n++; } else { - abort(); // Unsupported format specifier + if (n < len) buf[n] = '%'; + n++; + if (n < len) buf[n] = c; + n++; } i++; } else { diff --git a/test/mongoose_custom.h b/test/mongoose_custom.h index 7c2c6996..dbd41b10 100644 --- a/test/mongoose_custom.h +++ b/test/mongoose_custom.h @@ -18,3 +18,4 @@ #define MG_PATH_MAX 100 #define MG_ENABLE_SOCKET 0 +#define MG_ENABLE_DIRLIST 0