支持简单的ls、-a、-l、top等命令,|(管道)、>(重定向)等不支持
shell.c代码
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#define NUM 32
using namespace std;
int main()
{
char buff[1024] = {0};
for(;;)
{
string command;
string tips = "[XXX@localhost YYY]# ";
cout << tips;
fgets(buff,sizeof(buff)-1,stdin);
buff[strlen(buff)-1] = 0;
// cout << buff << endl;
char *argv[NUM];
argv[0] = strtok(buff," ");
int i = 0;
while(argv[i]!=NULL)
{
i++;
argv[i] = strtok(NULL," ");
}
pid_t id = fork();
if(id == 0)
{
cout << "child running..." << endl;
execvp(argv[0],argv);
exit(123);
}
else
{
int status = 0;
waitpid(id,&status,0);
cout << "Exit Code:" << WEXITSTATUS(status) << endl;
}
}
return 0;
}
结果截图