From a0f565405ddac6f00b92e175e9e5b1d8b54d546c Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 18 Oct 2022 22:47:40 +0800 Subject: [PATCH 1/5] update: `strdup` function --- include/common/sundry.h | 1 - src/applet/adguard.c | 4 ++-- src/applet/crontab.c | 3 ++- src/applet/overture.c | 9 +++++---- src/cleardns.c | 6 +++--- src/common/json.c | 2 +- src/common/structure.c | 7 +++---- src/common/sundry.c | 6 +----- src/common/system.c | 1 - src/loader/config.c | 7 ++++--- src/loader/default.c | 4 ++-- src/loader/loader.c | 18 +++++++++--------- src/utils/process.c | 5 +++-- 13 files changed, 35 insertions(+), 38 deletions(-) diff --git a/include/common/sundry.h b/include/common/sundry.h index c10542d..b841a91 100644 --- a/include/common/sundry.h +++ b/include/common/sundry.h @@ -4,7 +4,6 @@ #include char* show_bool(uint8_t value); -char* string_init(const char *str); char* uint32_to_string(uint32_t number); char* string_join(const char *base, const char *add); void string_list_debug(char *describe, char **string_list); diff --git a/src/applet/adguard.c b/src/applet/adguard.c index 9deaa3a..328d114 100644 --- a/src/applet/adguard.c +++ b/src/applet/adguard.c @@ -25,8 +25,8 @@ adguard* adguard_init() { // init adguard options info->dns_port = DNS_PORT; info->web_port = ADGUARD_PORT; info->upstream = string_join("127.0.0.1:", port_str); // default upstream - info->username = string_init(ADGUARD_USER); - info->password = string_init(ADGUARD_PASSWD); + info->username = strdup(ADGUARD_USER); + info->password = strdup(ADGUARD_PASSWD); free(port_str); return info; } diff --git a/src/applet/crontab.c b/src/applet/crontab.c index 515f1c5..126d1f0 100644 --- a/src/applet/crontab.c +++ b/src/applet/crontab.c @@ -1,4 +1,5 @@ #include +#include #include #include "logger.h" #include "sundry.h" @@ -17,7 +18,7 @@ void crontab_free(crontab *info) { // free crontab options crontab* crontab_init() { // init crontab options crontab *info = (crontab *)malloc(sizeof(crontab)); info->debug = FALSE; - info->cron = string_init(UPDATE_CRON); + info->cron = strdup(UPDATE_CRON); return info; } diff --git a/src/applet/overture.c b/src/applet/overture.c index 393478e..f9024b8 100644 --- a/src/applet/overture.c +++ b/src/applet/overture.c @@ -1,4 +1,5 @@ #include +#include #include "cJSON.h" #include "logger.h" #include "sundry.h" @@ -30,10 +31,10 @@ overture* overture_init() { // init overture options info->foreign_port = FOREIGN_PORT; info->domestic_port = DOMESTIC_PORT; info->reject_type = uint32_list_init(); - info->foreign_ip_file = string_init("/dev/null"); - info->domestic_ip_file = string_init("/dev/null"); - info->foreign_domain_file = string_init("/dev/null"); - info->domestic_domain_file = string_init("/dev/null"); + info->foreign_ip_file = strdup("/dev/null"); + info->domestic_ip_file = strdup("/dev/null"); + info->foreign_domain_file = strdup("/dev/null"); + info->domestic_domain_file = strdup("/dev/null"); return info; } diff --git a/src/cleardns.c b/src/cleardns.c index 8ca9c17..3ad366a 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -22,13 +22,13 @@ struct { } settings; void init(int argc, char *argv[]) { // return config file - settings.config = string_init(CONFIG_FILE); + settings.config = strdup(CONFIG_FILE); settings.debug = FALSE; settings.verbose = FALSE; if (getenv("CONFIG") != NULL) { free(settings.config); - settings.config = string_init(getenv("CONFIG")); + settings.config = strdup(getenv("CONFIG")); } if (getenv("DEBUG") != NULL && !strcmp(getenv("DEBUG"), "TRUE")) { settings.debug = TRUE; @@ -55,7 +55,7 @@ void init(int argc, char *argv[]) { // return config file exit(1); } free(settings.config); - settings.config = string_init(argv[++i]); // use custom config file + settings.config = strdup(argv[++i]); // use custom config file } } log_debug("Config file -> %s", settings.config); diff --git a/src/common/json.c b/src/common/json.c index 18c0aa2..7d308c2 100644 --- a/src/common/json.c +++ b/src/common/json.c @@ -87,7 +87,7 @@ char* json_string_value(char* caption, cJSON *json) { // json string value -> st if (!cJSON_IsString(json)) { log_fatal("`%s` must be string", caption); } - return string_init(json->valuestring); + return strdup(json->valuestring); } char** json_string_list_value(char *caption, cJSON *json, char **string_list) { // json string array diff --git a/src/common/structure.c b/src/common/structure.c index 4f7f526..a8b0e8a 100644 --- a/src/common/structure.c +++ b/src/common/structure.c @@ -2,7 +2,6 @@ #include #include #include -#include "sundry.h" #include "structure.h" char** string_list_init() { // init string list @@ -20,7 +19,7 @@ uint32_t string_list_len(char **string_list) { // get len of string list void string_list_append(char ***string_list, const char *string) { uint32_t len = string_list_len(*string_list); *string_list = (char **)realloc(*string_list, sizeof(char *) * (len + 2)); // extend string list - (*string_list)[len] = string_init(string); + (*string_list)[len] = strdup(string); (*string_list)[len + 1] = NULL; // list end sign } @@ -39,7 +38,7 @@ void string_list_free(char **string_list) { // free string list char* string_list_dump(char **string_list) { // ['a', 'b', 'c', ...] if (string_list_len(string_list) == 0) { - return string_init("[]"); // empty string list + return strdup("[]"); // empty string list } char *string_ret = (char *)malloc(2); strcpy(string_ret, "["); @@ -86,7 +85,7 @@ void uint32_list_free(uint32_t **uint32_list) { // free uint32 list char* uint32_list_dump(uint32_t **uint32_list) { // [1, 2, 3, ...] if (uint32_list_len(uint32_list) == 0) { - return string_init("[]"); // empty int list + return strdup("[]"); // empty int list } char uint32_str[12]; char *string_ret = (char *)malloc(2); diff --git a/src/common/sundry.c b/src/common/sundry.c index 1f0edcf..bb6ff2c 100644 --- a/src/common/sundry.c +++ b/src/common/sundry.c @@ -14,10 +14,6 @@ char* show_bool(uint8_t value) { // return `true` or `false` return "false"; } -char* string_init(const char *str) { // new string - return strcpy((char *)malloc(strlen(str) + 1), str); -} - char* string_join(const char *base, const char *add) { // combine string char *ret = (char *)malloc(strlen(base) + strlen(add) + 1); return strcat(strcpy(ret, base), add); @@ -26,7 +22,7 @@ char* string_join(const char *base, const char *add) { // combine string char* uint32_to_string(uint32_t number) { // convert uint32 -> string char to_str[11]; // MAX_LEN(uint32) -> 4294967296(10-bytes) sprintf(to_str, "%u", number); - return string_init(to_str); + return strdup(to_str); } void string_list_debug(char *describe, char **string_list) { // show string list in debug log diff --git a/src/common/system.c b/src/common/system.c index 3c32b62..f59318c 100644 --- a/src/common/system.c +++ b/src/common/system.c @@ -4,7 +4,6 @@ #include #include #include "logger.h" -#include "sundry.h" #include "system.h" #include "constant.h" diff --git a/src/loader/config.c b/src/loader/config.c index 483a585..e5b3117 100644 --- a/src/loader/config.c +++ b/src/loader/config.c @@ -1,4 +1,5 @@ #include +#include #include "config.h" #include "logger.h" #include "sundry.h" @@ -33,11 +34,11 @@ cleardns_config* config_init() { // init config struct of cleardns config->adguard.port = ADGUARD_PORT; config->adguard.enable = TRUE; - config->adguard.username = string_init(ADGUARD_USER); - config->adguard.password = string_init(ADGUARD_PASSWD); + config->adguard.username = strdup(ADGUARD_USER); + config->adguard.password = strdup(ADGUARD_PASSWD); config->assets.disable = FALSE; - config->assets.cron = string_init(UPDATE_CRON); + config->assets.cron = strdup(UPDATE_CRON); config->assets.update_file = string_list_init(); config->assets.update_url = string_list_init(); diff --git a/src/loader/default.c b/src/loader/default.c index d827ac3..2480f9b 100644 --- a/src/loader/default.c +++ b/src/loader/default.c @@ -1,7 +1,7 @@ #include +#include #include "json.h" #include "logger.h" -#include "sundry.h" #include "system.h" #define DEFAULT_CONFIG "\ @@ -63,7 +63,7 @@ void load_default_config(const char *config_file) { remove_file(temp_file); } if (config_content == NULL) { - config_content = string_init(DEFAULT_CONFIG); + config_content = strdup(DEFAULT_CONFIG); } save_file(config_file, config_content); free(config_content); diff --git a/src/loader/loader.c b/src/loader/loader.c index 5e375ea..6914654 100644 --- a/src/loader/loader.c +++ b/src/loader/loader.c @@ -1,5 +1,5 @@ -#include #include +#include #include "config.h" #include "loader.h" #include "logger.h" @@ -63,14 +63,14 @@ overture* load_diverter(cleardns_config *config) { if (string_list_len(config->ttl)) { free(diverter->ttl_file); - diverter->ttl_file = string_init(ASSET_TTL); + diverter->ttl_file = strdup(ASSET_TTL); char *ttl_file = string_join(WORK_DIR, ASSET_TTL); save_string_list(ttl_file, config->ttl); free(ttl_file); } if (string_list_len(config->hosts)) { free(diverter->host_file); - diverter->host_file = string_init(ASSET_HOSTS); + diverter->host_file = strdup(ASSET_HOSTS); char *hosts_file = string_join(WORK_DIR, ASSET_HOSTS); save_string_list(hosts_file, config->hosts); free(hosts_file); @@ -79,9 +79,9 @@ overture* load_diverter(cleardns_config *config) { free(diverter->domestic_ip_file); free(diverter->foreign_domain_file); free(diverter->domestic_domain_file); - diverter->domestic_ip_file = string_init(ASSET_CHINA_IP); - diverter->foreign_domain_file = string_init(ASSET_GFW_LIST); - diverter->domestic_domain_file = string_init(ASSET_CHINA_LIST); + diverter->domestic_ip_file = strdup(ASSET_CHINA_IP); + diverter->foreign_domain_file = strdup(ASSET_GFW_LIST); + diverter->domestic_domain_file = strdup(ASSET_CHINA_LIST); char *gfwlist = string_join(WORK_DIR, ASSET_GFW_LIST); char *china_ip = string_join(WORK_DIR, ASSET_CHINA_IP); @@ -108,8 +108,8 @@ adguard* load_filter(cleardns_config *config) { adguard *filter = adguard_init(); filter->dns_port = config->port; filter->web_port = config->adguard.port; - filter->username = string_init(config->adguard.username); - filter->password = string_init(config->adguard.password); + filter->username = strdup(config->adguard.username); + filter->password = strdup(config->adguard.password); char *diverter_port = uint32_to_string(config->diverter.port); filter->upstream = string_join("127.0.0.1:", diverter_port); free(diverter_port); @@ -121,7 +121,7 @@ crontab* load_crond(cleardns_config *config) { return NULL; // disable crond } crontab *crond = crontab_init(); - crond->cron = string_init(config->assets.cron); + crond->cron = strdup(config->assets.cron); return crond; } diff --git a/src/utils/process.c b/src/utils/process.c index ccb1834..f4c2ed4 100644 --- a/src/utils/process.c +++ b/src/utils/process.c @@ -3,6 +3,7 @@ #endif #include +#include #include #include #include @@ -28,7 +29,7 @@ void process_exec(process *proc); process* process_init(const char *caption, const char *bin) { // init process struct process *proc = (process *)malloc(sizeof(process)); proc->pid = 0; // process not running - proc->name = string_init(caption); // process caption + proc->name = strdup(caption); // process caption proc->cmd = string_list_init(); string_list_append(&proc->cmd, bin); // argv[0] normally be process file name proc->env = string_list_init(); // empty environment variable @@ -116,7 +117,7 @@ char* get_exit_msg(int status) { // get why the child process death free(exit_sig); return exit_msg; } - return string_init("Unknown reason"); + return strdup("Unknown reason"); } void server_exit(int exit_code) { // kill sub process and exit From 36142ef5c543cd8a033bca3cea51db0d6fbdabe5 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 19 Oct 2022 10:04:21 +0800 Subject: [PATCH 2/5] feat: string load demo --- include/common/sundry.h | 1 + src/cleardns.c | 10 +++++++--- src/common/sundry.c | 11 ++++++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/include/common/sundry.h b/include/common/sundry.h index b841a91..f12a2a5 100644 --- a/include/common/sundry.h +++ b/include/common/sundry.h @@ -4,6 +4,7 @@ #include char* show_bool(uint8_t value); +char* string_load(const char *fmt, ...); char* uint32_to_string(uint32_t number); char* string_join(const char *base, const char *add); void string_list_debug(char *describe, char **string_list); diff --git a/src/cleardns.c b/src/cleardns.c index 3ad366a..e1f4f0e 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -115,8 +115,12 @@ void cleardns() { // cleardns service } int main(int argc, char *argv[]) { - init(argc, argv); - log_info("ClearDNS server start (%s)", VERSION); - cleardns(); +// init(argc, argv); +// log_info("ClearDNS server start (%s)", VERSION); +// cleardns(); + + char *test = string_load("`%s` -> %d", "test", 2333); + printf("%s\n", test); + return 0; } diff --git a/src/common/sundry.c b/src/common/sundry.c index bb6ff2c..ab5e9de 100644 --- a/src/common/sundry.c +++ b/src/common/sundry.c @@ -1,8 +1,9 @@ #include +#include #include #include -#include #include +#include #include "logger.h" #include "constant.h" #include "structure.h" @@ -19,6 +20,14 @@ char* string_join(const char *base, const char *add) { // combine string return strcat(strcpy(ret, base), add); } +char* string_load(const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + char *buf = NULL; + vasprintf(&buf, fmt, ap); + return buf; +} + char* uint32_to_string(uint32_t number) { // convert uint32 -> string char to_str[11]; // MAX_LEN(uint32) -> 4294967296(10-bytes) sprintf(to_str, "%u", number); From 7b9e1781c3232e5fd8e55c00404e85f4ae0bb576 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 19 Oct 2022 10:17:50 +0800 Subject: [PATCH 3/5] update: using string load function --- src/cleardns.c | 10 +++------- src/common/json.c | 4 +--- src/common/structure.c | 2 +- src/common/system.c | 7 +++---- src/utils/assets.c | 4 +--- 5 files changed, 9 insertions(+), 18 deletions(-) diff --git a/src/cleardns.c b/src/cleardns.c index e1f4f0e..3ad366a 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -115,12 +115,8 @@ void cleardns() { // cleardns service } int main(int argc, char *argv[]) { -// init(argc, argv); -// log_info("ClearDNS server start (%s)", VERSION); -// cleardns(); - - char *test = string_load("`%s` -> %d", "test", 2333); - printf("%s\n", test); - + init(argc, argv); + log_info("ClearDNS server start (%s)", VERSION); + cleardns(); return 0; } diff --git a/src/common/json.c b/src/common/json.c index 7d308c2..95f33e7 100644 --- a/src/common/json.c +++ b/src/common/json.c @@ -1,4 +1,3 @@ -#include #include #include #include "cJSON.h" @@ -27,8 +26,7 @@ char* to_json(const char *file) { // convert JSON / TOML / YAML to json format ( flag[8] = '\0'; char *output_file = string_join("/tmp/to-json-", flag); - char *to_json_cmd = (char *)malloc(strlen(file) + strlen(output_file) + 11); - sprintf(to_json_cmd, "toJSON %s > %s", file, output_file); + char *to_json_cmd = string_load("toJSON %s > %s", file, output_file); int to_json_ret = run_command(to_json_cmd); free(to_json_cmd); diff --git a/src/common/structure.c b/src/common/structure.c index a8b0e8a..2698e86 100644 --- a/src/common/structure.c +++ b/src/common/structure.c @@ -92,7 +92,7 @@ char* uint32_list_dump(uint32_t **uint32_list) { // [1, 2, 3, ...] strcpy(string_ret, "["); for (uint32_t **number = uint32_list; *number != NULL; ++number) { sprintf(uint32_str, "%u", **number); - string_ret = (char*)realloc(string_ret, strlen(string_ret) + 15); + string_ret = (char *)realloc(string_ret, strlen(string_ret) + 15); string_ret = strcat(strcat(string_ret, uint32_str), ", "); } string_ret[strlen(string_ret) - 2] = '\0'; diff --git a/src/common/system.c b/src/common/system.c index f59318c..476d579 100644 --- a/src/common/system.c +++ b/src/common/system.c @@ -4,6 +4,7 @@ #include #include #include "logger.h" +#include "sundry.h" #include "system.h" #include "constant.h" @@ -65,8 +66,7 @@ void save_file(const char *file, const char *content) { // save content into fil } void file_append(const char *base_file, const char *append_file) { // append_file >> base_file - char *append_cmd = (char *)malloc(strlen(base_file) + strlen(append_file) + 9); - sprintf(append_cmd, "cat %s >> %s", append_file, base_file); + char *append_cmd = string_load("cat %s >> %s", append_file, base_file); run_command(append_cmd); free(append_cmd); } @@ -108,8 +108,7 @@ void save_string_list(const char *file, char **string_list) { // save string lis void download_file(const char *file, const char *url) { // download file log_debug("Download file `%s` -> %s", file, url); - char *download_cmd = (char *)malloc(strlen(file) + strlen(url) + 15); - sprintf(download_cmd, "wget -T 8 -O %s %s", file, url); + char *download_cmd = string_load("wget -T 8 -O %s %s", file, url); if (run_command(download_cmd)) { log_warn("File `%s` download failed", url); } diff --git a/src/utils/assets.c b/src/utils/assets.c index 6b9bb89..9f31f42 100644 --- a/src/utils/assets.c +++ b/src/utils/assets.c @@ -1,4 +1,3 @@ -#include #include #include #include @@ -81,8 +80,7 @@ void extract(const char *file) { // extract one asset file from `.tar.xz` file } free(output_file); - char *extract_cmd = (char *)malloc(strlen(ASSETS_PKG) + strlen(file) + strlen(ASSETS_DIR) + 13); - sprintf(extract_cmd, "tar xf %s %s -C %s", ASSETS_PKG, file, ASSETS_DIR); + char *extract_cmd = string_load("tar xf %s %s -C %s", ASSETS_PKG, file, ASSETS_DIR); if (run_command(extract_cmd)) { log_warn("Extract asset `%s` failed", file); } else { From ebf6821d22bdb521b6738f84d496c953bd5e23c9 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 19 Oct 2022 10:36:01 +0800 Subject: [PATCH 4/5] remove: unused function --- include/applet/dnsproxy.h | 4 ---- src/applet/dnsproxy.c | 12 ------------ 2 files changed, 16 deletions(-) diff --git a/include/applet/dnsproxy.h b/include/applet/dnsproxy.h index b112689..81a26e7 100644 --- a/include/applet/dnsproxy.h +++ b/include/applet/dnsproxy.h @@ -20,8 +20,4 @@ void dnsproxy_free(dnsproxy *info); dnsproxy* dnsproxy_init(uint16_t port); process* dnsproxy_load(const char *caption, dnsproxy *info, const char *file); -void dnsproxy_add_primary(dnsproxy *info, const char *server); -void dnsproxy_add_fallback(dnsproxy *info, const char *server); -void dnsproxy_add_bootstrap(dnsproxy *info, const char *server); - #endif diff --git a/src/applet/dnsproxy.c b/src/applet/dnsproxy.c index ca2f480..593b4b2 100644 --- a/src/applet/dnsproxy.c +++ b/src/applet/dnsproxy.c @@ -10,18 +10,6 @@ char* dnsproxy_config(dnsproxy *info); void dnsproxy_dump(const char *caption, dnsproxy *info); -void dnsproxy_add_primary(dnsproxy *info, const char *server) { // add primary dns server - string_list_append(&info->primary, server); -} - -void dnsproxy_add_fallback(dnsproxy *info, const char *server) { // add fallback dns server - string_list_append(&info->fallback, server); -} - -void dnsproxy_add_bootstrap(dnsproxy *info, const char *server) { // add bootstrap dns server - string_list_append(&info->bootstrap, server); -} - void dnsproxy_free(dnsproxy *info) { // free dnsproxy options string_list_free(info->bootstrap); string_list_free(info->fallback); From e16fa3b0ef4d274737671d8b375092ae42996cce Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 19 Oct 2022 10:43:27 +0800 Subject: [PATCH 5/5] fix: define `_GNU_SOURCE` flag --- src/common/sundry.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/common/sundry.c b/src/common/sundry.c index ab5e9de..7191199 100644 --- a/src/common/sundry.c +++ b/src/common/sundry.c @@ -1,3 +1,7 @@ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE // NOLINT +#endif + #include #include #include