From afa2d9cb647041a6560636aa02eb7378c80e49e2 Mon Sep 17 00:00:00 2001 From: Heng Guo <2085471348@qq.com> Date: Fri, 1 Sep 2023 22:23:03 +0800 Subject: [PATCH] Add feature os_debian. --- chsrc.c | 40 +++++++++++++++++++++++++++++++++------- chsrc.h | 22 ++++++++++++++++++++-- helper.h | 3 ++- 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/chsrc.c b/chsrc.c index d54f08f..94ef999 100644 --- a/chsrc.c +++ b/chsrc.c @@ -572,22 +572,47 @@ os_ubuntu_setsrc (char* option) system(backup); xy_info ("chsrc: 备份文件名: /etc/apt/sources.list.bak"); - const char* current_url = xy_strch(source_url,'/',"\\/"); - char* cmd = xy_strjoin(3, "sed -E \'s/(^[^#]* .*)http[:|\\.|\\/|a-z|A-Z]*\\/ubuntu\\//\\1", - current_url, - "/\'< /etc/apt/sources.list.bak | cat > /etc/apt/sources.list"); + char* cmd = xy_strjoin(3, "sed -E \'s@(^[^#]* .*)http[:|\\.|\\/|a-z|A-Z]*\\/ubuntu\\/@\\1", + source_url, + "@\'< /etc/apt/sources.list.bak | cat > /etc/apt/sources.list"); system(cmd); free(cmd); char* rm = "rm -rf /etc/apt/source.list.bak"; system(rm); - // free(rm); xy_info ("chsrc: 为 ubuntu 命令换源"); xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); } +void +os_debian_setsrc (char* option) +{ + int selected = 0; + for (int i=0;iname; + const char* source_abbr = os_ubuntu_sources[selected].mirror->abbr; + const char* source_url = os_ubuntu_sources[selected].url; + char* backup = "cp -rf /etc/apt/sources.list /etc/apt/sources.list.bak"; + system(backup); + + xy_info ("chsrc: 备份文件名: /etc/apt/sources.list.bak"); + + char* cmd = xy_strjoin(3, "sed -E \'s@(^[^#]* .*)http[:|\\.|\\/|a-z|A-Z]*\\/debian\\/@\\1", + source_url, + "@\'< /etc/apt/sources.list.bak | cat > /etc/apt/sources.list"); + system(cmd); + free(cmd); + + char* rm = "rm -rf /etc/apt/source.list.bak"; + system(rm); + + xy_info ("chsrc: 为 ubuntu 命令换源"); + xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); +} #define setsrc_fn(func) (const char const*)func @@ -613,10 +638,11 @@ static const char const }, -*os_ubuntu [] = {"ubuntu", NULL, setsrc_fn(os_ubuntu_setsrc)}, +*os_ubuntu [] = {"ubuntu", NULL, setsrc_fn(os_ubuntu_setsrc),NULL,sources_ptr(os_ubuntu_sources)}, +*os_debian [] = {"debian", NULL, setsrc_fn(os_debian_setsrc),NULL,sources_ptr(os_debian_sources)}, **os_systems[] = { - os_ubuntu + os_ubuntu,os_debian }, diff --git a/chsrc.h b/chsrc.h index 06765af..e43562d 100644 --- a/chsrc.h +++ b/chsrc.h @@ -239,7 +239,11 @@ pl_php_sources[] = { - +/** + * 2023-09-01 更新 + * + * TODO: 1. 源并不完整,且未经测试是否有效 + */ static source_info os_ubuntu_sources[] = { {&Ali, "https://mirrors.aliyun.com/ubuntu/"}, @@ -249,8 +253,22 @@ os_ubuntu_sources[] = { {&Tencent, "https://mirrors.tencent.com/ubuntu/"}, {&Netease, "https://mirrors.163.com/ubuntu/"}, {&Sohu, "https://mirrors.sohu.com/ubuntu/"}, -}; +}, +/** + * 2023-09-01 更新 + * + * TODO: 1. 源并不完整,且未经测试是否有效 + */ +os_debian_sources[] = { + {&Ali, "https://mirrors.aliyun.com/ubuntu/"}, + {&Bfsu, "https://mirrors.bfsu.edu.cn/ubuntu/"}, + {&Ustc, "https://mirrors.ustc.edu.cn/ubuntu/"}, + {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/"}, + {&Tencent, "https://mirrors.tencent.com/ubuntu/"}, + {&Netease, "https://mirrors.163.com/ubuntu/"}, + {&Sohu, "https://mirrors.sohu.com/ubuntu/"}, +}; /* 函数签名 */ bool does_the_program_exist (char* check_cmd, char* progname); diff --git a/helper.h b/helper.h index 157e1e5..1bf7e31 100644 --- a/helper.h +++ b/helper.h @@ -99,7 +99,8 @@ xy_log (int level, const char* str) /** - * + * 将str中所有的src字符替换成dest,并返回一个全新的字符串 + * 现在已经废弃不用 */ static char* xy_strch (const char* str, char src,const char* dest)