Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using exec call with uknown amount of args...

Status
Not open for further replies.

GooMooPunch

Programmer
Jan 26, 2004
5
US
Hello,

I have a shell program with a function that handles external unix calls, but I can't figure out how to make it so I can use an exec call because the amount of arguments being sent to the function is different for different external commands...

int exCmdhandler(char* cmd, char* args[]){
pid_t child = fork();
if(child == 0)
if(execlp(cmd, cmd, NULL) == -1) {
printf("Error: %s\n", srerror(errno));
return 1;
}
else if(child != -1) {
int status;
waitpid(child, &status, 0);
}
return 0;
}
-----------
I've tried using execvp and passing the args array to it, but it gives me a Bad Address error...
 
The function you want is
Code:
int execv(const char *path, char *const argv[]);

So it would be used as
Code:
if(execv(cmd, args) == -1)

> but it gives me a Bad Address error...
Did you end your argv[] with a NULL, like so
Code:
char *args[] = {
  "hello",
  "world",
  NULL,
};

For better code formatting, please surround your code with [ignore]
Code:
[/ignore] tags

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top