add new getcmd

This commit is contained in:
Heng Guo
2023-09-05 20:48:38 +08:00
committed by Aoran Zeng
parent 46ee35a40f
commit 898c7cf24c
2 changed files with 61 additions and 5 deletions

24
xy.h
View File

@@ -448,7 +448,7 @@ xy_str_strip (const char* str)
* 注意:执行结果后面有回车换行
*/
char *
xy_getcmd(const char * cmd)
xy_getcmd(const char * cmd, bool (*func)(const char*))
{
const int BUFSIZE = 1024;
@@ -465,15 +465,29 @@ xy_getcmd(const char * cmd)
// 从 stream 指针指向的文件中读取数据。
char *ret;
do {
ret = fgets(buf, sizeof(buf), stream);
if(ret==NULL)
break;
if(fgets(buf, sizeof(buf), stream)==NULL)
{
break;
}
if(func==NULL)
{
ret = buf;
}
else
{
if(func(buf))
{
ret = buf;
break;
}
}
}while(1);
// 关闭 stream 指针。
pclose(stream);
return buf;
return ret;
}
#endif