From b032ec7710233d0c4e4393b4d4ff4e20316716b9 Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Wed, 27 Sep 2023 19:31:23 +0800 Subject: [PATCH] Refactor `xy_getcmd()` --- chsrc.c | 45 +++++++++++++++++++-------------------------- chsrc.h | 23 ++++++++--------------- xy.h | 13 ++++++++++--- 3 files changed, 37 insertions(+), 44 deletions(-) diff --git a/chsrc.c b/chsrc.c index a9e6c32..9888550 100644 --- a/chsrc.c +++ b/chsrc.c @@ -19,10 +19,19 @@ void pl_ruby_getsrc (char* option) { - char* cmd = "gem sources"; - chsrc_run(cmd); - cmd = "bundle config get mirror.https://rubygems.org"; - chsrc_run(cmd); + chsrc_run("gem sources"); + chsrc_run("bundle config get mirror.https://rubygems.org"); +} + +void +pl_ruby_remove_gem_source (const char* source) +{ + char* cmd = NULL; + if (xy_str_start_with(source, "http")){ + cmd = xy_str_delete_suffix(source, "\n"); + cmd = xy_2strjoin("gem sources -r ", cmd); + chsrc_run(cmd); + } } /** @@ -46,17 +55,7 @@ pl_ruby_setsrc (char* option) char* cmd = NULL; - FILE* fp = popen("gem sources -l", "r"); - char buf[512] = {0}; - while(NULL!=fgets(buf, 512, fp)) { - if (xy_str_start_with(buf, "http")){ - cmd = xy_str_delete_suffix(buf, "\n"); - cmd = xy_2strjoin("gem sources -r ", cmd); - chsrc_run(cmd); - } - memset(buf, 0, 512); - } - pclose(fp); + xy_getcmd ("gem sources -l", 0, pl_ruby_remove_gem_source); cmd = xy_2strjoin("gem source -a ", source.url); chsrc_run(cmd); @@ -438,14 +437,8 @@ pl_java_check_cmd_(bool* maven_exist, bool* gradle_exist) char* pl_java_find_maven_config_ () { - FILE* fp = popen("mvn -v", "r"); - char buf[512]; - fgets(buf, 512, fp); - memset(buf, 0, 512); - fgets(buf, 512, fp); - pclose(fp); + char* buf = xy_getcmd ("mvn -v", 2, NULL); char* maven_home = xy_str_delete_prefix(buf, "Maven home: "); - // xy_info (buf); maven_home = xy_str_strip(maven_home); char* maven_config = xy_uniform_path(xy_2strjoin(maven_home, "/conf/settings.xml")); @@ -794,7 +787,7 @@ os_ubuntu_setsrc (char* option) chsrc_backup ("/etc/apt/sources.list"); - char* arch = xy_getcmd("arch", NULL); + char* arch = xy_getcmd("arch", 0, NULL); char* cmd = NULL; if (strncmp(arch, "x86_64", 6)==0) { @@ -1050,7 +1043,7 @@ os_arch_setsrc(char* option) bool arch_flag = false; char* new_file = NULL; - char* arch = xy_getcmd("arch", NULL); + char* arch = xy_getcmd("arch", 0, NULL); if (strncmp(arch, "x86_64", 6)==0) { arch_flag = true; @@ -1400,9 +1393,9 @@ os_netbsd_setsrc(char* option) chsrc_backup ("/usr/pkg/etc/pkgin/repositories.conf"); - char* arch = xy_getcmd("arch", NULL); + char* arch = xy_getcmd("arch", 0, NULL); char* vercmd = "cat /etc/os-release | grep \"VERSION=\" | grep -Po \"[8-9].[0-9]+\""; - char* version = xy_getcmd(vercmd, NULL); + char* version = xy_getcmd(vercmd, 0, NULL); char* url = xy_strjoin(5, source.url, arch, "/", version, "/All"); chsrc_overwrite_file (url, "/usr/pkg/etc/pkgin/repositories.conf"); diff --git a/chsrc.h b/chsrc.h index 53f3ee3..f7f2273 100644 --- a/chsrc.h +++ b/chsrc.h @@ -97,7 +97,7 @@ does_the_input_mirror_exist (source_info* sources, size_t size, char* target, ch /** - * 该函数来自 oh-my-mirrorz.py,由我(@ccmywish)翻译为C语言,但功劳和版权属于原作者 + * 该函数来自 oh-my-mirrorz.py,由 @ccmywish 翻译为C语言,但功劳和版权属于原作者 */ char* to_human_readable_speed (double speed) @@ -125,7 +125,7 @@ to_human_readable_speed (double speed) /** * 测速代码参考自 https://github.com/mirrorz-org/oh-my-mirrorz/blob/master/oh-my-mirrorz.py - * 功劳和版权属于原作者,由我(@ccmywish)修改为C语言,并做了额外调整。 + * 功劳和版权属于原作者,由 @ccmywish 修改为C语言,并做了额外调整 * * @return 返回测得的速度,若出错,返回-1 */ @@ -150,15 +150,11 @@ test_speed_url (const char* url) // xy_info (xy_2strjoin("chsrc: 测速 ", url)); - FILE* fp = popen(curl_cmd, "r"); - char buf[64] = {0}; - while(NULL!=fgets(buf, 64, fp)); - // puts(buf); - + char* buf = xy_getcmd (curl_cmd, 0, NULL); // 如果尾部有换行,删除 - char* last_lf = strrchr(buf, '\n'); - if (last_lf) *last_lf = '\0'; + buf = xy_str_strip (buf); + // 分隔两部分数据 char* split = strchr(buf, ' '); if (split) *split = '\0'; @@ -168,8 +164,8 @@ test_speed_url (const char* url) char* speedstr = to_human_readable_speed(speed); if (200!=http_code) { - char* httpcodestr = xy_str_to_yellow(xy_2strjoin("HTTP码 ", buf)); - puts (xy_strjoin(3, speedstr, " | ", httpcodestr)); + char* httpcodestr = xy_str_to_yellow (xy_2strjoin("HTTP码 ", buf)); + puts (xy_strjoin (3, speedstr, " | ", httpcodestr)); } else { puts (speedstr); } @@ -262,10 +258,7 @@ ensure_root () { char* euid = getenv("$EUID"); if (NULL==euid) { - FILE* fp = popen("id -u", "r"); - char buf[10] = {0}; - fgets(buf, 10, fp); - fclose(fp); + char* buf = xy_getcmd("id -u", 0, NULL); if (0!=atoi(buf)) goto not_root; else return; } else { diff --git a/xy.h b/xy.h index c21ae6b..9bec834 100644 --- a/xy.h +++ b/xy.h @@ -456,12 +456,16 @@ xy_str_strip (const char* str) /** - * 执行cmd,返回其最后一行输出结果 + * 执行cmd,返回其最后某行输出结果 + * + * @param cmd 要执行的命令 + * @param n 命令打印的结果行,0 表示最后一行,n (n>0) 表示第n行 + * @param func 对读取的行执行函数 * * @note 返回的字符串最后面可能有换行符号 */ static char* -xy_getcmd(const char* cmd, bool (*func)(const char*)) +xy_getcmd (const char* cmd, unsigned long n, void (*func)(const char*)) { const int size = 512; char* buf = (char*) malloc(size); @@ -473,10 +477,13 @@ xy_getcmd(const char* cmd, bool (*func)(const char*)) } char* ret = NULL; + unsigned long count = 0; while (true) { - if(NULL==fgets(buf, size, stream)) break; + if (NULL==fgets(buf, size, stream)) break; ret = buf; + count += 1; + if (n==count) break; if (func) { func(buf); } }