[cs631apue] exec error

Jan Schaumann jschauma at stevens.edu
Tue Nov 26 22:48:44 EST 2019


Hem Shah <hshah42 at stevens.edu> wrote:
> int
> main(int argc, char **argv) {
>         (void) setenv("something", "something", 1);
>         (void) execl("/cgi-bin/something.cgi", (char *) 0);
>         printf("%s\n", strerror(errno));
>         return 1;
> }

Are you compiling this program with "-Wall -Werror"?

You do not have enough arguments in your call to
execl(3); the function requires at least a third
argument, per the manual page:

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

Use

	execl("/cgi-bin/something.cgi", "something.cgi", (char *) 0);

instead, and it should work.

(I don't know yet why the program works if you don't
call setenv(3) or if you add a printf(3) after
setenv(3), but calling execl(3) without a third
argument means you have undefined behavior, so
anything is fair game.)

-Jan


More information about the cs631apue mailing list