From f0794e561440da795470c6253dd4bd58b6fe5eaa Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 21 Sep 2022 09:35:34 +0800 Subject: [PATCH] update: interface of string list --- include/common/structure.h | 6 ++++-- src/applet/dnsproxy.c | 6 +++--- src/cleardns.c | 28 +++++++++++++++++++++++++--- src/common/json.c | 4 ++-- src/common/structure.c | 36 +++++++++++++++++++++++++++--------- src/common/system.c | 14 ++++++++------ src/loader/loader.c | 18 +++++++++--------- src/utils/process.c | 5 +++-- 8 files changed, 81 insertions(+), 36 deletions(-) diff --git a/include/common/structure.h b/include/common/structure.h index ccc77ba..1268381 100644 --- a/include/common/structure.h +++ b/include/common/structure.h @@ -7,8 +7,10 @@ char** string_list_init(); void string_list_free(char **string_list); char* string_list_dump(char **string_list); uint32_t string_list_len(char **string_list); -char** string_list_update(char **base_list, char **update_list); -char** string_list_append(char **string_list, const char *string); +//char** string_list_update(char **base_list, char **update_list); +//char** string_list_append(char **string_list, const char *string); +void string_list_update(char ***base_list, char **update_list); +void string_list_append(char ***string_list, const char *string); uint32_t** uint32_list_init(); char* uint32_list_dump(uint32_t **int_list); diff --git a/src/applet/dnsproxy.c b/src/applet/dnsproxy.c index 0d676d8..47512a0 100644 --- a/src/applet/dnsproxy.c +++ b/src/applet/dnsproxy.c @@ -11,15 +11,15 @@ 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 - info->primary = string_list_append(info->primary, server); + string_list_append(&info->primary, server); } void dnsproxy_add_fallback(dnsproxy *info, const char *server) { // add fallback dns server - info->fallback = string_list_append(info->fallback, server); + string_list_append(&info->fallback, server); } void dnsproxy_add_bootstrap(dnsproxy *info, const char *server) { // add bootstrap dns server - info->bootstrap = string_list_append(info->bootstrap, server); + string_list_append(&info->bootstrap, server); } void dnsproxy_free(dnsproxy *info) { // free dnsproxy options diff --git a/src/cleardns.c b/src/cleardns.c index 9f1a975..772baec 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -10,6 +10,7 @@ #include "adguard.h" #include "system.h" #include "sundry.h" +#include "structure.h" char* init(int argc, char *argv[]) { // return config file char *config = string_init(CONFIG_FILE); @@ -45,6 +46,27 @@ int main(int argc, char *argv[]) { // ClearDNS service LOG_LEVEL = LOG_DEBUG; log_info("ClearDNS server start (%s)", VERSION); + char **test = string_list_init(); + log_info("len -> %u", string_list_len(test)); + string_list_append(&test, "test1"); + string_list_append(&test, "test2"); + string_list_append(&test, "test3"); + log_info("len -> %u", string_list_len(test)); + + char *temp = string_list_dump(test); + log_info("content -> %s", temp); + free(temp); + + char **add = string_list_init(); + string_list_append(&add, "test4"); + string_list_append(&add, "test5"); + + string_list_update(&test, add); + log_info("len -> %d", string_list_len(test)); + temp = string_list_dump(test); + log_info("content -> %s", temp); + free(temp); + // process *test = process_init("TEST", "lls"); // process *test = process_init("TEST", "ls"); // process_add_arg(test, "-al"); @@ -56,11 +78,11 @@ int main(int argc, char *argv[]) { // ClearDNS service // wait(&status); // return 0; - create_folder(WORK_DIR); +// create_folder(WORK_DIR); // TODO: load assets first - load_config(config_file); - free(config_file); +// load_config(config_file); +// free(config_file); // process_list_init(); // process_list_append(dnsproxy_load("Domestic", loader.domestic, "domestic.json")); diff --git a/src/common/json.c b/src/common/json.c index 623958d..faccd76 100644 --- a/src/common/json.c +++ b/src/common/json.c @@ -92,14 +92,14 @@ char* json_string_value(char* key, cJSON *json) { // json string value -> string char** json_string_list_value(char *key, cJSON *json, char **string_list) { // json string array -> string list if (cJSON_IsString(json)) { - string_list = string_list_append(string_list, json->valuestring); + string_list_append(&string_list, json->valuestring); } else if (cJSON_IsArray(json)) { json = json->child; while (json != NULL) { if (!cJSON_IsString(json)) { log_fatal("`%s` must be string array", key); } - string_list = string_list_append(string_list, json->valuestring); + string_list_append(&string_list, json->valuestring); json = json->next; // next key } } else if (!cJSON_IsNull(json)) { // allow null -> empty string list diff --git a/src/common/structure.c b/src/common/structure.c index 90b89cb..d0bf9b2 100644 --- a/src/common/structure.c +++ b/src/common/structure.c @@ -4,6 +4,7 @@ #include #include "sundry.h" #include "constant.h" +#include "structure.h" char** string_list_init() { // init string list char **string_list = (char **)malloc(sizeof(char *)); @@ -18,19 +19,36 @@ uint32_t string_list_len(char **string_list) { // get len of string list } // TODO: use char *** with void return -char** string_list_append(char **string_list, const char *string) { // add new string at the end of list - 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 + 1] = NULL; // list end sign - return string_list; +//char** string_list_append(char **string_list, const char *string) { // add new string at the end of list +// 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 + 1] = NULL; // list end sign +// return 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 + 1] = NULL; // list end sign +// return string_list; } -char** string_list_update(char **base_list, char **update_list) { // combine two string list +//char** string_list_update(char **base_list, char **update_list) { // combine two string list +// for (char **string = update_list; *string != NULL; ++string) { +//// base_list = string_list_append(base_list, *string); +// string_list_append(&base_list, *string); +// } +// return base_list; +//} + +void string_list_update(char ***base_list, char **update_list) { // combine two string list for (char **string = update_list; *string != NULL; ++string) { - base_list = string_list_append(base_list, *string); +// base_list = string_list_append(base_list, *string); + string_list_append(base_list, *string); } - return base_list; +// return base_list; } void string_list_free(char **string_list) { // free string list diff --git a/src/common/system.c b/src/common/system.c index 8799993..703695c 100644 --- a/src/common/system.c +++ b/src/common/system.c @@ -7,16 +7,17 @@ #include "sundry.h" #include "constant.h" -int run_command(const char *command) { // running command with system shell +int run_command(const char *command) { // running command under system shell log_debug("Run command -> `%s`", command); - int ret = system(command) / 256; - if (ret != 0) { - log_warn("Command `%s` return non-zero code -> %d", command, ret); + int ret_code = system(command) / 256; + if (ret_code != 0) { + log_warn("Command `%s` return non-zero code %d", command, ret_code); } - return ret; + return ret_code; } -void remove_file(const char *file) { +void remove_file(const char *file) { // delete file + // TODO: use system interface (not `rm` command) char *remove_cmd = string_join("rm -f ", file); run_command(remove_cmd); free(remove_cmd); @@ -32,6 +33,7 @@ void create_folder(const char *folder) { // create folder return; } log_debug("Create folder -> %s", folder); + // TODO: use system command (not `mkdir` command) char *command = string_join("mkdir -p ", folder); system(command); free(command); diff --git a/src/loader/loader.c b/src/loader/loader.c index 8b58ad4..1c3ae82 100644 --- a/src/loader/loader.c +++ b/src/loader/loader.c @@ -35,9 +35,9 @@ dnsproxy* load_domestic(cleardns_config *config) { domestic->cache = config->cache.size; domestic->optimistic = config->cache.optimistic; } - domestic->bootstrap = string_list_update(domestic->bootstrap, config->domestic.bootstrap); - domestic->fallback = string_list_update(domestic->fallback, config->domestic.fallback); - domestic->primary = string_list_update(domestic->primary, config->domestic.primary); + string_list_update(&domestic->bootstrap, config->domestic.bootstrap); + string_list_update(&domestic->fallback, config->domestic.fallback); + string_list_update(&domestic->primary, config->domestic.primary); return domestic; } @@ -49,9 +49,9 @@ dnsproxy* load_foreign(cleardns_config *config) { foreign->cache = config->cache.size; foreign->optimistic = config->cache.optimistic; } - foreign->bootstrap = string_list_update(foreign->bootstrap, config->foreign.bootstrap); - foreign->fallback = string_list_update(foreign->fallback, config->foreign.fallback); - foreign->primary = string_list_update(foreign->primary, config->foreign.primary); + string_list_update(&foreign->bootstrap, config->foreign.bootstrap); + string_list_update(&foreign->fallback, config->foreign.fallback); + string_list_update(&foreign->primary, config->foreign.primary); return foreign; } @@ -119,8 +119,8 @@ void load_config(const char *config_file) { config_parser(config, config_file); // configure parser // insert code (remove after test) - config->assets.update_file = string_list_append(config->assets.update_file, "geoip.dat"); - config->assets.update_url = string_list_append(config->assets.update_url, "https://test.net/geoip.dat"); + string_list_append(&config->assets.update_file, "geoip.dat"); + string_list_append(&config->assets.update_url, "https://test.net/geoip.dat"); config_dump(config); log_info("Loading configure options"); @@ -136,6 +136,6 @@ void load_config(const char *config_file) { loader.filter = load_filter(config); log_debug("Filter options parser success"); loader.script = string_list_init(); - loader.script = string_list_update(loader.script, config->script); + string_list_update(&loader.script, config->script); config_free(config); } diff --git a/src/utils/process.c b/src/utils/process.c index e1496a2..9e1d61e 100644 --- a/src/utils/process.c +++ b/src/utils/process.c @@ -15,7 +15,8 @@ process **process_list; process* process_init(const char *caption, const char *bin) { // init process struct process *proc = (process *)malloc(sizeof(process)); proc->name = string_init(caption); // process caption - proc->cmd = string_list_append(string_list_init(), bin); // argv[0] normally be process file name + 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 proc->cwd = WORK_DIR; // current working directory proc->is_group = FALSE; // create new process group @@ -23,7 +24,7 @@ process* process_init(const char *caption, const char *bin) { // init process st } void process_add_arg(process *proc, const char *arg) { // add argument for process - proc->cmd = string_list_append(proc->cmd, arg); + string_list_append(&proc->cmd, arg); } void process_list_init() { // init process list