Xcode:wordexp() 有时不起作用,为“~”返回 0 个匹配的单词
当在 Xcode 中启动时,在最小的 C 程序中使用 wordexp() 时,我看到一些奇怪的行为。我无法通过从命令行启动编译的二进制文件来重现此问题。
#include <stdio.h>
#include <stdlib.h>
#include <wordexp.h>
#include <errno.h>
#include <assert.h>
int main (int argc, const char * argv[])
{
int wordexpResult;
wordexp_t words;
char* origPath = "~";
wordexpResult = wordexp(origPath, &words, 0);
printf("wordexpResult = %i\n", wordexpResult);
switch (wordexpResult)
{
case 0:
break;
case WRDE_BADCHAR:
fprintf(stderr, "BADCHAR\n");
exit(EXIT_FAILURE);
case WRDE_BADVAL:
fprintf(stderr, "BADVAL\n");
exit(EXIT_FAILURE);
case WRDE_CMDSUB:
fprintf(stderr, "CMDSUB\n");
exit(EXIT_FAILURE);
case WRDE_NOSPACE:
fprintf(stderr, "NOSPACE\n");
exit(EXIT_FAILURE);
case WRDE_SYNTAX:
fprintf(stderr, "SYNTAX\n");
exit(EXIT_FAILURE);
default:
fprintf(stderr, "Unrecognized value: %d\n", wordexpResult);
exit(EXIT_FAILURE);
}
assert(words.we_wordc != 0);
wordfree(&words);
return 0;
}
有时(大约五分之一的运行),断言的计算结果为 false,即使 wordexp()总是返回 0(即 switch() 中的所有情况都不被执行)。这意味着 wordexp() 不会返回错误,但也不会扩展波形符。怎么可能呢?
幸运的是,我现在不必依赖 wordexp,但我仍然对这里发生的事情感兴趣。我的意思是......没有多线程,没有变化的输入数据,什么都没有。有什么想法吗?
它在 Mac OS X 10.6.4、Xcode 3.2.4 上运行。
干杯,
马可
I'm seeing some weird behaviour when using wordexp() in a minimal C program when started within Xcode. I cannot reproduce this by starting the compiled binary from the command line.
#include <stdio.h>
#include <stdlib.h>
#include <wordexp.h>
#include <errno.h>
#include <assert.h>
int main (int argc, const char * argv[])
{
int wordexpResult;
wordexp_t words;
char* origPath = "~";
wordexpResult = wordexp(origPath, &words, 0);
printf("wordexpResult = %i\n", wordexpResult);
switch (wordexpResult)
{
case 0:
break;
case WRDE_BADCHAR:
fprintf(stderr, "BADCHAR\n");
exit(EXIT_FAILURE);
case WRDE_BADVAL:
fprintf(stderr, "BADVAL\n");
exit(EXIT_FAILURE);
case WRDE_CMDSUB:
fprintf(stderr, "CMDSUB\n");
exit(EXIT_FAILURE);
case WRDE_NOSPACE:
fprintf(stderr, "NOSPACE\n");
exit(EXIT_FAILURE);
case WRDE_SYNTAX:
fprintf(stderr, "SYNTAX\n");
exit(EXIT_FAILURE);
default:
fprintf(stderr, "Unrecognized value: %d\n", wordexpResult);
exit(EXIT_FAILURE);
}
assert(words.we_wordc != 0);
wordfree(&words);
return 0;
}
Sometimes (about one in five runs), the assertion evaluates to false, even though wordexp() always returns 0 (i.e. none of the cases in switch() are executed). This means that wordexp() does not return an error, but it does not expand the tilde either. How can that be?
Luckily, I don't have to rely on wordexp right now, but I'd still be interested in what is going on here. I mean... there's no multi threading, no varying input data, nothing. Any ideas?
This is running on Mac OS X 10.6.4, Xcode 3.2.4.
Cheers,
Marco
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论