Browse Source

update: using string load function

dev
Dnomd343 2 years ago
parent
commit
7b9e1781c3
  1. 10
      src/cleardns.c
  2. 4
      src/common/json.c
  3. 2
      src/common/structure.c
  4. 7
      src/common/system.c
  5. 4
      src/utils/assets.c

10
src/cleardns.c

@ -115,12 +115,8 @@ void cleardns() { // cleardns service
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
// init(argc, argv); init(argc, argv);
// log_info("ClearDNS server start (%s)", VERSION); log_info("ClearDNS server start (%s)", VERSION);
// cleardns(); cleardns();
char *test = string_load("`%s` -> %d", "test", 2333);
printf("%s\n", test);
return 0; return 0;
} }

4
src/common/json.c

@ -1,4 +1,3 @@
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "cJSON.h" #include "cJSON.h"
@ -27,8 +26,7 @@ char* to_json(const char *file) { // convert JSON / TOML / YAML to json format (
flag[8] = '\0'; flag[8] = '\0';
char *output_file = string_join("/tmp/to-json-", flag); char *output_file = string_join("/tmp/to-json-", flag);
char *to_json_cmd = (char *)malloc(strlen(file) + strlen(output_file) + 11); char *to_json_cmd = string_load("toJSON %s > %s", file, output_file);
sprintf(to_json_cmd, "toJSON %s > %s", file, output_file);
int to_json_ret = run_command(to_json_cmd); int to_json_ret = run_command(to_json_cmd);
free(to_json_cmd); free(to_json_cmd);

2
src/common/structure.c

@ -92,7 +92,7 @@ char* uint32_list_dump(uint32_t **uint32_list) { // [1, 2, 3, ...]
strcpy(string_ret, "["); strcpy(string_ret, "[");
for (uint32_t **number = uint32_list; *number != NULL; ++number) { for (uint32_t **number = uint32_list; *number != NULL; ++number) {
sprintf(uint32_str, "%u", **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 = strcat(strcat(string_ret, uint32_str), ", ");
} }
string_ret[strlen(string_ret) - 2] = '\0'; string_ret[strlen(string_ret) - 2] = '\0';

7
src/common/system.c

@ -4,6 +4,7 @@
#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"
@ -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 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); char *append_cmd = string_load("cat %s >> %s", append_file, base_file);
sprintf(append_cmd, "cat %s >> %s", append_file, base_file);
run_command(append_cmd); run_command(append_cmd);
free(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 void download_file(const char *file, const char *url) { // download file
log_debug("Download file `%s` -> %s", file, url); log_debug("Download file `%s` -> %s", file, url);
char *download_cmd = (char *)malloc(strlen(file) + strlen(url) + 15); char *download_cmd = string_load("wget -T 8 -O %s %s", file, url);
sprintf(download_cmd, "wget -T 8 -O %s %s", file, url);
if (run_command(download_cmd)) { if (run_command(download_cmd)) {
log_warn("File `%s` download failed", url); log_warn("File `%s` download failed", url);
} }

4
src/utils/assets.c

@ -1,4 +1,3 @@
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <signal.h>
@ -81,8 +80,7 @@ void extract(const char *file) { // extract one asset file from `.tar.xz` file
} }
free(output_file); free(output_file);
char *extract_cmd = (char *)malloc(strlen(ASSETS_PKG) + strlen(file) + strlen(ASSETS_DIR) + 13); char *extract_cmd = string_load("tar xf %s %s -C %s", ASSETS_PKG, file, ASSETS_DIR);
sprintf(extract_cmd, "tar xf %s %s -C %s", ASSETS_PKG, file, ASSETS_DIR);
if (run_command(extract_cmd)) { if (run_command(extract_cmd)) {
log_warn("Extract asset `%s` failed", file); log_warn("Extract asset `%s` failed", file);
} else { } else {

Loading…
Cancel
Save