C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-24-2009, 03:45 AM   #1
Registered User
 
Join Date: Mar 2009
Posts: 21
execv failed :(

Hello guys,

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");
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:
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");
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.

Last edited by Andaluz; 09-24-2009 at 03:49 AM.
Andaluz is offline   Reply With Quote
Old 09-24-2009, 04:17 AM   #2
Cat without Hat
 
CornedBee's Avatar
 
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   Reply With Quote
Old 09-24-2009, 04:50 AM   #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");
But than the arguments seems not be recognized somehow.

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
}
However, the called program respond with missing argument.
I'm just one step from succes
thank you...
Andaluz is offline   Reply With Quote
Old 09-24-2009, 08:26 AM   #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");
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.

Last edited by Andaluz; 09-24-2009 at 08:28 AM. Reason: Forgot '[]' at arguments.
Andaluz is offline   Reply With Quote
Old 09-24-2009, 12:56 PM   #5
Jaxom's & Imriel's Dad
 
Kennedy's Avatar
 
Join Date: Aug 2006
Location: Alabama
Posts: 801
I'd make one change. . .
Quote:
Originally Posted by man execv
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
Code:
char* arguments[] = {
  "somecmd",
  "-c",
  "604",
  "-n",
  "1",
  "-f",
  "550",
  (char*) 0
};
to
Code:
char* arguments[] = {
  "somecmd",
  "-c",
  "604",
  "-n",
  "1",
  "-f",
  "550",
 NULL
};
but that is because I don't believe in the use of magic numbers.
Kennedy is offline   Reply With Quote
Old 09-24-2009, 11:33 PM   #6
Registered User
 
Join Date: Oct 2007
Posts: 22
Quote:
Originally Posted by Andaluz View Post
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");
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.
Valery Reznic is offline   Reply With Quote
Old 09-25-2009, 01:41 AM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:07 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22