我对wordexp
的使用有疑问。如果此函数找不到任何文件,则返回就像找到1一样。
#include <stdio.h>
#include <wordexp.h>
#include <string.h>
int main(int argc, char* argv[])
{
if (argc < 2)
return -1;
printf("searching for %s:\n", argv[1]);
wordexp_t p;
memset(&p, 0, sizeof p);
if (wordexp(argv[1], &p, 0) != 0)
return -1;
char **w = p.we_wordv;
printf("p.we_offs = %zu\n", p.we_offs);
printf("p.we_wordc = %zu\n", p.we_wordc);
for (unsigned int i = 0; i < p.we_wordc; i++)
{
printf("file found: %s\n", w[i]);
}
wordfree(&p);
return 0;
}
使用./a.out "test*.c"
调用此程序会导致
searching for test*.c:
p.we_offs = 0
p.we_wordc = 2
file found: test1.c
file found: test2.c
但是使用./a.out "test0*.c"
调用它会导致
searching for test0*.c:
p.we_offs = 0
p.we_wordc = 1
file found: test0*.c
不应该p.we_wordc
等于0
,因为找不到文件?
提前致谢