Skip to content

Commit f64c86d

Browse files
committed
Use mg_snprintf()
1 parent 9e63431 commit f64c86d

File tree

22 files changed

+659
-458
lines changed

22 files changed

+659
-458
lines changed

examples/file-upload/main.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
// Streaming upload example. Demonstrates how to use chunked encoding
55
// to send large payload in smaller chunks. To test, use curl utility:
66
//
7-
// curl http://localhost:8000/upload \
8-
// --data-binary @my_large_file.txt -H 'Transfer-Encoding: chunked'
7+
// curl http://localhost:8000/upload?name=a.txt --data-binary @large_file.txt
98

109
#include "mongoose.h"
1110

mongoose.c

Lines changed: 299 additions & 182 deletions
Large diffs are not rendered by default.

mongoose.h

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,6 @@ extern "C" {
9999

100100
#define socklen_t int
101101
#define closesocket(x) soc_close(x)
102-
#define gmtime_r(a, b) gmtime(a)
103-
#define MG_INT64_FMT "%lld"
104-
105-
static __inline struct tm *localtime_r(const time_t *t, struct tm *tm) {
106-
struct tm *x = localtime(t);
107-
*tm = *x;
108-
return tm;
109-
}
110102

111103
#undef FOPEN_MAX
112104

@@ -192,16 +184,6 @@ struct timeval {
192184
#error Set LWIP_SOCKET variable to 1 (in lwipopts.h)
193185
#endif
194186

195-
#if LWIP_POSIX_SOCKETS_IO_NAMES != 0
196-
// LWIP_POSIX_SOCKETS_IO_NAMES must be disabled in posix-compatible OS
197-
// enviroment (freertos mimics to one) otherwise names like `read` and `write`
198-
// conflict
199-
#error LWIP_POSIX_SOCKETS_IO_NAMES must be set to 0 (in lwipopts.h) for FreeRTOS
200-
#endif
201-
202-
#define MG_INT64_FMT "%lld"
203-
#define MG_DIRSEP '/'
204-
205187
// Re-route calloc/free to the FreeRTOS's functions, don't use stdlib
206188
static inline void *mg_calloc(int cnt, size_t size) {
207189
void *p = pvPortMalloc(cnt * size);
@@ -211,7 +193,6 @@ static inline void *mg_calloc(int cnt, size_t size) {
211193
#define calloc(a, b) mg_calloc((a), (b))
212194
#define free(a) vPortFree(a)
213195
#define malloc(a) pvPortMalloc(a)
214-
#define gmtime_r(a, b) gmtime(a)
215196
#define mkdir(a, b) (-1)
216197

217198
#endif // MG_ARCH == MG_ARCH_FREERTOS_LWIP
@@ -275,8 +256,6 @@ static inline void *mg_calloc(int cnt, size_t size) {
275256
#define malloc(a) pvPortMalloc(a)
276257
#define mkdir(a, b) (-1)
277258

278-
#define gmtime_r(a, b) gmtime(a)
279-
280259
#if !defined(__GNUC__)
281260
// copied from GCC on ARM; for some reason useconds are signed
282261
struct timeval {
@@ -331,8 +310,6 @@ struct timeval {
331310
#include <time.h>
332311
#include <unistd.h>
333312

334-
#define MG_INT64_FMT "%" PRId64
335-
336313
#ifndef MG_ENABLE_DIRLIST
337314
#define MG_ENABLE_DIRLIST 1
338315
#endif
@@ -430,19 +407,6 @@ typedef int socklen_t;
430407
#define MG_ENABLE_DIRLIST 1
431408
#endif
432409

433-
// https://lgtm.com/rules/2154840805/ -gmtime, localtime, ctime and asctime
434-
static __inline struct tm *gmtime_r(const time_t *t, struct tm *tm) {
435-
struct tm *x = gmtime(t);
436-
*tm = *x;
437-
return tm;
438-
}
439-
440-
static __inline struct tm *localtime_r(const time_t *t, struct tm *tm) {
441-
struct tm *x = localtime(t);
442-
*tm = *x;
443-
return tm;
444-
}
445-
446410
#endif
447411

448412

@@ -584,6 +548,17 @@ const char *mg_strstr(const struct mg_str haystack, const struct mg_str needle);
584548
bool mg_match(struct mg_str str, struct mg_str pattern, struct mg_str *caps);
585549
bool mg_globmatch(const char *pattern, size_t plen, const char *s, size_t n);
586550
bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v);
551+
bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v);
552+
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap);
553+
size_t mg_snprintf(char *buf, size_t len, const char *fmt, ...);
554+
char *mg_hexdump(const void *buf, size_t len);
555+
char *mg_hex(const void *buf, size_t len, char *dst);
556+
void mg_unhex(const char *buf, size_t len, unsigned char *to);
557+
unsigned long mg_unhexn(const char *s, size_t len);
558+
int mg_asprintf(char **buf, size_t size, const char *fmt, ...);
559+
int mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap);
560+
int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip);
561+
int64_t mg_to64(struct mg_str str);
587562

588563

589564

@@ -687,14 +662,6 @@ void mg_random(void *buf, size_t len);
687662
uint16_t mg_ntohs(uint16_t net);
688663
uint32_t mg_ntohl(uint32_t net);
689664
uint32_t mg_crc32(uint32_t crc, const char *buf, size_t len);
690-
char *mg_hexdump(const void *buf, size_t len);
691-
char *mg_hex(const void *buf, size_t len, char *dst);
692-
void mg_unhex(const char *buf, size_t len, unsigned char *to);
693-
unsigned long mg_unhexn(const char *s, size_t len);
694-
int mg_asprintf(char **buf, size_t size, const char *fmt, ...);
695-
int mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap);
696-
int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip);
697-
int64_t mg_to64(struct mg_str str);
698665
int64_t mg_millis(void);
699666

