Refactor xy_getcmd()

This commit is contained in:
Aoran Zeng 2023-09-27 19:31:23 +08:00
parent 17afaae1f4
commit b032ec7710
3 changed files with 37 additions and 44 deletions

45
chsrc.c
View File

@ -19,10 +19,19 @@
void void
pl_ruby_getsrc (char* option) pl_ruby_getsrc (char* option)
{ {
char* cmd = "gem sources"; chsrc_run("gem sources");
chsrc_run(cmd); chsrc_run("bundle config get mirror.https://rubygems.org");
cmd = "bundle config get mirror.https://rubygems.org"; }
chsrc_run(cmd);
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; char* cmd = NULL;
FILE* fp = popen("gem sources -l", "r"); xy_getcmd ("gem sources -l", 0, pl_ruby_remove_gem_source);
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);
cmd = xy_2strjoin("gem source -a ", source.url); cmd = xy_2strjoin("gem source -a ", source.url);
chsrc_run(cmd); chsrc_run(cmd);
@ -438,14 +437,8 @@ pl_java_check_cmd_(bool* maven_exist, bool* gradle_exist)
char* char*
pl_java_find_maven_config_ () pl_java_find_maven_config_ ()
{ {
FILE* fp = popen("mvn -v", "r"); char* buf = xy_getcmd ("mvn -v", 2, NULL);
char buf[512];
fgets(buf, 512, fp);
memset(buf, 0, 512);
fgets(buf, 512, fp);
pclose(fp);
char* maven_home = xy_str_delete_prefix(buf, "Maven home: "); char* maven_home = xy_str_delete_prefix(buf, "Maven home: ");
// xy_info (buf);
maven_home = xy_str_strip(maven_home); maven_home = xy_str_strip(maven_home);
char* maven_config = xy_uniform_path(xy_2strjoin(maven_home, "/conf/settings.xml")); 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"); chsrc_backup ("/etc/apt/sources.list");
char* arch = xy_getcmd("arch", NULL); char* arch = xy_getcmd("arch", 0, NULL);
char* cmd = NULL; char* cmd = NULL;
if (strncmp(arch, "x86_64", 6)==0) if (strncmp(arch, "x86_64", 6)==0)
{ {
@ -1050,7 +1043,7 @@ os_arch_setsrc(char* option)
bool arch_flag = false; bool arch_flag = false;
char* new_file = NULL; char* new_file = NULL;
char* arch = xy_getcmd("arch", NULL); char* arch = xy_getcmd("arch", 0, NULL);
if (strncmp(arch, "x86_64", 6)==0) { if (strncmp(arch, "x86_64", 6)==0) {
arch_flag = true; arch_flag = true;
@ -1400,9 +1393,9 @@ os_netbsd_setsrc(char* option)
chsrc_backup ("/usr/pkg/etc/pkgin/repositories.conf"); 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* 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"); char* url = xy_strjoin(5, source.url, arch, "/", version, "/All");
chsrc_overwrite_file (url, "/usr/pkg/etc/pkgin/repositories.conf"); chsrc_overwrite_file (url, "/usr/pkg/etc/pkgin/repositories.conf");

23
chsrc.h
View File

@ -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* char*
to_human_readable_speed (double speed) 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 * https://github.com/mirrorz-org/oh-my-mirrorz/blob/master/oh-my-mirrorz.py
* (@ccmywish)C语言 * @ccmywish C语言
* *
* @return -1 * @return -1
*/ */
@ -150,15 +150,11 @@ test_speed_url (const char* url)
// xy_info (xy_2strjoin("chsrc: 测速 ", url)); // xy_info (xy_2strjoin("chsrc: 测速 ", url));
FILE* fp = popen(curl_cmd, "r"); char* buf = xy_getcmd (curl_cmd, 0, NULL);
char buf[64] = {0};
while(NULL!=fgets(buf, 64, fp));
// puts(buf);
// 如果尾部有换行,删除 // 如果尾部有换行,删除
char* last_lf = strrchr(buf, '\n'); buf = xy_str_strip (buf);
if (last_lf) *last_lf = '\0';
// 分隔两部分数据
char* split = strchr(buf, ' '); char* split = strchr(buf, ' ');
if (split) *split = '\0'; if (split) *split = '\0';
@ -168,8 +164,8 @@ test_speed_url (const char* url)
char* speedstr = to_human_readable_speed(speed); char* speedstr = to_human_readable_speed(speed);
if (200!=http_code) { if (200!=http_code) {
char* httpcodestr = xy_str_to_yellow(xy_2strjoin("HTTP码 ", buf)); char* httpcodestr = xy_str_to_yellow (xy_2strjoin("HTTP码 ", buf));
puts (xy_strjoin(3, speedstr, " | ", httpcodestr)); puts (xy_strjoin (3, speedstr, " | ", httpcodestr));
} else { } else {
puts (speedstr); puts (speedstr);
} }
@ -262,10 +258,7 @@ ensure_root ()
{ {
char* euid = getenv("$EUID"); char* euid = getenv("$EUID");
if (NULL==euid) { if (NULL==euid) {
FILE* fp = popen("id -u", "r"); char* buf = xy_getcmd("id -u", 0, NULL);
char buf[10] = {0};
fgets(buf, 10, fp);
fclose(fp);
if (0!=atoi(buf)) goto not_root; if (0!=atoi(buf)) goto not_root;
else return; else return;
} else { } else {

13
xy.h
View File

@ -456,12 +456,16 @@ xy_str_strip (const char* str)
/** /**
* cmd * cmd
*
* @param cmd
* @param n 0 n (n>0) n行
* @param func
* *
* @note * @note
*/ */
static char* 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; const int size = 512;
char* buf = (char*) malloc(size); char* buf = (char*) malloc(size);
@ -473,10 +477,13 @@ xy_getcmd(const char* cmd, bool (*func)(const char*))
} }
char* ret = NULL; char* ret = NULL;
unsigned long count = 0;
while (true) { while (true) {
if(NULL==fgets(buf, size, stream)) break; if (NULL==fgets(buf, size, stream)) break;
ret = buf; ret = buf;
count += 1;
if (n==count) break;
if (func) { func(buf); } if (func) { func(buf); }
} }