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

📄 mysh.c

📁 linuxunix 下的c语言实现
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <pwd.h>
#include <libgen.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <time.h>
#include <errno.h>
#include <sys/times.h>
#include <pwd.h>

#include "do_welcom.h"
#include "do_ls.h"
#include "do_cat.h"
#include "do_mkdir.h"
#include "do_rm.h"
#include "do_cd.h"
#include "do_chmod.h"
#include "do_pwd.h"
#include "do_wc.h"
#include "do_date.h"
#include "do_clear.h"
#include "do_kill.h"
#include "do_who.h"
#include "do_exec.h"



char cmd[32];    //user cmd
char cmd_clone[32];    //clone cmd for case 11 : time 

char cmd1[3]="ls";
char cmd2[5]="echo";
char cmd3[4]="cat";
char cmd4[6]="mkdir";
char cmd5[3]="rm";
char cmd6[3]="cd";
char cmd7[6]="chmod";
char cmd8[4]="pwd";
char cmd9[3]="wc";
char cmd10[5]="date";
char cmd11[5]="time";
char cmd12[5]="exit";
char cmd13[5]="help";
char cmd14[5]="kill";
char cmd15[4]="who";
char cmd16[8]="history";
char cmd17[11]="systeminfo";
char cmd18[6]="clear";

/*char* history1=NULL;char* history2=NULL;char* history3=NULL;
char* history4=NULL;char* history5=NULL;char* history6=NULL;
char* history7=NULL;char* history8=NULL;char* history9=NULL;*/
int i = 1;    // counter of history
char* history[64];

char* tmp;     // 第一个参数
char* tmp1;    // 第二个 



void print_prompt();
void get_input();
int parse_input();
int do_cmd(int para);
void savelist(int para);

//*********************************************************************************************************


int main(int argc, char **argv)
{ 
	do_clear();
	do_welcom();
	do_clear();


       while(1){

       print_prompt();

       get_input();

       int cmdPara = parse_input();

       int judge = do_cmd(cmdPara);



		history[i++] = cmd_clone;

	}

	return 0;
}



//**********************************************************************************************



void print_prompt(){
	printf("『 love Coral 』*^__^* "); 
}

void get_input(){
	//scanf("%[^\n]", cmd);    //带空格的scanf 用法,但是,大爷的,不知为什么它总是在循环!!!
	fflush( stdin );    // flush is compulsory for gets()
	gets( cmd ); 
	strcpy(cmd_clone , cmd);
	
}

int parse_input(){

	//cmd is user command , cmd_main is fist part of cmd , cmd_para is second part
	tmp = NULL;               // global var , each time iniatail
	tmp1= NULL;
	char* cmd_para;           // ptr token command seperete  大爷的,段错误~~~~
	char *delim = " ";
	char *cmd_main;
	cmd_main = strtok(cmd, delim);
	while((cmd_para = strtok(NULL, delim)) != NULL){
			if(tmp==NULL) tmp = cmd_para;
			else if(tmp1 == NULL) tmp1 = cmd_para;
	}


	if(!(strcmp(cmd_main,cmd1)))return 1;
	if(!(strcmp(cmd_main,cmd2)))return 2;
	if(!(strcmp(cmd_main,cmd3)))return 3;
	if(!(strcmp(cmd_main,cmd4)))return 4;
	if(!(strcmp(cmd_main,cmd5)))return 5;
	if(!(strcmp(cmd_main,cmd6)))return 6;
	if(!(strcmp(cmd_main,cmd7)))return 7;
	if(!(strcmp(cmd_main,cmd8)))return 8;
	if(!(strcmp(cmd_main,cmd9)))return 9;
	if(!(strcmp(cmd_main,cmd10)))return 10;
	if(!(strcmp(cmd_main,cmd11)))return 11;
	if(!(strcmp(cmd_main,cmd12)))return 12;
	if(!(strcmp(cmd_main,cmd13)))return 13;
	if(!(strcmp(cmd_main,cmd14)))return 14;
	if(!(strcmp(cmd_main,cmd15)))return 15;
	if(!(strcmp(cmd_main,cmd16)))return 16;
	if(!(strcmp(cmd_main,cmd17)))return 17;
	if(!(strcmp(cmd_main,cmd18)))return 18;
	if(do_exec(cmd))return 19;
    return 0;

}

