LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-04-2004, 08:29 AM   #1
fosh
LQ Newbie
 
Registered: Jul 2004
Posts: 2

Rep: Reputation: 0
c++ exec


hi guys,
what can i call in linux to execute a command , for instance, another program, and what do i have to include / link against to access said function?
i was thinking it might be something along the lines of exec() or system(), but cant find anything.
cheers

-scott
 
Old 07-04-2004, 09:00 AM   #2
Quis
Member
 
Registered: May 2001
Location: Germany, Süd-Baden
Distribution: Gentoo
Posts: 139

Rep: Reputation: 15
You can execute a binary in your own program with the exec-family.
try "man execlp".
Be aware, if you execute a binary with an exec* command you will override your old program,
so you have to fork() a new process if you want anything from the executed binary.

Here is an example of execlp() and fork():
/*
"prg" is the binary to execute
"arg1" is an argument1 for the binary
"arg2" is an argument2 for the binary
"type" controls the output: 1 is stdout, 2 is stderr
The funtion returns:
-1 in case of an incorrect "type"
-2 if no pipe could be created
-3 if a process couldn't be forked
str a string from stdout or stderr.
*/

char* execSub(char *prg, char *arg1, char *arg2, int type)
{
pid_t Pid;
int pipefd[2];
char buffer[5000];
int anz, i, status;
char *str;

status=0;
anz=0;
str=NULL;

/* pipe is | */
if(pipe(pipefd)==-1){ //PIPE-error
str=(char*) malloc(2);
strcpy(str, "-2");
return str;}

switch((Pid=fork())) {
case 0:{ //---------------Child
close(pipefd[0]);
if(type==0)
close(1);
if(type==1)
close(2);
else{
str=(char*) malloc(2);
strcpy(str, "-1");
return str;}
dup(pipefd[1]);
execlp(prg, prg, arg1, arg2, NULL); //prg gets executed
exit(0);
}
case -1: { //FORK-error
str=(char*) malloc(2);
strcpy(str, "-3");
return str;}
default:{ //---------------Parent
wait(&status);
close(pipefd[1]);
printf(" executing %s ", prg);
while((anz=read(pipefd[0],buffer,5000))>0){
if(str==NULL)
str=(char*) calloc((anz+1), sizeof(anz));
else if(buffer==NULL && str !=NULL)
str=(char*) realloc(sizeof(anz+1));
for(i=0; i<anz;i++)
str[i]=buffer[i];
}
}
return str;
}
}

I hope this helps.
 
Old 07-04-2004, 11:44 AM   #3
fosh
LQ Newbie
 
Registered: Jul 2004
Posts: 2

Original Poster
Rep: Reputation: 0
cheers, ill try that out asap
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot exec 'cc1plus' martine Mandriva 3 02-26-2005 12:11 PM
php exec devit Programming 4 04-08-2004 04:26 AM
find -exec cricbk Linux - Newbie 4 01-05-2004 08:03 PM
cannot exec as person Linux From Scratch 5 12-19-2003 05:31 AM
exec, a file or something.. how..? WiZaC Programming 5 10-03-2003 09:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration