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
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");

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*
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 {

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
*/
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); }
}