Browse Source

update: `strdup` function

dev
Dnomd343 2 years ago
parent
commit
a0f565405d
  1. 1
      include/common/sundry.h
  2. 4
      src/applet/adguard.c
  3. 3
      src/applet/crontab.c
  4. 9
      src/applet/overture.c
  5. 6
      src/cleardns.c
  6. 2
      src/common/json.c
  7. 7
      src/common/structure.c
  8. 6
      src/common/sundry.c
  9. 1
      src/common/system.c
  10. 7
      src/loader/config.c
  11. 4
      src/loader/default.c
  12. 18
      src/loader/loader.c
  13. 5
      src/utils/process.c

1
include/common/sundry.h

@ -4,7 +4,6 @@
#include <stdint.h> #include <stdint.h>
char* show_bool(uint8_t value); char* show_bool(uint8_t value);
char* string_init(const char *str);
char* uint32_to_string(uint32_t number); char* uint32_to_string(uint32_t number);
char* string_join(const char *base, const char *add); char* string_join(const char *base, const char *add);
void string_list_debug(char *describe, char **string_list); void string_list_debug(char *describe, char **string_list);

4
src/applet/adguard.c

@ -25,8 +25,8 @@ adguard* adguard_init() { // init adguard options
info->dns_port = DNS_PORT; info->dns_port = DNS_PORT;
info->web_port = ADGUARD_PORT; info->web_port = ADGUARD_PORT;
info->upstream = string_join("127.0.0.1:", port_str); // default upstream info->upstream = string_join("127.0.0.1:", port_str); // default upstream
info->username = string_init(ADGUARD_USER); info->username = strdup(ADGUARD_USER);
info->password = string_init(ADGUARD_PASSWD); info->password = strdup(ADGUARD_PASSWD);
free(port_str); free(port_str);
return info; return info;
} }

3
src/applet/crontab.c

@ -1,4 +1,5 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include "logger.h" #include "logger.h"
#include "sundry.h" #include "sundry.h"
@ -17,7 +18,7 @@ void crontab_free(crontab *info) { // free crontab options
crontab* crontab_init() { // init crontab options crontab* crontab_init() { // init crontab options
crontab *info = (crontab *)malloc(sizeof(crontab)); crontab *info = (crontab *)malloc(sizeof(crontab));
info->debug = FALSE; info->debug = FALSE;
info->cron = string_init(UPDATE_CRON); info->cron = strdup(UPDATE_CRON);
return info; return info;
} }

9
src/applet/overture.c

@ -1,4 +1,5 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "cJSON.h" #include "cJSON.h"
#include "logger.h" #include "logger.h"
#include "sundry.h" #include "sundry.h"
@ -30,10 +31,10 @@ overture* overture_init() { // init overture options
info->foreign_port = FOREIGN_PORT; info->foreign_port = FOREIGN_PORT;
info->domestic_port = DOMESTIC_PORT; info->domestic_port = DOMESTIC_PORT;
info->reject_type = uint32_list_init(); info->reject_type = uint32_list_init();
info->foreign_ip_file = string_init("/dev/null"); info->foreign_ip_file = strdup("/dev/null");
info->domestic_ip_file = string_init("/dev/null"); info->domestic_ip_file = strdup("/dev/null");
info->foreign_domain_file = string_init("/dev/null"); info->foreign_domain_file = strdup("/dev/null");
info->domestic_domain_file = string_init("/dev/null"); info->domestic_domain_file = strdup("/dev/null");
return info; return info;
} }

6
src/cleardns.c

