Linux下自己从0开始动手实现一个shell

源代码

/*************************************************************************
	> File Name: kkbshell.c
	> Author: liuhao
	> Mail: [email protected]
	> Created Time: Mon 13 Sep 2021 07:16:36 PM CST
 ************************************************************************/

#include<stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <ctype.h>
#include <sys/fcntl.h>
#include <sys/stat.h>

#define LEN 1024

char* trim(char*);

int main() {
    
    
    char buff[LEN];
    while (1) {
    
    
        printf("kaikeba$");
        // scanf("%s", buff);
        fgets(buff, LEN, stdin);
        buff[strlen(buff) - 1] = 0;
        printf("cmd:[%s]\n", buff);
        // exit
        if (!strcmp(buff, "exit")) {
    
    
            printf("exit~~\n");
            break;
        }
        // create a sub_process
        pid_t pid = fork();
        if (pid < 0) {
    
    
            perror("fork error");
            exit(1);
        }
        // main_process wait sub_process so that to continue
        if (pid) {
    
    
        	// main_process is sleep until the sub_process awake it
            wait(NULL);
            continue;
        }
        // the follow is sub_process
        int redfd = -1;
        if (strstr(buff, "<")) {
    
    
            redfd = 0;
        }
        if (strstr(buff, ">")) {
    
    
            redfd = 1;
        }
        char* cmd = NULL;
        // if contain > or < ,we can make it two method
        // else the cmd = buff is the command
        if (redfd != -1) {
    
    
            char* token = strtok(buff, "<>");
            printf("token:[%s]\n", token);
            cmd = token;
            token = strtok(NULL, "<>");
            printf("token:[%s]\n", token);

            token = trim(token);
            int fd;
            if (redfd) {
    
    
                fd = open(token, O_RDWR | O_CREAT, 0644);
            }
            else {
    
    
                fd = open(token, O_RDWR);
            }
            if(fd < 0) {
    
    
                perror(token);
                exit(1);
            }
            printf("open %s successfully~\n", token);
            dup2(fd, redfd);
        }
        else {
    
    
            cmd = buff;
        }
        // execvp make it
        int i = 0;
        char* argarr[20];
        char* tk = strtok(cmd, " ");
        while (tk) {
    
    
            argarr[i++] = tk;
            tk = strtok(NULL, " ");
        }
        argarr[i] = tk;
        execvp(argarr[0], argarr);
        perror(argarr[0]);
        // sub_process is ending, the main_process is continue
        exit(1);
    }
    return 0;
}
// trim
char* trim(char* str) {
    
    
    int head = 0;
    int tail = strlen(str) - 1;
    // isspace: whether the str is blank
    while (isspace(str[head])) {
    
    
        head++;
    }
    while (isspace(str[tail])) {
    
    
        str[tail--] = 0;
    }
    return str + head;
}

执行程序

lh@iZwz9hl4wc7w8vb0fryco4Z ~ %gcc kkbshell.c                                     [0]
lh@iZwz9hl4wc7w8vb0fryco4Z ~ %./a.out                                            [0]
kaikeba$ls
cmd:[ls]
a.out  euler	  index.txt   leetcode			     Project	ttt.txt
c      fs	  kegg.csv    libevent-2.1.12-stable	     python
c++    hello.txt  kepp.py     libevent-2.1.12-stable.tar.gz  shelledit
data   http	  kkbshell.c  oj			     tencent
kaikeba$exit
cmd:[exit]
exit~~

猜你喜欢

转载自blog.csdn.net/qq_48322523/article/details/120275216