From 890bf23b28cb57b9bfb6d81879dca3b5386c8f19 Mon Sep 17 00:00:00 2001 From: Heng Guo <2085471348@qq.com> Date: Sun, 3 Sep 2023 12:01:43 +0800 Subject: [PATCH] Feature os_fedora,kali,openbsd,mysys2 and fix some bug with os;Add stdarg in helper.h ;Add OS_macros in helper.h --- chsrc.c | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- chsrc.h | 88 +++++++++++++++++++++++---- helper.h | 26 ++++++-- 3 files changed, 270 insertions(+), 23 deletions(-) diff --git a/chsrc.c b/chsrc.c index 8254f23..769be6d 100644 --- a/chsrc.c +++ b/chsrc.c @@ -612,6 +612,9 @@ pl_julia_setsrc (char* option) +/** + * ubuntu不同架构下的换源是不一样的,这个针对x86架构 + */ void os_ubuntu_setsrc (char* option) { @@ -634,12 +637,16 @@ os_ubuntu_setsrc (char* option) system(cmd); free(cmd); - char* rm = "rm -rf /etc/apt/source.list.bak"; - system(rm); + // char* rm = "rm -rf /etc/apt/source.list.bak"; + // system(rm); xy_info ("chsrc: 为 ubuntu 命令换源"); xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); } +/** + * Debian Buster 以上版本默认支持 HTTPS 源。如果遇到无法拉取 HTTPS 源的情况,请先使用 HTTP 源并安装 + * sudo apt install apt-transport-https ca-certificates + */ void os_debian_setsrc (char* option) { @@ -662,10 +669,156 @@ os_debian_setsrc (char* option) system(cmd); free(cmd); - char* rm = "rm -rf /etc/apt/source.list.bak"; - system(rm); + // char* rm = "rm -rf /etc/apt/source.list.bak"; + // system(rm); - xy_info ("chsrc: 为 ubuntu 命令换源"); + xy_info ("chsrc: 为 debian 命令换源"); + xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); +} +/** + * fedora29版本及以下暂不支持 + */ +void +os_fedora_setsrc (char* option) +{ + int selected = 0; + for (int i=0;iname; + const char* source_abbr = os_fedora_sources[selected].mirror->abbr; + const char* source_url = os_fedora_sources[selected].url; + + char* backup = "cp -rf /etc/yum.repos.d/fedora.repo /etc/yum.repos.d/fedora.repo.bak"; + system(backup); + backup = "cp -rf /etc/yum.repos.d/fedora-updates.repo /etc/yum.repos.d/fedora-updates.repo.bak"; + system(backup); + + xy_info ("chsrc: 备份文件名:1. /etc/yum.repos.d/fedora.repo.bak"); + xy_info ("chsrc: 备份文件名:2. /etc/yum.repos.d/fedora-updates.repo.bak"); + + + char* cmd = xy_strjoin(9, "sed -e 's|^metalink=|#metalink=|g' ", + "-e 's|^#baseurl=http://download.example/pub/fedora/linux/|baseurl=", + source_url, + "|g' ", + "-i.bak ", + "/etc/yum.repos.d/fedora.repo ", + "/etc/yum.repos.d/fedora-modular.repo ", + "/etc/yum.repos.d/fedora-updates.repo ", + "/etc/yum.repos.d/fedora-updates-modular.repo"); + system(cmd); + free(cmd); + + xy_info("替换文件:/etc/yum.repos.d/fedora.repo"); + xy_info("新增文件:/etc/yum.repos.d/fedora-modular.repo"); + xy_info("替换文件:/etc/yum.repos.d/fedora-updates.repo"); + xy_info("新增文件:/etc/yum.repos.d/fedora-updates-modular.repo"); + + // char* rm = "rm -rf /etc/yum.repos.d/fedora.repo.bak"; + // system(rm); + // char* rm = "rm -rf /etc/yum.repos.d/fedora-updates.repo.bak"; + // system(rm); + + xy_info ("chsrc: 为 fedora 命令换源"); + xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); +} + + + + +void +os_kali_setsrc(char* option) +{ + int selected = 0; + for (int i=0;iname; + const char* source_abbr = os_kali_sources[selected].mirror->abbr; + const char* source_url = os_kali_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 -i \'s@(^[^#]* .*)http[:|\\.|\\/|a-z|A-Z]*\\/kali\\/@\\1", + source_url, + "@g\' /etc/apt/sources.list"); + system(cmd); + free(cmd); + + // char* rm = "rm -rf /etc/apt/source.list.bak"; + // system(rm); + + xy_info ("chsrc: 为 kali 命令换源"); + xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); +} + + +void +os_openbsd_setsrc(char* option) +{ + int selected = 0; + for (int i=0;iname; + const char* source_abbr = os_openbsd_sources[selected].mirror->abbr; + const char* source_url = os_openbsd_sources[selected].url; + + char* backup = "cp -rf /etc/installurl /etc/installurl.bak"; + system(backup); + + xy_info ("chsrc: 备份文件名: /etc/installurl.bak"); + + char* cmd = xy_strjoin(3,"echo ",source_url," > /etc/installurl"); + system(cmd); + free(cmd); + + // char* rm = "rm -rf /etc/installurl.bak"; + // system(rm); + + xy_info ("chsrc: 为 openbsd 命令换源"); + xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); +} + + +void +os_mysys2_setsrc(char* option) +{ + int selected = 0; + for (int i=0;iname; + const char* source_abbr = os_mysys2_sources[selected].mirror->abbr; + const char* source_url = os_mysys2_sources[selected].url; + + char* backup = "cp -rf /etc/pacman.d/mirrorlist.mingw32 /etc/pacman.d/mirrorlist.mingw32.bak"; + system(backup); + backup = "cp -rf /etc/pacman.d/mirrorlist.mingw64 /etc/pacman.d/mirrorlist.mingw64.bak"; + system(backup); + backup = "cp -rf /etc/pacman.d/mirrorlist.msys /etc/pacman.d/mirrorlist.msys.bak"; + system(backup); + + xy_info ("chsrc: 备份文件名:1. /etc/pacman.d/mirrorlist.mingw32.bak"); + xy_info ("chsrc: 备份文件名:2. /etc/pacman.d/mirrorlist.mingw64.bak"); + xy_info ("chsrc: 备份文件名:3. /etc/pacman.d/mirrorlist.msys.bak"); + + char* cmd = xy_strjoin(3,"sed -i \"s#https\?://mirror.msys2.org/#",source_url,"#g\" /etc/pacman.d/mirrorlist* "); + system(cmd); + free(cmd); + + // char* rm = "rm -rf /etc/pacman.d/mirrorlist.mingw32.bak"; + // system(rm); + // rm = "rm -rf /etc/pacman.d/mirrorlist.mingw64.bak"; + // system(rm); + // rm = "rm -rf /etc/pacman.d/mirrorlist.msys.bak"; + // system(rm); + + xy_info ("chsrc: 为 mysys2 命令换源"); xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); } @@ -708,11 +861,19 @@ static const char const target_info - os_ubuntu_target = {os_ubuntu_setsrc, NULL, NULL, os_ubuntu_sources, 7}, - os_debian_target = {os_debian_setsrc, NULL, NULL, os_debian_sources, 7}; + os_ubuntu_target = {os_ubuntu_setsrc, NULL, NULL, os_ubuntu_sources, 7}, + os_debian_target = {os_debian_setsrc, NULL, NULL, os_debian_sources, 7}, + os_fedora_target = {os_ubuntu_setsrc, NULL, NULL, os_ubuntu_sources, 7}, + os_kali_target = {os_ubuntu_setsrc, NULL, NULL, os_ubuntu_sources, 7}, + os_openbsd_target = {os_ubuntu_setsrc, NULL, NULL, os_ubuntu_sources, 7}, + os_mysys2_target = {os_ubuntu_setsrc, NULL, NULL, os_ubuntu_sources, 7}; static const char const -*os_ubuntu [] = {"ubuntu", NULL, targetinfo(&os_ubuntu_target)}, -*os_debian [] = {"debian", NULL, targetinfo(&os_debian_target)}, +*os_ubuntu [] = {"ubuntu", NULL, targetinfo(&os_ubuntu_target)}, +*os_debian [] = {"debian", NULL, targetinfo(&os_debian_target)}, +*os_fedora [] = {"debian", NULL, targetinfo(&os_debian_target)}, +*os_kali [] = {"debian", NULL, targetinfo(&os_debian_target)}, +*os_openbsd [] = {"debian", NULL, targetinfo(&os_debian_target)}, +*os_mysys2 [] = {"debian", NULL, targetinfo(&os_debian_target)}, **os_systems[] = { os_ubuntu, os_debian diff --git a/chsrc.h b/chsrc.h index 5eb445c..78d3009 100644 --- a/chsrc.h +++ b/chsrc.h @@ -254,7 +254,7 @@ os_ubuntu_sources[] = { {&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/"}, + {&Sohu, "https://mirrors.sohu.com/ubuntu/"} }, /** @@ -263,14 +263,81 @@ os_ubuntu_sources[] = { * 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/"}, -}; + {&Ali, "https://mirrors.aliyun.com/debian/"}, + {&Bfsu, "https://mirrors.bfsu.edu.cn/debian/"}, + {&Ustc, "https://mirrors.ustc.edu.cn/debian/"}, + {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/debian/"}, + {&Tencent, "https://mirrors.tencent.com/debian/"}, + {&Netease, "https://mirrors.163.com/debian/"}, + {&Sohu, "https://mirrors.sohu.com/debian/"} +}, + + + +/** + * 2023-09-02 更新 + * + * TODO: 1. 源并不完整,且未经测试是否有效 + */ +os_fedora_sources[] = { + {&Ali, "https://mirrors.aliyun.com/fedora/"}, + {&Bfsu, "https://mirrors.bfsu.edu.cn/fedora/"}, + {&Ustc, "https://mirrors.ustc.edu.cn/fedora/"}, + {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/fedora/"}, + {&Tencent, "https://mirrors.tencent.com/fedora/"}, + {&Netease, "https://mirrors.163.com/fedora/"}, + {&Sohu, "https://mirrors.sohu.com/fedora/"} +}, + + +/** + * 2023-09-02 更新 + * + * TODO: 1. 源并不完整,且未经测试是否有效 + */ +os_kali_sources[] = { + {&Ali, "https://mirrors.aliyun.com/kali/"}, + {&Bfsu, "https://mirrors.bfsu.edu.cn/kali/"}, + {&Ustc, "https://mirrors.ustc.edu.cn/kali/"}, + {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/kali/"}, + {&Tencent, "https://mirrors.tencent.com/kali/"}, + {&Netease, "https://mirrors.163.com/kali/"}, + {&Sohu, "https://mirrors.sohu.com/kali/"} +}, + + + +/** + * 2023-09-02 更新 + * + * TODO: 1. 源并不完整,且未经测试是否有效 + */ +os_openbsd_sources[] = { + {&Ali, "https://mirrors.aliyun.com/OpenBSD/"}, + {&Bfsu, "https://mirrors.bfsu.edu.cn/OpenBSD/"}, + {&Ustc, "https://mirrors.ustc.edu.cn/OpenBSD/"}, + {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/OpenBSD/"}, + {&Tencent, "https://mirrors.tencent.com/OpenBSD/"}, + {&Netease, "https://mirrors.163.com/OpenBSD/"}, + {&Sohu, "https://mirrors.sohu.com/OpenBSD/"} +}, + + +/** + * 2023-09-02 更新 + * + * TODO: 1. 源并不完整,且未经测试是否有效 + */ +os_mysys2_sources[] = { + {&Ali, "https://mirrors.aliyun.com/msys2/"}, + {&Bfsu, "https://mirrors.bfsu.edu.cn/msys2/"}, + {&Ustc, "https://mirrors.ustc.edu.cn/msys2/"}, + {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/msys2/"}, + {&Tencent, "https://mirrors.tencent.com/msys2/"}, + {&Netease, "https://mirrors.163.com/msys2/"}, + {&Sohu, "https://mirrors.sohu.com/msys2/"} +} +; /* 函数签名 */ bool does_the_program_exist (char* check_cmd, char* progname); @@ -282,7 +349,8 @@ def_target_sources_n(pl_ruby); def_target_sources_n(pl_python); def_target_sourc def_target_sources_n(pl_perl); def_target_sources_n(pl_php); def_target_sources_n(pl_go); def_target_sources_n(pl_rust); def_target_sources_n(pl_java); def_target_sources_n(pl_r); def_target_sources_n(pl_julia); -def_target_sources_n(os_ubuntu); def_target_sources_n(os_debian); +def_target_sources_n(os_ubuntu); def_target_sources_n(os_debian); def_target_sources_n(os_fedora); +def_target_sources_n(os_kali); def_target_sources_n(os_openbsd); def_target_sources_n(os_mysys2); /* Target Info */ typedef struct { diff --git a/helper.h b/helper.h index b9a7b27..bb0c136 100644 --- a/helper.h +++ b/helper.h @@ -12,6 +12,7 @@ #ifndef XY_H #define XY_H +#include #include #include #include @@ -19,6 +20,7 @@ // #define NDEBUG + #ifdef _WIN32 static bool xy_on_windows = true; @@ -26,20 +28,36 @@ static bool xy_on_macos = false; static bool xy_on_bsds = false; - static char* xy_os_devnull = "nul"; - #include #define xy_useutf8() SetConsoleOutputCP(65001) -#else +#elif defined(__linux__) || defined(__linux) + static bool xy_on_windows = false; static bool xy_on_linux = true; static bool xy_on_macos = false; static bool xy_on_bsds = false; - static char* xy_os_devnull = "/dev/null" + #define xy_useutf8() + +#elif defined(TARGET_OS_MAC) ||defined(__MACOSX__) + + static bool xy_on_windows = false; + static bool xy_on_linux = false; + static bool xy_on_macos = true; + static bool xy_on_bsds = false; #define xy_useutf8() + +#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) + + static bool xy_on_windows = false; + static bool xy_on_linux = false; + static bool xy_on_macos = false; + static bool xy_on_bsds = true; + + #define xy_useutf8() + #endif