wannabe_cpgeek
asked on
Execvp function
I have written a program to parse user commands and send them to execvp to execute, which it does, but after the execvp nothing happens. I want it to print out a message, but it just gets stuck. Am I missing something after the execvp call?
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char comm[100];
char *pnt[100];
int i=0;
int pid;
int status;
if (argc==1 || argc >2){
printf("Incorrect number of arguments. Exiting\n");
exit(0);
}
printf("%s ", argv[1]);
gets(comm);
pnt[i] = strtok (comm," ");
while (pnt[i] != NULL){
pnt[++i]=strtok(NULL," ");
}
if ((pid = fork()) < 0){
printf("Error unable to fork");
exit(1);
}
else if (pid == 0){
int count=1;
printf("Executing command no:%d\n",count);
execvp(*pnt,pnt);
printf("Command no. %d was executed\n",count);
count++;}
else{ while (waitpid(pid));
printf("Would you like to enter another command?");
exit(0);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
use popen() to run the commands. That opens the called program as a process and you can read from the "stream" just like reading from a FILE * (actually popen returns a FILE*).
Read the man page on popen or google for a sample.
Read the man page on popen or google for a sample.
ASKER
Thank you, but my assignment saids I have to use execvp, so is there a way using it?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER