Support curl on Cygwin

[GitHub #65]
This commit is contained in:
Aoran Zeng 2024-09-05 18:29:28 +08:00
parent f869b6e292
commit 16e7007197
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98
2 changed files with 37 additions and 6 deletions

View File

@ -2,7 +2,7 @@
<img alt="chsrc logo" src="image/chsrc.png"/> <img alt="chsrc logo" src="image/chsrc.png"/>
</div> </div>
全平台命令行换源工具,**目标支持 Linux, Windows, macOS, BSD 等尽可能多的操作系统龙芯、飞腾、RISC-V 等尽可能多的 CPU**。 全平台命令行换源工具,**目标支持 Linux, Windows (MSYS2, Cygwin), macOS, BSD 等尽可能多的操作系统环境龙芯、飞腾、RISC-V 等尽可能多的 CPU**。
我们使用 **C99** 来完成上述目标。我们并不使用 Python 或 JS 等解释语言,因为一个简单的换源工具,不应该强行塞给用户一个庞大的解释器和数十、数百 MB 其他文件。 我们使用 **C99** 来完成上述目标。我们并不使用 Python 或 JS 等解释语言,因为一个简单的换源工具,不应该强行塞给用户一个庞大的解释器和数十、数百 MB 其他文件。

View File

@ -6,8 +6,8 @@
* | Heng Guo <2085471348@qq.com> * | Heng Guo <2085471348@qq.com>
* Contributors : Peng Gao <gn3po4g@outlook.com> * Contributors : Peng Gao <gn3po4g@outlook.com>
* | * |
* Created on : <2023-08-29> * Created On : <2023-08-29>
* Last modified : <2024-09-04> * Last Modified : <2024-09-05>
* *
* chsrc * chsrc
* ------------------------------------------------------------*/ * ------------------------------------------------------------*/
@ -436,16 +436,47 @@ measure_speed_for_url (void *url)
ipv6 = "--ipv6"; ipv6 = "--ipv6";
} }
char *os_devnull = xy_os_devnull;
bool on_cygwin = false;
// https://github.com/RubyMetric/chsrc/issues/65
// curl (仅)在 Cygwin 上 -o nul 会把 nul 当做普通文件
// 为了践行 chsrc everywhere 的承诺,我们也考虑支持 Cygwin
if (0==system ("cygcheck --version>nul"))
{
on_cygwin = true;
os_devnull = "/tmp/chsrc-measure-downloaded";
}
// 我们用 —L因为Ruby China源会跳转到其他地方 // 我们用 —L因为Ruby China源会跳转到其他地方
// npmmirror 也会跳转 // npmmirror 也会跳转
char *curl_cmd = xy_strjoin (7, "curl -qsL ", ipv6, char *curl_cmd = xy_strjoin (8, "curl -qsL ", ipv6,
" -o " xy_os_devnull, " -o ", os_devnull,
" -w \"%{http_code} %{speed_download}\" -m", time_sec, " -w \"%{http_code} %{speed_download}\" -m", time_sec,
" -A chsrc/" Chsrc_Banner_Version " ", url); " -A chsrc/" Chsrc_Banner_Version " ", url);
// chsrc_info (xy_2strjoin ("测速命令 ", curl_cmd)); // chsrc_info (xy_2strjoin ("测速命令 ", curl_cmd));
char *curl_buf = xy_run (curl_cmd, 0, NULL); char *curl_buf = NULL;
if (on_cygwin)
{
char *curl_script = ".chsrc_measure_tmp.sh";
FILE *f = fopen (curl_script, "w");
if (f==NULL)
exit (Exit_UserCause);
fputs (curl_cmd, f);
fclose (f);
curl_buf = xy_run ( xy_2strjoin ("bash .\\", curl_script), 0, NULL);
system (xy_2strjoin ("del ", curl_script));
system (xy_strjoin (3, "bash -c \"rm ", os_devnull, "\""));
}
else
{
curl_buf = xy_run (curl_cmd, 0, NULL);
}
// 如果尾部有换行,删除 // 如果尾部有换行,删除
curl_buf = xy_str_strip (curl_buf); curl_buf = xy_str_strip (curl_buf);