Como usar execvp ()
O usuário lerá uma linha e manterei a primeira palavra como um comando para execvp.
Vamos dizer que ele digitará"cat file.txt" ... comando será gato. Mas não sei como usar issoexecvp()
, li alguns tutoriais, mas ainda não o entendi.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *buf;
char command[32];
char name[32];
char *pointer;
char line[80];
printf(">");
while((buf = readline(""))!=NULL){
if (strcmp(buf,"exit")==0)
break;
if(buf[0]!=NULL)
add_history(buf);
pointer = strtok(buf, " ");
if(pointer != NULL){
strcpy(command, pointer);
}
pid_t pid;
int status;
if ((pid = fork()) < 0) {
printf("*** ERROR: forking child process failed\n");
exit(1);
}
else if (pid == 0) {
if (execvp(command, buf) < 0) {
printf("*** ERROR: exec failed\n");
exit(1);
}
}
else
while (wait(&status) != pid)
;
free(buf);
printf(">");
}///end While
return 0;
}