@ -22,13 +22,13 @@ struct {
} settings; } settings;
void init(int argc, char *argv[]) { // return config file void init(int argc, char *argv[]) { // return config file
settings.config = string_init(CONFIG_FILE); settings.config = strdup(CONFIG_FILE);
settings.debug = FALSE; settings.debug = FALSE;
settings.verbose = FALSE; settings.verbose = FALSE;
if (getenv("CONFIG") != NULL) { if (getenv("CONFIG") != NULL) {
free(settings.config); free(settings.config);
settings.config = string_init(getenv("CONFIG")); settings.config = strdup(getenv("CONFIG"));
} }
if (getenv("DEBUG") != NULL && !strcmp(getenv("DEBUG"), "TRUE")) { if (getenv("DEBUG") != NULL && !strcmp(getenv("DEBUG"), "TRUE")) {
settings.debug = TRUE; settings.debug = TRUE;
@ -55,7 +55,7 @@ void init(int argc, char *argv[]) { // return config file
exit(1); exit(1);
} }
free(settings.config); 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); log_debug("Config file -> %s", settings.config);

2
src/common/json.c

@ -87,7 +87,7 @@ char* json_string_value(char* caption, cJSON *json) { // json string value -> st
if (!cJSON_IsString(json)) { if (!cJSON_IsString(json)) {
log_fatal("`%s` must be string", caption); 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 char** json_string_list_value(char *caption, cJSON *json, char **string_list) { // json string array

7
src/common/structure.c

@ -2,7 +2,6 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "sundry.h"
#include "structure.h" #include "structure.h"
char** string_list_init() { // init string list 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) { void string_list_append(char ***string_list, const char *string) {
uint32_t len = string_list_len(*string_list); uint32_t len = string_list_len(*string_list);
*string_list = (char **)realloc(*string_list, sizeof(char *) * (len + 2)); // extend 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 (*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', ...] char* string_list_dump(char **string_list) { // ['a', 'b', 'c', ...]
if (string_list_len(string_list) == 0) { if (string_list_len(string_list) == 0) {
return string_init("[]"); // empty string list return strdup("[]"); // empty string list
} }
char *string_ret = (char *)malloc(2); char *string_ret = (char *)malloc(2);
strcpy(string_ret, "["); 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, ...] char* uint32_list_dump(uint32_t **uint32_list) { // [1, 2, 3, ...]
if (uint32_list_len(uint32_list) == 0) { if (uint32_list_len(uint32_list) == 0) {
return string_init("[]"); // empty int list return strdup("[]"); // empty int list
} }
char uint32_str[12]; char uint32_str[12];
char *string_ret = (char *)malloc(2); char *string_ret = (char *)malloc(2);

6
src/common/sundry.c

@ -14,10 +14,6 @@ char* show_bool(uint8_t value) { // return `true` or `false`
return "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* string_join(const char *base, const char *add) { // combine string
char *ret = (char *)malloc(strlen(base) + strlen(add) + 1); char *ret = (char *)malloc(strlen(base) + strlen(add) + 1);
return strcat(strcpy(ret, base), add); 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* uint32_to_string(uint32_t number) { // convert uint32 -> string
char to_str[11]; // MAX_LEN(uint32) -> 4294967296(10-bytes) char to_str[11]; // MAX_LEN(uint32) -> 4294967296(10-bytes)
sprintf(to_str, "%u", number); 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 void string_list_debug(char *describe, char **string_list) { // show string list in debug log

1
src/common/system.c

@ -4,7 +4,6 @@
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "logger.h" #include "logger.h"
#include "sundry.h"
#include "system.h" #include "system.h"
#include "constant.h" #include "constant.h"

7
src/loader/config.c

@ -1,4 +1,5 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "config.h" #include "config.h"
#include "logger.h" #include "logger.h"
#include "sundry.h" #include "sundry.h"
@ -33,11 +34,11 @@ cleardns_config* config_init() { // init config struct of cleardns
config->adguard.port = ADGUARD_PORT; config->adguard.port = ADGUARD_PORT;
config->adguard.enable = TRUE; config->adguard.enable = TRUE;
config->adguard.username = string_init(ADGUARD_USER); config->adguard.username = strdup(ADGUARD_USER);
config->adguard.password = string_init(ADGUARD_PASSWD); config->adguard.password = strdup(ADGUARD_PASSWD);
config->assets.disable = FALSE; 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_file = string_list_init();
config->assets.update_url = string_list_init(); config->assets.update_url = string_list_init();

4
src/loader/default.c

@ -1,7 +1,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "json.h" #include "json.h"
#include "logger.h" #include "logger.h"
#include "sundry.h"
#include "system.h" #include "system.h"
#define DEFAULT_CONFIG "\ #define DEFAULT_CONFIG "\
@ -63,7 +63,7 @@ void load_default_config(const char *config_file) {
remove_file(temp_file); remove_file(temp_file);
} }
if (config_content == NULL) { if (config_content == NULL) {
config_content = string_init(DEFAULT_CONFIG); config_content = strdup(DEFAULT_CONFIG);
} }
save_file(config_file, config_content); save_file(config_file, config_content);
free(config_content); free(config_content);

18
src/loader/loader.c

@ -1,5 +1,5 @@
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "config.h" #include "config.h"
#include "loader.h" #include "loader.h"
#include "logger.h" #include "logger.h"
@ -63,14 +63,14 @@ overture* load_diverter(cleardns_config *config) {
if (string_list_len(config->ttl)) { if (string_list_len(config->ttl)) {
free(diverter->ttl_file); 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); char *ttl_file = string_join(WORK_DIR, ASSET_TTL);
save_string_list(ttl_file, config->ttl); save_string_list(ttl_file, config->ttl);
free(ttl_file); free(ttl_file);
} }
if (string_list_len(config->hosts)) { if (string_list_len(config->hosts)) {
free(diverter->host_file); 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); char *hosts_file = string_join(WORK_DIR, ASSET_HOSTS);
save_string_list(hosts_file, config->hosts); save_string_list(hosts_file, config->hosts);
free(hosts_file); free(hosts_file);
@ -79,9 +79,9 @@ overture* load_diverter(cleardns_config *config) {
free(diverter->domestic_ip_file); free(diverter->domestic_ip_file);
free(diverter->foreign_domain_file); free(diverter->foreign_domain_file);
free(diverter->domestic_domain_file); free(diverter->domestic_domain_file);
diverter->domestic_ip_file = string_init(ASSET_CHINA_IP); diverter->domestic_ip_file = strdup(ASSET_CHINA_IP);
diverter->foreign_domain_file = string_init(ASSET_GFW_LIST); diverter->foreign_domain_file = strdup(ASSET_GFW_LIST);
diverter->domestic_domain_file = string_init(ASSET_CHINA_LIST); diverter->domestic_domain_file = strdup(ASSET_CHINA_LIST);
char *gfwlist = string_join(WORK_DIR, ASSET_GFW_LIST); char *gfwlist = string_join(WORK_DIR, ASSET_GFW_LIST);
char *china_ip = string_join(WORK_DIR, ASSET_CHINA_IP); char *china_ip = string_join(WORK_DIR, ASSET_CHINA_IP);
@ -108,8 +108,8 @@ adguard* load_filter(cleardns_config *config) {
adguard *filter = adguard_init(); adguard *filter = adguard_init();
filter->dns_port = config->port; filter->dns_port = config->port;
filter->web_port = config->adguard.port; filter->web_port = config->adguard.port;
filter->username = string_init(config->adguard.username); filter->username = strdup(config->adguard.username);
filter->password = string_init(config->adguard.password); filter->password = strdup(config->adguard.password);
char *diverter_port = uint32_to_string(config->diverter.port); char *diverter_port = uint32_to_string(config->diverter.port);
filter->upstream = string_join("127.0.0.1:", diverter_port); filter->upstream = string_join("127.0.0.1:", diverter_port);
free(diverter_port); free(diverter_port);
@ -121,7 +121,7 @@ crontab* load_crond(cleardns_config *config) {
return NULL; // disable crond return NULL; // disable crond
} }
crontab *crond = crontab_init(); crontab *crond = crontab_init();
crond->cron = string_init(config->assets.cron); crond->cron = strdup(config->assets.cron);
return crond; return crond;
} }

5
src/utils/process.c

@ -3,6 +3,7 @@
#endif #endif
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/prctl.h> #include <sys/prctl.h>
@ -28,7 +29,7 @@ void process_exec(process *proc);
process* process_init(const char *caption, const char *bin) { // init process struct process* process_init(const char *caption, const char *bin) { // init process struct
process *proc = (process *)malloc(sizeof(process)); process *proc = (process *)malloc(sizeof(process));
proc->pid = 0; // process not running proc->pid = 0; // process not running
proc->name = string_init(caption); // process caption proc->name = strdup(caption); // process caption
proc->cmd = string_list_init(); proc->cmd = string_list_init();
string_list_append(&proc->cmd, bin); // argv[0] normally be process file name string_list_append(&proc->cmd, bin); // argv[0] normally be process file name
proc->env = string_list_init(); // empty environment variable 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); free(exit_sig);
return exit_msg; return exit_msg;
} }
return string_init("Unknown reason"); return strdup("Unknown reason");
} }
void server_exit(int exit_code) { // kill sub process and exit void server_exit(int exit_code) { // kill sub process and exit

Loading…
Cancel
Save