PDA

View Full Version : execv failed :(



Andaluz
09-24-2009, 03:45 AM
Hello guys,

I almost shame to ask, but can you help me with this.
When doing this:


char* av[] = {
"/bin/ls", "-al"
}

if(execv(av[0], av) <0)
printf("Failed to execute command.\n");
else printf("Yeah! It works!\n");


This works fine and the command is executing fine. And the output seems to be correct, this means it shows all directories/files in the current directory and also "-l" properties.

But when I do the following, the execution keeps failing:


char* avlong = {
"~/Development/Projects/somecmd",
"-c 604 -n 1 -f 550"
}

if(execv(avlong[0], avlong) <0)
printf("Failed to execute command.\n");
else printf("Yeah! It works!\n");


My goal is to call a function for executing a process with options that is determined during runtime. The called process is my other self written program.
I hope you can help me out, thanks.

CornedBee
09-24-2009, 04:17 AM
Call perror() to print out a text version of the error execv encountered.

However, I'm going out on a limb and say that it doesn't find the executable. The reason is that replacing ~ by the home directory is a shell substitution, whereas execv wants the real path of the executable. Try execvp instead.

Andaluz
09-24-2009, 04:50 AM
Thanks for your answer CornedBeef,

You are right , it has to do with that tilde '~'.
So I modified it to: /home/userme/Development/Projects/somecmd


char* avlong = {
"/home/userme/Development/Projects/somecmd",
"-c 604 -n 1 -f 550"
}

if(execv(avlong[0], avlong) <0)
printf("Failed to execute command.\n");
else printf("Yeah! It works!\n");

But than the arguments seems not be recognized somehow.

So I tried several ways, like:


char* avlong = {
"/home/userme/Development/Projects/somecmd",
"somecmd",
"-c 604",
"-n 1",
-f 550",
"\0"
}

//even this:
char* avlong = {
"/home/userme/Development/Projects/somecmd",
"somecmd",
"-c",
"604",
"-n",
"1",
"-f",
"550",
(char*) 0
}

However, the called program respond with missing argument.
I'm just one step from succes :confused:
thank you...

Andaluz
09-24-2009, 08:26 AM
Ok guys,

I finally got it, though it was very very frustrating man.
To execute a program with crazy options, try this out:


char* program = "/home/userme/Development/Projects/somecmd";
char* arguments[] = {
"somecmd",
"-c",
"604",
"-n",
"1",
"-f",
"550",
(char*) 0
};

if(execv(program, arguments) <0) //it works also with execvp() call.
printf("Failed to execute command.\n");
else printf("Yeah! It works!\n");

This should work, if I haven't made some mistypes.
Ok, you might ask yourself how I solved it. Well, sometimes you want to get your code done quick for simple tasks and don't want to waste to much time on it. But with exec() calls, if you didn't read the man carefully, you'll end up with a broken keyboard. So I calmed down, opened the Advanced C programming - Linux ebook and read the chapter about process carefully. Tried out the example code and taraaa, it works!

At your service.

Kennedy
09-24-2009, 12:56 PM
I'd make one change. . .
The execv() and execvp() functions provide an array of pointers to null-terminated strings that represent the argument list avail-
able to the new program. The first argument, by convention, should point to the filename associated with the file being executed.
The array of pointers must be terminated by a NULL pointer.
I would change
char* arguments[] = {
"somecmd",
"-c",
"604",
"-n",
"1",
"-f",
"550",
(char*) 0
};to
char* arguments[] = {
"somecmd",
"-c",
"604",
"-n",
"1",
"-f",
"550",
NULL
};but that is because I don't believe in the use of magic numbers.

Valery Reznic
09-24-2009, 11:33 PM
Ok guys,

I finally got it, though it was very very frustrating man.
To execute a program with crazy options, try this out:


char* program = "/home/userme/Development/Projects/somecmd";
char* arguments[] = {
"somecmd",
"-c",
"604",
"-n",
"1",
"-f",
"550",
(char*) 0
};

if(execv(program, arguments) <0) //it works also with execvp() call.
printf("Failed to execute command.\n");
else printf("Yeah! It works!\n");

This should work, if I haven't made some mistypes.
Ok, you might ask yourself how I solved it. Well, sometimes you want to get your code done quick for simple tasks and don't want to waste to much time on it. But with exec() calls, if you didn't read the man carefully, you'll end up with a broken keyboard. So I calmed down, opened the Advanced C programming - Linux ebook and read the chapter about process carefully. Tried out the example code and taraaa, it works!

At your service.
While reading man page is very useful thing to do, there is also something else that make life a lot easy - compile you program with warnings at least with -Wall and make sure you fixed all warnings.

This way compiler will tell you where to look.

Andaluz
09-25-2009, 01:41 AM
Hello guys,

Thanks for your replies.

Kennedy, it shouldn't make any difference.
Yes, thanks for the tip. I just took it from the examples. Anyway, the main thing is, I think, is to make sure you have a null-pointer.

Valery Reznic, that's a good tip. Normally I'm used to work on Visual Studio, but since I work with KDevelop, I had to learn a lot of new things. But I totally forgot the -Wall option. Cause I don't like to have warnings, I want it superclean :)

Thank you!.