#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
int main()
{
char buf[128];
while(1){
printf("[test@my-host-name myshell]");
fflush(stdout);
sleep(3);
ssize_t _s = read(0,buf,sizeof(buf) - 1);
if(_s > 0){
buf[_s-1] = '\0';
}else{
perror("read");
return 1;
}
char* argv[32];
argv[0] = buf;
int i = 1;
char *start = buf;
while(*start){
if(isspace(*start)){
*start = '\0';
start++;
argv[i] = start;
i++;
// printf("i = %d\n",i);
continue;
}
start++;
}
// printf("NULL : i = %d\n",i);
argv[i] = NULL;
pid_t id = fork();
if(id < 0){
perror("fork");
}else if(id == 0){//child
execvp(argv[0],argv);
exit(1);
}else{
int status = 0;
pid_t ret = waitpid(id, &status,0);
if(ret > 0 && WIFEXITED(status)){
// printf("exit code:%d\n",WEXITSTATUS(status));
}else{//father
perror("waitpid");
}
}
}
return 0;
}