⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bzshell.c

📁 shell 实现 操作系统课程设计 实现一个shell程序
💻 C
字号:
#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <sys/wait.h>#include <fcntl.h>#include <string.h>char *parse_cmd(char *cmd, char **argarr, int *narg, int *s) {        enum states { S_START, S_IN_TOKEN, S_IN_QUOTES, S_SEMI };        int argc=0;        int loop=1;        enum states state = S_START;        int lastch;        while (loop) {                switch (state) {                case S_START:                        if (*cmd == '"') {                                *argarr++ = cmd + 1;                                ++argc;                                state = S_IN_QUOTES;                        }                        else if (*cmd == 0 || *cmd == ';' || *cmd == '|' || *cmd == '>' || *cmd == '<')                                loop = 0;                        else if (*cmd <= ' ')                                *cmd = 0;                        else {                                *argarr++ = cmd;                                ++argc;                                state = S_IN_TOKEN;                        }                        break;                case S_IN_TOKEN:                        if (*cmd == 0 || *cmd == ';' || *cmd == '|' || *cmd == '>' || *cmd == '<')                                loop = 0;                        else if (*cmd <= ' ') {                                *cmd=0;                                state=S_START;                        }                        break;                case S_IN_QUOTES:                        if (*cmd == 0)                                loop = 0;                        else if (*cmd == '"') {                                *cmd = 0;                                state = S_START;                        }                }                cmd++;        }        *argarr = NULL;        if(narg != NULL) *narg = argc;        lastch = cmd[-1];        cmd[-1] = 0;        if( lastch == '|')            *s = 1;        else if( lastch == '>')            *s = 2;        else if( lastch == '<')            *s = 3;        else *s = 0;        return (lastch == ';' || lastch == '|' || lastch == '>'|| lastch == '<') ? cmd : NULL;}int main(int argc, char **argv) {        char cmd[80];        char *source = NULL;        char *arg[21];        int statval;        char *prompt="bzsh";        char *test_env;        int numargs;        int state;        int fd;        char *file;	char path[100];	FILE *fp;	int pipepre=0;	int prepipe[2];	int postpipe[2];          if (test_env=getenv("BSPROMPT"))                prompt=test_env;        while (1) {                if(source == NULL) {			getcwd(path, sizeof(path));                        printf("%s[%s]#", prompt, path);                        source = gets(cmd);                        if(source == NULL) exit(0);                }                source = parse_cmd(source, arg, &numargs, &state);                if (numargs == 0) continue;                if (!strcmp(arg[0],"exit"))                                exit(0);		while(*source == ' ')			source ++;                if( state == 2 || state == 3 )               	{                    file = source;                    while( *source != ' ' && *source != 0 )                        source ++;		    *source = 0;		    source++;		    while(*source == ' ')			    source ++;                    if(*source == 0) source = NULL;	       	}		if(!strcmp(arg[0], "cd"))		{			chdir(arg[1]);			continue;		}		prepipe[0]=postpipe[0];		prepipe[1]=postpipe[1];		pipe(postpipe);                if (fork()==0) {                    if(state == 2 )                    {                        fd = creat( file, 0644 );                        dup2( fd, 1 );                        close( fd );                    }                    if(state == 3 )                    {                        fd = open( file, O_RDONLY );                        dup2( fd, 0 );                        close( fd );                    }		    if(pipepre == 1)		    {			    close(prepipe[1]);			    dup2(prepipe[0], 0);			    close(prepipe[0]);		    }		    if(state == 1)		    {			    close(postpipe[0]);			    dup2(postpipe[1], 1);			    close(postpipe[1]);		    }		    execvp(arg[0],arg);                    printf("failed\n");                    exit(1);                }		else{			if(pipepre == 1)			{				close(prepipe[0]);				close(prepipe[1]);			}			wait(&statval);			if(state == 1)				pipepre = 1;			else pipepre = 0;		}        }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -