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

📄 main.c

📁 一个实现基本功能的shell命令解释器
💻 C
字号:
/*********************************************************///Company: SunPlus APP CO.LTD//Author : Moxudong//Func   : main.c//Verison: V1.0/*********************************************************//*********************************************************///Founction file name: main.c myfun.h mysh.h//Args: void//Return: none//Author: Moxudong//Verison: V1.0/*********************************************************/#include "myfun.h"pid_t pid = 1;int status;int main(void){//	pid_t pid = 1;//	signal(SIGTSTP, ctrl_z);//	signal(SIGCHLD, sig_child);	int wt, sig, num[1];	num[0] = 0;	char  buff[MAX/4] = "\0"		 ,first[MAX/4] = "\0"		 ,temp[MAX/8] = "\0"	     ,*argv[5]		 ,cmd_histry[50][50]		 ,*path_rec[10] /*store all path var in pointer[]*/		 ,path[MAX/8] = "\0";	char app;	strcpy(path, getenv("PATH"));		if(is_founded() < 0)/* make my_profile file, and create the PATH env */	{		printf("My_profile not found! Creat it now!\n");		init_enveriment(path, path_rec);	}	else	{		read_enveriment(path, path_rec);/*this func cut PATH to *path_rec[]*/	}		while(1)/* main loop */	{		if(pid == 0) /* child ID */		{				execv(temp, argv);			printf("No such command: %s\n", argv[0]);			exit(ERROR_EXIT);		}/* child ID */		else if(pid > 0)/* parent ID */		{			p_info();/* print [user@ root]# */			if((app = getch()) < 0)			{				strcpy(buff, histry_cmd(cmd_histry, &app, num, path_rec));			}			else			{				printf("%c", app);				first[0] = app;				first[1] = '\0';				fgets(buff, MAX/8, stdin);				buff[strlen(buff)-1] = '\0';				strcat(first, buff);				strcpy(buff, first);				add_histry(buff, cmd_histry, num);			}			if(strncmp(buff, "cd", 2) == 0)			{				cmd_cut(buff, argv);				if(chdir(argv[1]) < 0)					printf("No such dir: %s\n", argv[1]);				continue;			}			else if(strncmp(buff, "exit", 4) == 0)			{				printf("Bye bye !\n");				exit(NORMAL_EXIT);			}			else if(strchr(buff, '|') != NULL)			{				my_pipe(buff, path_rec);/* pipe function */				continue;			}			else if(strchr(buff, '>') != NULL)			{				redirect(buff);/* redirect function */				continue;			}			else if(strchr(buff, '&') != NULL)			{				background(buff, path_rec);				continue;			}			else if(strncmp(buff, "./", 2) == 0 || strncmp(buff, "fg", 2) == 0												|| strncmp(buff, "bg", 2) == 0)			{				jobs(buff);				continue;			}			else if(strcmp(buff, "jobs") == 0)			{				show_job();				continue;			}			else			{				if((sig = cmd_cut(buff, argv)) < 0)				{					continue;				}				if(bin_founded(temp, path_rec, argv[0]) == -1)/* find the input cmd */				{					printf("Command not found!\n");					continue;				}				if((pid = fork()) < 0)				{					perror("fork()");					continue;				}				wait(&wt);			}		}/* parent ID */	}/* main loop (while) */	return 0;}/* main */

⌨️ 快捷键说明

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