![]() |
| | #1 |
| Registered User Join Date: Mar 2009
Posts: 21
| execv failed :( I almost shame to ask, but can you help me with this. When doing this: Code: char* av[] = {
"/bin/ls", "-al"
}
if(execv(av[0], av) <0)
printf("Failed to execute command.\n");
else printf("Yeah! It works!\n");
But when I do the following, the execution keeps failing: Code: 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");
I hope you can help me out, thanks. Last edited by Andaluz; 09-24-2009 at 03:49 AM. |
| Andaluz is offline | |
| | #2 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,439
| 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.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #3 |
| Registered User Join Date: Mar 2009
Posts: 21
| 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 Code: 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");
So I tried several ways, like: Code: 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
}
I'm just one step from succes ![]() thank you... |
| Andaluz is offline | |
| | #4 |
| Registered User Join Date: Mar 2009
Posts: 21
| Ok guys, I finally got it, though it was very very frustrating man. To execute a program with crazy options, try this out: Code: 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");
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. Last edited by Andaluz; 09-24-2009 at 08:28 AM. Reason: Forgot '[]' at arguments. |
| Andaluz is offline | |
| | #5 | |
| Jaxom's & Imriel's Dad Join Date: Aug 2006 Location: Alabama
Posts: 801
| I'd make one change. . . Quote:
Code: char* arguments[] = {
"somecmd",
"-c",
"604",
"-n",
"1",
"-f",
"550",
(char*) 0
};
Code: char* arguments[] = {
"somecmd",
"-c",
"604",
"-n",
"1",
"-f",
"550",
NULL
};
| |
| Kennedy is offline | |
| | #6 | |
| Registered User Join Date: Oct 2007
Posts: 22
| Quote:
This way compiler will tell you where to look. | |
| Valery Reznic is offline | |
| | #7 |
| Registered User Join Date: Mar 2009
Posts: 21
| 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!. |
| Andaluz is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Deleting an object if the constructor failed. | g4j31a5 | C++ Programming | 31 | 10-03-2009 08:52 PM |
| Find Injected DLLs In A Process? | pobri19 | Windows Programming | 33 | 02-07-2009 04:37 AM |
| Thread Prog in C language (seg fault) | kumars | C Programming | 22 | 10-09-2008 01:17 PM |
| C++ text file | statquos | C++ Programming | 7 | 06-12-2008 01:42 PM |
| execv and defunct process | groorj | C Programming | 5 | 08-05-2005 07:37 AM |