简易 inux shell 编辑器

<pre name="code" class="cpp">#include <unistd.h>
#include <stdio.h>
#include <pwd.h>
#include <sys/types.h>
int word_count=0;
void type_prompt(){
    char name[50],direct[50];
    struct passwd *my_info;
    my_info = getpwuid(getuid());
    gethostname(name,sizeof(name));
    getcwd(direct,sizeof(direct));
    printf("[MY_shell]%s@%s:%s$ ",my_info->pw_name,name,direct);
}

void read_command(char command[],char *parameters[]){      
    char input[50],*left,*right;
    int let_count=0;
    fgets(input,sizeof(input),stdin);
    word_count = 0;
    
    left = input;right = input;
    while(*right!=10){
        let_count = 0;
        while(*right!=10&&*right!=32){
            right++;
        }
        while(left!=right){
            parameters[word_count][let_count] = *left;
            if(word_count==0){command[let_count]=*left;}
            let_count++;
            left++;
        }
        if(word_count==0){command[let_count]='\0';}
        parameters[word_count][let_count]='\0';
        word_count++;
        if(*left==10||*right==10){break;}
        while(*right==32){right++;}
	if(*right ==10){break;}
        left = right;
    }
    parameters[word_count] = NULL;
}

int main(){
    int i=0,pipe_posi,is_pipe=0,file_pipes[2];
    char command[50],*parameters[5];
    char p1[50],p2[50],p3[50],p4[50],p5[50];
    pid_t pid,pid2;
    while(1){
        is_pipe = 0;
        command[0]='0';
        parameters[0] = p1;parameters[1] = p2;
        parameters[2] = p3;parameters[3] = p4;
        parameters[4] = p5;
        type_prompt();
        read_command(command,parameters);
        if(command[0]=='e'&&command[1]=='x'&&command[2]=='i'&&command[3]=='t'){  // if exit then quit.
            return 0;  
        }
	if(command[0]=='c'&&command[1]=='d'){                                        // if cd then change directory 
		chdir(parameters[1]);
	}
	i=0;
	
	while(i<=word_count -1){ 
		if(parameters[i]==NULL){break;}	
		if(parameters[i][0]=='|'){
			pipe_posi = i;
			is_pipe = 1;
			parameters[i] = NULL;
			break;
		}
		i+=1;
		}
	if(is_pipe){
		pipe(file_pipes);
		pid = fork();
		if(pid!=0){
				waitpid(pid,0,NULL);
			}
			
			else{
				pipe(file_pipes);
				if(fork()!=0){
				//read from pipe 
				close(file_pipes[1]);
				close(0);
				dup(file_pipes[0]);
				execvp(parameters[pipe_posi+1],parameters+pipe_posi+1);
                }   
			else{
			//write into pipe   
                close(file_pipes[0]);
                close(1);
                dup(file_pipes[1]);
                execvp(parameters[0],parameters);
                }
			}
		
		
        }
	else{
	        if(fork()!=0){
            	wait(NULL);
        	}
        	else{
            	execvp(command,parameters);
        	}
        }
    }
}	



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值