写一个shell,需要循环以下几个过程:
1.获取命令行
2.解析命令行
3.建立一个子进程
4.替换子进程
5.父进程等待子进程退出
代码:
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *argv[8];
int argc = 0;
void do_parse(char* buf)
{
int i;
int status = 0;
for(argc=i=0; buf[i]; i++){
if(!isspace(buf[i])&& status == 0){
argv[argc++] = buf + i;
status = 1;
}else if(isspace(buf[i])){
status = 0;
buf[i] = 0;
}
}
argv[argc] = NULL;
}
void do_execute(void)
{
pid_t pid = fork();
}
void do_execute(void)
{
pid_t pid = fork();
switch( pid ){
case -1:
perror("fork");
exit(EXIT_FAILURE);
break;
case 0:
execvp(argv[0], argv);
perror("execvp");
defalt:
{
int st;
while(wait(&st) != pid)
;
}
}
}
int main()
{
char buf[1024] = {};
while(1){
printf("myshell>");
scanf("%[^\n]%*c", buf);
do_parse(buf);
do_execute();
}
return 0;
}