LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 07: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, 08: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, 10:44 AM   #3
fosh
LQ Newbie
 
Registered: Jul 2004
Posts: 2

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


Reply



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 11:11 AM
php exec devit Programming 4 04-08-2004 03:26 AM
find -exec cricbk Linux - Newbie 4 01-05-2004 07:03 PM
cannot exec as person Linux From Scratch 5 12-19-2003 04:31 AM
exec, a file or something.. how..? WiZaC Programming 5 10-03-2003 08:37 PM

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

All times are GMT -5. The time now is 11:20 AM.

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
Open Source Consulting | Domain Registration