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

📄 shell_minix.c

📁 linux中的shell的实现 包括代码和题目
💻 C
📖 第 1 页 / 共 2 页
字号:
		for(i=0;i<k;i++)			free(analized_order[i]);		free(analized_order);		free(real_order);		return -1;	}	if((pid = fork()) == 0) 	{	    if(flag_out==1)			fd_out = open(out_filename,O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR );		if(flag_out==2)			fd_out = open(out_filename, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR );		if(flag_in==1)			fd_in = open(in_filename, O_RDONLY, S_IRUSR|S_IWUSR );		if(flag_in==2)			fd_in = open(in_filename, O_RDONLY, S_IRUSR|S_IWUSR );        if(fd_out==-1) 		{			printf("Open out %s error \n", out_filename);			return -1;		}		if(fd_in==-1) 		{			fprintf(stderr,"Open in %s error \n", in_filename);			return -1;		}		if(flag_out>0)			if(dup2(fd_out, STDOUT_FILENO) == -1)			{				fprintf(stderr,"Redirect Standard Out Error !\n");				exit(1);			}		if(flag_in>0)			if (dup2(fd_in,STDIN_FILENO)==-1)			{				fprintf(stderr,"Redirect Standard Out Error !\n");				exit(1);			}		execv(order_path,analized_order);		exit(1);			}	else                     	if(back==0)       		pid=waitpid(pid, &status, 0);		free(out_filename);	free(in_filename);	free(order_path);	for(i=0;i<k;i++)   		free(analized_order[i]);	free(analized_order);	return 1;}int is_back(char *order)	{	int len=strlen(order);	if(order[len]=='&')	{		order[len]='\0';		return 1;	}	else return 0;}char *is_file_exist(const char *order)		{	char * path, * p;	char *buffer;	int i,max_length;	i = 0;	path=getenv("PATH");	strcat(path,":");	p=path;	max_length=strlen(path)+strlen(order)+2;	if((buffer=(char *)malloc(max_length*(sizeof(char))))==0)	{		fprintf(stderr,"error! can't malloc enough space for buffer\n");		return NULL;	}	while(*p != '\0') 	{			if(*p != ':')  			buffer[i++] = *p;		else 		{		buffer[i++] = '/';		buffer[i] = '\0';				strcat(buffer,order);		if(access(buffer,F_OK) == 0)     			return buffer;		else                          			i=0;		}		p++;	}	strcpy(buffer,order);	if(access(buffer,F_OK) == 0)     		return buffer;	return NULL;}int number(const char *input)	{	int i=0,k=0,fj=0;	int input_len=strlen(input);	k=0;	for (i=0;i<input_len;i++)	{		if(input[i]==' '||input[i]=='<'||input[i]=='>'||input[i]=='	')		{			fj=0;			continue;		}		else 		{			if(fj==0)			{				fj=1;				k++;			}		}	}	return k;}char **analize(const char *input)	{	int number(const char *input);			int i,j,k;		int input_len;	int is_back=0;	char *buffer;	char **arg;	input_len=strlen(input);	if((buffer=(char *)malloc((input_len+1)*(sizeof(char))))==0)	{		fprintf(stderr,"error! can't malloc enough space for buffer\n");		return NULL;	}		k=number(input);	if((arg=(char **)malloc((k+1)*sizeof(char *)))==0)	{		fprintf(stderr,"error! can't malloc enough space for arg\n");		return NULL;	}	for (i=0,j=0,k=0;i<=input_len;i++)	{		if(input[i]==' '||input[i]=='<'||input[i]=='>'||input[i]=='	'||input[i]=='\0')		{			if(j == 0) 				continue;			else 			{				buffer[j] = '\0';				j++;				arg[k] = (char *)malloc(sizeof(char)*j);								strcpy(arg[k], buffer);				j=0; 				k++;			}		}		else 		{   			if(input[i]== '&' && input[i+1]=='\0') 			{				is_back = 1;				continue;			}			buffer[j]=input[i];			j++;		}	}		arg[k]=NULL;	free(buffer);	return arg;}char *read_order(char *buffer){	char lc_char;	char *input;	int input_lenth=0;	lc_char = getchar();	while(lc_char != '\n' && input_lenth < MAXSIZE) 		{			buffer[input_lenth] = lc_char;			lc_char = getchar();			input_lenth++;		}	if(input_lenth >= MAXSIZE) 	{		fprintf(stderr,"Your command too long ! Please reenter your command !\n");		input_lenth = 0;		return NULL;	}	else  	buffer[input_lenth] = '\0';	if((input=(char *)malloc(sizeof(char)*(input_lenth+1)))==0)	{		fprintf(stderr,"error! can't malloc enough space for input\n");		return NULL;	}	strcpy(input, buffer);	return input;}/*void CDcommand(char *argv[]){ 	if(argv[1]!=NULL)	{ 		if(chdir(argv[1])<0)		{ 			switch(errno)			{				case ENOTDIR: 					printf("@ myshell -> SYSTEM @ ERROR! IT IS NOT A DIRECTORY.\n"); 					break; 				case ENOENT: 					printf("@ myshell -> SYSTEM @ ERROR! DIRECTORY IS NOT FOUND.\n"); 					break;  				case EACCES: 					printf("@ myshell -> SYSTEM @ ERROR! YOU HAVE NOT ENOUGH RIGHT.\n"); 					break; 				default: 					printf("@ myshell -> SYSTEM @ ERROR! THERE EXISTS SOME ERROR.\n"); 			} 		}	}}int DisjointCount(char* command){	int i=0,head=0,count=0;	for (i=0;i<strlen(command);i++)	{		if(command[i]==' ')		{			head=0;			continue;		}else 		{			if(head==0)			{				head=1;				count++;			}		}	}	return count;}char **Disjoint(char *command_string){	int DisjointCount(char* command);	void ShowMallocError(void);	char *buffer;	char **command;	int i=0,k=0,head=0,count=DisjointCount(command_string);	if((buffer=(char *)malloc((strlen(command_string)+1)*(sizeof(char))))==0)	{		ShowMallocError();		return NULL;	}	if((command=(char **)malloc((count+1)*sizeof(char *)))==0)	{		ShowMallocError();		return NULL;	}	for (i=0,head=0,count=0;i<=strlen(command_string);i++)	{		if(command_string[i]==' '||command_string[i]=='\0')		{			if(head==0) 				continue;			else 			{				buffer[head] = '\0';				head++;				command[count] = (char *)malloc(sizeof(char)*head);							strcpy(command[count], buffer);				head=0; 				count++;			}		}		else 		{   			if(command_string[i]== '&' && command_string[i+1]=='\0') 				continue;			buffer[head]=command_string[i];			head++;		}	}	command[count]=NULL;	free(buffer);	return command;} int OperateCommand(char *command_string0){	char *GetPath(char *command_string);	void CDcommand(char *argv[]);	int CheckDeamon(char *command);		char **Disjoint(char *command_string);	int deamon=CheckDeamon(command_string0);	char **command_string;	char *path;	pid_t pid;	int i=0;	int status;	int count=DisjointCount(command_string0);	command_string=Disjoint(command_string0);	if (command_string[0]==NULL)	{		printf("@ myshell -> SYSTEM @ INCORRECT COMMAND!.\n");		for(i=0;i<count;i++)			free(command_string[i]);		free(command_string);		free(command_string0);		return 1;	}	if (strcmp(command_string[0],"leave")==0)	{		printf("@ myshell -> SYSTEM @ Thank you for using ZML's shell.\n");		for(i=0;i<count;i++)			free(command_string[i]);		free(command_string);		free(command_string0);		exit(1);		return 1;	}	if (strcmp(command_string[0],"cd")==0)	{		CDcommand(command_string);		for(i=0;i<count;i++)			free(command_string[i]);		free(command_string);		free(command_string0);		return 1;	}	if((path=GetPath(command_string[0]))==NULL)	{		printf("@ myshell -> SYSTEM @ Error! This command is not found.\n");		for(i=0;i<count;i++)			free(command_string[i]);		free(command_string);		free(command_string0);		return -1;	}	if((pid = fork())==0)	{		execv(path,command_string);		exit(1);			}	else if(deamon==0)        		pid=waitpid(pid, &status, 0);	free(path);	for(i=0;i<count;i++)   		free(command_string[i]);	free(command_string);	return 1;}int CheckDeamon(char *command)	{	int length=strlen(command);	if(command[length]=='&')	{		command[length]='\0';		return 1;	}	return 0;}char* GetPath(char *command_string){	void ShowMallocError(void);	char *path,*path0,*pathname;	int i=0;		path=getenv("PATH");	strcat(path,":");	path0=path;	if((pathname=(char *)malloc((strlen(path)+strlen(command_string)+2)*(sizeof(char))))==0)	{		ShowMallocError();		return NULL;	}	do{		if((*path0!=':')&&(*path0!='\0'))		{  			pathname[i++]=*path0;		}else{			pathname[i++]='/';			pathname[i]='\0';			strcat(pathname,command_string);			if(access(pathname,F_OK)==0)				return pathname;			else                          				i=0;		}		path0++;	}while(*path0!='\0');	strcat(pathname,command_string);	if(access(pathname,F_OK)==0)		return pathname;	return NULL;}int OperateCommand(char *command_string0);for (i=0;i<command_parts;i++)				OperateCommand(command[i]);*/

⌨️ 快捷键说明

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