Re: execv fails with EFAULT

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Prasanta Sadhukhan wrote:

> >> When I tried the following program
> >> void test4()
> >> {
> >>     printf("doing execv\n");
> >>     if(execv("/bin/ls", "-l") == -1)
> >>         printf("exec failed with errno %d\n", errno);
> >>     else
> >>         printf("exec succeed\n");
> >> }
> >
> >
> > Prasanta,
> >
> > A NULL terminated array of arguments must be passed to execv(2) as the
> > prototype indicates: int execv(const char *path, char *const argv[]);
> >
> > For example:
> >
> > #include <unistd.h>
> >
> > char *cmd[] = { "ls", "-l", NULL };
> > int result = execv ("/bin/ls", cmd);
> >
> > An error of EFAULT usually means that an argument points to an illegal 
> > address.
> >
> >     \Steve
> 
> Thanks Steve... If I have a string like this sprintf(str, 
> "-Xparameter:%d %s, value, command"), how to make it NULL terminated. Is 
> this declaration char *cmd[] = {str, NULL} and invocation
> execv(path, cmd) correct?

Probably not. It's correct in that the second argument to execv() has
the correct type, but if you're trying to pass multiple arguments, it
won't work.

It's more likely that you want something like:

	char str[20];
	char *args[4];

	sprintf(str, "-Xparameter:%d", value);

	args[0] = path;
	args[1] = str;
	args[2] = command;
	args[3] = NULL;

	result = execv(path, args);

This assumes that "command" is meant to be a single argument, not a
list of arguments. If it's a list (e.g. with individual items
separated by spaces), you'll have to split it back into a list before
passing it to execv().

-- 
Glynn Clements <glynn@xxxxxxxxxxxxxxxxxx>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux