开发者

Negative argument to a C program in execl detected as an option

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", (doubl开发者_高级运维e)(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?


You' re using execl() incorrectly. You should set arg0 to the name of the executable:

execl("/localhome/globususer/mandel",
      "/localhome/globususer/mandel",
      "-b",
      xmin, 
      xmax, 
      ymin, 
      ymax, 
      "-f", 
      name, 
      NULL);

From the man page:

The const char *arg and subsequent ellipses in the execl(), execlp(), and execle() functions can be thought of as arg0, arg1, ..., argn. Together they describe a list of one or more pointers to null-terminated strings that represent the argument list available to the executed program. The first argument, by convention, should point to the filename associated with the file being executed.

When mandel runs getopt() with your original argument list, it skips over the -b (since it's in argv[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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