From 452fc7276262a03e24b4daa8c1741f9f263a8a19 Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Tue, 11 Jun 2024 16:54:28 +0800 Subject: [PATCH] Rename to use `xy_run` --- include/xy.h | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/include/xy.h b/include/xy.h index 52e5ae7..7ad3d6b 100644 --- a/include/xy.h +++ b/include/xy.h @@ -3,7 +3,7 @@ * License : MIT * Authors : Aoran Zeng * Created on : <2023-08-28> - * Last modified : <2024-06-08> + * Last modified : <2024-06-11> * * xy: * @@ -17,7 +17,7 @@ #ifndef XY_H #define XY_H -#define XY_Version "v0.1.2-2024/06/08" +#define XY_Version "v0.1.2-2024/06/11" #define XY_Maintain_URL "https://gitee.com/RubyMetric/chsrc/blob/main/xy.h" #include @@ -545,19 +545,22 @@ _xy_log_remarkably (int level, const char *prompt1, const char *prompt2, const c * System ******************************************************/ /** - * 执行cmd,返回其最后某行输出结果 + * 执行cmd,返回某行输出结果,并对已经遍历过的行执行iter_func * - * @param cmd 要执行的命令 - * @param n 命令打印的结果行,0 表示最后一行,n (n>0) 表示第n行 - * @param func 对读取的行执行函数 + * @param cmd 要执行的命令 + * @param n 指定命令执行输出的结果行中的某一行,0 表示最后一行,n (n>0) 表示第n行 + * 该函数会返回这一行的内容 + * @param iter_func 对遍历时经过的行的内容,进行函数调用 * - * @note 返回的字符串最后面可能有换行符号 + * @note 返回的字符串最后面一般有换行符号 + * + * 由于目标行会被返回出来,所以 iter_func() 并不执行目标行,只会执行遍历过的行 */ static char * -xy_getcmd (const char *cmd, unsigned long n, void (*func) (const char *)) +xy_run (const char *cmd, unsigned long n, void (*iter_func) (const char *)) { const int size = 512; - char *buf = (char *)malloc (size); + char *buf = (char *) malloc (size); FILE *stream = popen (cmd, "r"); if (stream == NULL) @@ -577,9 +580,9 @@ xy_getcmd (const char *cmd, unsigned long n, void (*func) (const char *)) count += 1; if (n == count) break; - if (func) + if (iter_func) { - func (buf); + iter_func (buf); } }