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

📄 linuxshell.c

📁 Linux中Shell的几条简单命令的实现,基本框架已经搭好了
💻 C
字号:
/*    By Shoarh(1)   pid [-p]    to get current process pid or parent process pid with parameter -p(2)   cd          change derectory(3)   pwd         current work derectory(4)   logout      exit shell  (5)   :           Use token : to execute every instruct by order           [Normal Expression]      instruction  -> pid [-p] | cd [..] [directory] | pwd | logout | [OR MORE]      format       -> format : instruction | instruction                         You can use Multi-Instruction,each instructin separate by token :      e.g ls -l : cd .. : pid -p : cd : pwd : ls -l : logout : ps -a       If logout is executed , the instruction after logout will now be executed */#include <stdio.h>#include <string.h>#include <signal.h>#include <ctype.h>#include <unistd.h>#define ARGNUM 20   //number of arg#define ARGLEN 30   //length of each arg#define BSIZE 255   //size of bufferchar inputbuffer[BSIZE];    //buffer of inputchar argbuf[ARGNUM][ARGLEN];//get each argint indexnum;               //index arg positionchar* arglist[ARGNUM];      //number of arg to be store in arglistchar* currentpos;           //current position of bufferchar defaultpath[255];      //default current pathint multi;                  //is multi-instructvoid cdcommand();void pidcommand(int);char getarg(char* c)//get token{	int i=0;  //each character of arg        for(i=0;!isspace(*c);i++,c++,currentpos++)	{                 argbuf[indexnum][i]=*c;	}           argbuf[indexnum][i]='\0';           arglist[indexnum]=argbuf[indexnum];//from argbuf to arglist           indexnum++;	while(isspace(*currentpos))	{           currentpos++;	}	if(*currentpos=='\0') return '\0';	return *currentpos;}int exec(char *arglist[])//execute{        pid_t pid;int exitstatues;	pid=fork();	switch(pid) 	{		case -1: perror("fork failed");			 exit(1);		case 0:  execvp(arglist[0],arglist);                         perror("execvp failed");                         exit(1);		default: while(wait(&exitstatues)!=pid) ;	}}void conver(){       	indexnum=0;	printf("\n&");	memset(inputbuffer,' ',sizeof(inputbuffer));//clear inputbuffer	fgets(inputbuffer,BSIZE,stdin);	currentpos=inputbuffer;      //point to beginning of buffer	if(*currentpos==':') conver();	while(isspace(*currentpos))	{		currentpos++;	}	while(*currentpos!='\0')	{      		if(*currentpos==':')		{  			multi=1;			arglist[indexnum]='\0';			frame();			*currentpos++;			while(isspace(*currentpos)) *currentpos++; 			indexnum=0; printf("\n");			getarg(currentpos);		}		else		{ 			multi=0;			getarg(currentpos);		}	}	multi=0;	arglist[indexnum]='\0';}int frame(){	    while( strcmp(arglist[0],"logout")!=0 || arglist[1]!=NULL )            {                              int arglen=strlen(arglist[0]);                       if(strcmp(arglist[0],"cd")==0)             //cd                       {                                   if((arglist[0][arglen+1])=='\0'&& arglist[1]==NULL)                             {                                  arglist[1]=defaultpath;                                  cdcommand();                             }                             else  cdcommand();                        }                        else if(strcmp(arglist[0],"pid")==0)     //pid                        {                             if((arglist[0][arglen+1])=='\0'&& arglist[1]==NULL)                                  pidcommand(0);                             else if(strcmp(arglist[1],"-p")==0)//get parent pid                             pidcommand(1);                             else printf("Command Error!\n");                        }                        else                        {                             exec(arglist);                         }                    if(multi) break;                    else                    {                         conver();                       while(arglist[0]==NULL) conver();                    }            }            if( strcmp(arglist[0],"logout")==0 ) exit(0);}main(){        getcwd(defaultpath,255);  //get current default path        while(arglist[0]==NULL)         conver();        frame();}void cdcommand(){        char curpath[255];//	printf("Excuting cd command now!\n");	if((int*)(chdir(arglist[1])==-1)) perror("Chdir directory Error!");        getcwd(curpath,255); 	printf("Current work directory: %s\n",curpath);}void pidcommand(int p){//	printf("Excuting pid command now!\n");	printf("Current process pid is %d\n",getpid());	if(p) printf("Parent process pid is %d\n",getppid());}

⌨️ 快捷键说明

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