mg_file_write -> mg_file_printf

This commit is contained in:
cpq committed 2021-01-26 16:30:44 +00:00
1 parent 01691dafa0
commit 3c851bcfc3
7 files changed
+78 -28

No files matched your search

+2
View File
@@ -156,7 +156,9 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data,
if (d->c->is_resolving) {
d->c->is_resolving = 0;
if (dm.resolved) {
#if MG_ENABLE_LOG
char buf[100];
#endif
dm.addr.port = d->c->peer.port; // Save port
d->c->peer = dm.addr; // Copy resolved address
LOG(LL_DEBUG, ("%lu %s resolved to %s", d->c->id, dm.name,
+24 -10
View File
@@ -29,22 +29,36 @@ char *mg_file_read(const char *path) {
return data;
}
int mg_file_write(const char *path, const char *fmt, ...) {
char tmp[100];
int written = 0;
bool mg_file_write(const char *path, const void *buf, size_t len) {
bool result = false;
FILE *fp;
char tmp[PATH_MAX];
snprintf(tmp, sizeof(tmp), "%s.%d", path, rand());
fp = fopen(tmp, "wb");
if (fp != NULL) {
va_list ap;
va_start(ap, fmt);
written = vfprintf(fp, fmt, ap);
va_end(ap);
result = fwrite(buf, 1, len, fp) == len;
fclose(fp);
remove(path);
rename(tmp, path);
if (result) {
remove(path);
rename(tmp, path);
} else {
remove(tmp);
}
}
return written;
return result;
}
bool mg_file_printf(const char *path, const char *fmt, ...) {
char tmp[256], *buf = tmp;
bool result;
size_t len;
va_list ap;
va_start(ap, fmt);
len = mg_vasprintf(&buf, sizeof(tmp), fmt, ap);
va_end(ap);
result = mg_file_write(path, buf, len);
if (buf != tmp) free(buf);
return result;
}
void mg_random(void *buf, size_t len) {
+2 -1
View File
@@ -5,7 +5,8 @@
char *mg_file_read(const char *path);
size_t mg_file_size(const char *path);
int mg_file_write(const char *path, const char *fmt, ...);
bool mg_file_write(const char *path, const void *buf, size_t len);
bool mg_file_printf(const char *path, const char *fmt, ...);
void mg_random(void *buf, size_t len);
bool mg_globmatch(const char *pattern, int plen, const char *s, int n);
bool mg_next_comma_entry(struct mg_str *s, struct mg_str *k, struct mg_str *v);