From a0f565405ddac6f00b92e175e9e5b1d8b54d546c Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 18 Oct 2022 22:47:40 +0800 Subject: [PATCH] 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