Negative C program argument in execl detected as an option

I am using the execl() function with this call:

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

All xmin, xmax, ymin, ymax are initialized:

 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 target program (/ localhome / globususer / mandel) xmin and ymin are defined as options, since they are negative numbers. Thus, getopt() detects "-0" by their values ​​and causes an error.

However, a direct call from the command line, for example:

 ./mandel -b -0.452902 0.456189 0.367922 1.277013 -f /localhome/globususer/mandel.ppm 

correctly understood by the program.

Does anyone have any ideas?

+4
source share
1 answer

You are using execl() incorrectly. You must set arg0 name of the executable:

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

On the man page:

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

When mandel runs getopt() with your original argument list, it skips -b (since it is in argv[0] , and it considers the name of the executable path), and therefore starts -0.452902 arguments using the number ( -0.452902 in your example ) instead of -b . This forces you to interpret -0 as an option, and you're out of luck.

+4
source

All Articles