int do_cmd(int para){
	char p[32] ;     //用于time命令的 ,case 11 
	int j = 1;      // for case 16

	switch(para){

		case 0:
			printf("Error!Your command is not surpported by CORAL_SHELL.\n\n");
			break;
		case 1:
			if(tmp){
              if(!(strcmp(tmp,"-l"))) {
                            DIR * dir;
                            struct dirent * ptr;
							struct passwd *user;
							
							user = getpwuid(getuid());
                            dir =opendir(".");

                            while((ptr = readdir(dir))!=NULL){
                               struct stat buf;
                               stat (ptr->d_name,&buf);
                               printf("%d   %d   %s   %d   %d    %s        %s",
                                 buf.st_mode, buf.st_nlink, user->pw_name, getuid(), 
                                 buf.st_size , ptr->d_name, asctime(localtime(&buf.st_mtime)));
                            }
                            closedir(dir);
             }
			 else {
				 perror("Error: ls Parameter is invalid!");
				 return 1;
			 }
		   }
                            
             else do_ls();
			break;
		case 2:
			if(tmp!=NULL)printf("%s\n",tmp);
			else printf(" \n"); 
			break;
		case 3:
			do_cat(tmp);
			break;
		case 4:
			do_mkdir(tmp);
			break;
		case 5:
			do_rm(tmp);
			break;
		case 6:
			do_cd(tmp);
			break;
		case 7:
			do_chmod(tmp,tmp1);
			break;
		case 8:
			do_pwd();
			break;
		case 9:
			do_wc(tmp,tmp1);
			break;
		case 10:
			do_date();
			break;
		case 11:      // deal with 'time'
			if(tmp != NULL)
			{   
				if(strcpy(cmd, strcpy(p ,cmd_clone+5 )) != NULL){
					clock_t start,end ;
					struct tms firtime, sectime ;
					
					start = times(&firtime);    //time began
					do_cmd(parse_input());	
					end   = times(&sectime);     //time up

					double span = (double)(end - start)/(double)(sysconf(_SC_CLK_TCK));
					
					printf("\nsystime   [%fs]\n",span);
					break;
				}
			}
			printf("ERROR: Invalid command! Use 'help time' to lookout.\n");
			break;
		case 12:
			do_clear();
			exit(0);
			break;
		case 13:
			if(tmp == NULL) printf("For details usage for commands ,input 'help [command]'\n[Example]: 'help' for all command \n         or 'help pwd' for command pwd\n[CoralShell commands include]:\nls \necho \ncat \nmkdir \nrm \ncd \nchmod \npwd \nwc \ndate \nexit \nhelp \nkill \nwho \nhistory \nsysteminfo\nclear\n");
			else if(!(strcmp(tmp, cmd1))) printf("usage:  ls [-l]\n  -l: for long message to output\n");
			else if(!(strcmp(tmp, cmd2))) printf("usage:  echo [varity]\n");
			else if(!(strcmp(tmp, cmd3))) printf("usage:  cat [file | dir]\n");
			else if(!(strcmp(tmp, cmd4))) printf("usage:  mkdir [dir]\n");
			else if(!(strcmp(tmp, cmd5))) printf("usage:  rm [fiel | dir]\n");
			else if(!(strcmp(tmp, cmd6))) printf("usage:  cd [dir]\n");
			else if(!(strcmp(tmp, cmd7))) printf("usage:  chmod [prevlege] [file | dir]\n\n  [For example]: chmod 777 hello.c\n  '777' will give hello.c full previlege.\n");
			else if(!(strcmp(tmp, cmd8))) printf("usage:  pwd [dir]\n");
			else if(!(strcmp(tmp, cmd9))) printf("usage:  wc [-c | -l] [file | dir]\n\n  -c: number of bytes or characters\n  -l: number of lines\n");
			else if(!(strcmp(tmp, cmd10))) printf("usage:  date\n  Cmmand will show localtime\n");
			else if(!(strcmp(tmp, cmd11))) printf("usage:  time [cmmand]\n  show Command excecute time.\n  [For exam]: time date\n");
			else if(!(strcmp(tmp, cmd12))) printf("usage:  exit\n  Quit CoralShell\n");
			else if(!(strcmp(tmp, cmd13))) printf("usage:  help [cmmand]\n  Use 'help' to show all commands in Coral shell\n  Also use 'help command' to show details for each command\n");
			else if(!(strcmp(tmp, cmd14))) printf("usage:  kill [-1 | -9 | -15] [pid]\n  -1: sighup\n  -9: sigkill\n  -15: sigterm\n");
			else if(!(strcmp(tmp, cmd15))) printf("usage:  echo [varity]\n");
			else if(!(strcmp(tmp, cmd16))) printf("usage:  Show all command used.\n");
			else if(!(strcmp(tmp, cmd17))) printf("usage:  system message such as CPU, Memory ...\n");
			else if(!(strcmp(tmp, cmd18))) printf("usage:  just use 'clear' to clean screen\n");
			else printf("Usage: help [command]\n\nExample: 'help' for all command \n         or 'help pwd' for command pwd\n");

			break;
		case 14:
			do_kill(tmp,tmp1);
			break;
		case 15:
			do_who();
			break;
		case 16:
			for (j;j<=i ;j++ )
			{
				printf("%d        %s\n", j, history[j]);
			}
			/*
			if(history1!=NULL){
				printf("%d        %s\n", i, history1);
				i++;
		    }
			if(history2!=NULL){
				printf("%d        %s\n", i, history2);
				i++;
		    }
			if(history3!=NULL){
				printf("%d        %s\n", i, history3);
				i++;
		    }
			if(history4!=NULL){
				printf("%d        %s\n", i, history4);
				i++;
		    }
			if(history5!=NULL){
				printf("%d        %s\n", i, history5);
				i++;
		    }
			if(history6!=NULL){
				printf("%d        %s\n", i, history6);
				i++;
		    }
			if(history7!=NULL){
				printf("%d        %s\n", i, history7);
				i++;
		    }
			if(history8!=NULL){
				printf("%d        %s\n", i, history8);
				i++;
		    }
			if(history9!=NULL){
				printf("%d        %s\n", i, history9);
				i++;
		    }
			*/
			break;
		case 17:
			do_cat("/proc/cpuinfo");
			do_cat("/proc/meminfo");

			break;
		case 18:
			do_clear();
			break;
		case 19:
			//do_exec()
			break;
	    
      }
      return 0;
}

⌨️ 快捷键说明

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