bring mg_strdup() back

This commit is contained in:
Sergio R. Caprile committed 2024-05-31 17:45:53 -03:00
1 parent a4e0225c69
commit ab75bfa6e7
11 files changed
+46 -16

No files matched your search

+1 -1
View File
@@ -32,7 +32,7 @@
#define calloc(a, b) mg_calloc(a, b)
#define free(a) vPortFree(a)
#define malloc(a) pvPortMalloc(a)
#define strdup(s) mg_mprintf("%s", s)
#define strdup(s) ((char *) mg_strdup(mg_str(s)).buf)
// Re-route calloc/free to the FreeRTOS's functions, don't use stdlib
static inline void *mg_calloc(size_t cnt, size_t size) {
+1 -1
View File
@@ -22,7 +22,7 @@ extern uint32_t rt_time_get(void);
#include "cmsis_os2.h" // keep this include
#endif
#define strdup(s) mg_mprintf("%s", s)
#define strdup(s) ((char *) mg_strdup(mg_str(s)).buf)
#if defined(__ARMCC_VERSION)
#define mode_t size_t
+1 -2
View File
@@ -5,8 +5,7 @@ void mg_rpc_add(struct mg_rpc **head, struct mg_str method,
void (*fn)(struct mg_rpc_req *), void *fn_data) {
struct mg_rpc *rpc = (struct mg_rpc *) calloc(1, sizeof(*rpc));
if (rpc != NULL) {
rpc->method.buf = mg_mprintf("%.*s", method.len, method.buf);
rpc->method.len = method.len;
rpc->method = mg_strdup(method);
rpc->fn = fn;
rpc->fn_data = fn_data;
rpc->next = *head, *head = rpc;
+14
View File
@@ -23,6 +23,20 @@ int mg_casecmp(const char *s1, const char *s2) {
return diff;
}
struct mg_str mg_strdup(const struct mg_str s) {
struct mg_str r = {NULL, 0};
if (s.len > 0 && s.buf != NULL) {
char *sc = (char *) calloc(1, s.len + 1);
if (sc != NULL) {
memcpy(sc, s.buf, s.len);
sc[s.len] = '\0';
r.buf = sc;
r.len = s.len;
}
}
return r;
}
int mg_strcmp(const struct mg_str str1, const struct mg_str str2) {
size_t i = 0;
while (i < str1.len && i < str2.len) {
+1
View File
@@ -16,6 +16,7 @@ struct mg_str mg_str_n(const char *s, size_t n);
int mg_casecmp(const char *s1, const char *s2);
int mg_strcmp(const struct mg_str str1, const struct mg_str str2);
int mg_strcasecmp(const struct mg_str str1, const struct mg_str str2);
struct mg_str mg_strdup(const struct mg_str s);
bool mg_match(struct mg_str str, struct mg_str pattern, struct mg_str *caps);
bool mg_span(struct mg_str s, struct mg_str *a, struct mg_str *b, char delim);
+1 -2
View File
@@ -1233,8 +1233,7 @@ static int mg_parse_pem(const struct mg_str pem, const struct mg_str label,
const char *c;
struct mg_str caps[5];
if (!mg_match(pem, mg_str("#-----BEGIN #-----#-----END #-----#"), caps)) {
der->buf = mg_mprintf("%.*s", pem.len, pem.buf);
der->len = pem.len;
*der = mg_strdup(pem);
return 0;
}
if (mg_strcmp(caps[1], label) != 0 || mg_strcmp(caps[3], label) != 0) {