700667
#define mg_htons(x) mg_ntohs(x)

src/arch_azurertos.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,6 @@
2525

2626
#define socklen_t int
2727
#define closesocket(x) soc_close(x)
28-
#define gmtime_r(a, b) gmtime(a)
29-
#define MG_INT64_FMT "%lld"
30-
31-
static __inline struct tm *localtime_r(const time_t *t, struct tm *tm) {
32-
struct tm *x = localtime(t);
33-
*tm = *x;
34-
return tm;
35-
}
3628

3729
#undef FOPEN_MAX
3830

src/arch_freertos_lwip.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,6 @@ struct timeval {
2929
#error Set LWIP_SOCKET variable to 1 (in lwipopts.h)
3030
#endif
3131

32-
#if LWIP_POSIX_SOCKETS_IO_NAMES != 0
33-
// LWIP_POSIX_SOCKETS_IO_NAMES must be disabled in posix-compatible OS
34-
// enviroment (freertos mimics to one) otherwise names like `read` and `write`
35-
// conflict
36-
#error LWIP_POSIX_SOCKETS_IO_NAMES must be set to 0 (in lwipopts.h) for FreeRTOS
37-
#endif
38-
39-
#define MG_INT64_FMT "%lld"
40-
#define MG_DIRSEP '/'
41-
4232
// Re-route calloc/free to the FreeRTOS's functions, don't use stdlib
4333
static inline void *mg_calloc(int cnt, size_t size) {
4434
void *p = pvPortMalloc(cnt * size);
@@ -48,7 +38,6 @@ static inline void *mg_calloc(int cnt, size_t size) {
4838
#define calloc(a, b) mg_calloc((a), (b))
4939
#define free(a) vPortFree(a)
5040
#define malloc(a) pvPortMalloc(a)
51-
#define gmtime_r(a, b) gmtime(a)
5241
#define mkdir(a, b) (-1)
5342

5443
#endif // MG_ARCH == MG_ARCH_FREERTOS_LWIP

src/arch_freertos_tcp.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ static inline void *mg_calloc(int cnt, size_t size) {
5858
#define malloc(a) pvPortMalloc(a)
5959
#define mkdir(a, b) (-1)
6060

61-
#define gmtime_r(a, b) gmtime(a)
62-
6361
#if !defined(__GNUC__)
6462
// copied from GCC on ARM; for some reason useconds are signed
6563
struct timeval {

src/arch_unix.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
#include <time.h>
3131
#include <unistd.h>
3232

33-
#define MG_INT64_FMT "%" PRId64
34-
3533
#ifndef MG_ENABLE_DIRLIST
3634
#define MG_ENABLE_DIRLIST 1
3735
#endif

src/arch_win32.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,4 @@ typedef int socklen_t;
9090
#define MG_ENABLE_DIRLIST 1
9191
#endif
9292

93-
// https://lgtm.com/rules/2154840805/ -gmtime, localtime, ctime and asctime
94-
static __inline struct tm *gmtime_r(const time_t *t, struct tm *tm) {
95-
struct tm *x = gmtime(t);
96-
*tm = *x;
97-
return tm;
98-
}
99-
100-
static __inline struct tm *localtime_r(const time_t *t, struct tm *tm) {
101-
struct tm *x = localtime(t);
102-
*tm = *x;
103-
return tm;
104-
}
105-
10693
#endif

src/dns.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms,
231231
dnsc->c = mg_connect(c->mgr, dnsc->url, NULL, NULL);
232232
if (dnsc->c != NULL) {
233233
dnsc->c->pfn = dns_cb;
234-
// snprintf(dnsc->c->label, sizeof(dnsc->c->label), "%s", "DNS");
235234
// dnsc->c->is_hexdumping = 1;
236235
}
237236
}

src/fs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ bool mg_file_write(struct mg_fs *fs, const char *path, const void *buf,
4747
bool result = false;
4848
struct mg_fd *fd;
4949
char tmp[MG_PATH_MAX];
50-
snprintf(tmp, sizeof(tmp), "%s..%d", path, rand());
50+
mg_snprintf(tmp, sizeof(tmp), "%s..%d", path, rand());
5151
if ((fd = mg_fs_open(fs, tmp, MG_FS_WRITE)) != NULL) {
5252
result = fs->wr(fd->fd, buf, len) == len;
5353
mg_fs_close(fd);

0 commit comments

Comments
 (0)