execl 中 C 程序的负参数被检测为选项
我在该调用中使用了 execl() 函数:
execl("/localhome/globususer/mandel", "-b", xmin, xmax, ymin, ymax, "-f", name, (char*)NULL);
所有 xmin、xmax、ymin、ymax 均通过以下方式初始化:
sprintf(xmin, "%f", (double)(XPOS - realmargin));
sprintf(xmax, "%f", (double)(XPOS + realmargin));
sprintf(ymin, "%f", (double)(YPOS - realmargin));
sprintf(ymax, "%f", (double)(YPOS + realmargin));
在目标程序 (/localhome/globususer/mandel) 中,xmin 和 ymin 被检测为选项,因为它们是负数。 因此,getopt()
检测到其值上的“-0”,并引发错误。
来自命令行的直接调用,例如:
./mandel -b -0.452902 0.456189 0.367922 1.277013 -f /localhome/globususer/mandel.ppm
但是,程序可以正确理解
有人有什么想法吗?
I use the execl()
function with that call:
execl("/localhome/globususer/mandel", "-b", xmin, xmax, ymin, ymax, "-f", name, (char*)NULL);
All the xmin, xmax, ymin, ymax are initialized by:
sprintf(xmin, "%f", (double)(XPOS - realmargin));
sprintf(xmax, "%f", (double)(XPOS + realmargin));
sprintf(ymin, "%f", (double)(YPOS - realmargin));
sprintf(ymax, "%f", (double)(YPOS + realmargin));
In the targeted program (/localhome/globususer/mandel), xmin and ymin are detected as options since they are negative numbers.
So getopt()
detects "-0" on their values, and raises an error.
However, a direct call from the command line such as:
./mandel -b -0.452902 0.456189 0.367922 1.277013 -f /localhome/globususer/mandel.ppm
is correctly understood by the program.
Does anybody have any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您错误地使用了
execl()
。您应该将arg0
设置为可执行文件的名称:来自手册页< /a>:
当
mandel
使用原始参数列表运行getopt()
时,它会跳过-b
(因为它位于argv[0] 中)
,它认为这是可执行路径名),因此开始用数字(示例中的-0.452902
)而不是-b
解析参数。这使得它把-0
解释为一个选项,你运气不好。You' re using
execl()
incorrectly. You should setarg0
to the name of the executable:From the man page:
When
mandel
runsgetopt()
with your original argument list, it skips over the-b
(since it's inargv[0]
, and it thinks that's the executable path name), and therefore starts parsing the args with the number (-0.452902
in your example) instead of the-b
. That makes it interpret the-0
as an option, and you're out of luck.