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

📄 shell.c

📁 This is to bulid a sample linux shell.
💻 C
字号:
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<wait.h>

#define BUFSIZE 200
#define ARGVSIZE 40
#define DELIM "\n\t\r "

int help()
{
  
printf("*************LIST OF COMMANDS IMPLEMENTED***************\n");
printf("\n\tdaemon shell help...");
printf("\nls\t:Display all files in current directory.");
printf("\ncp\t:Copy Files.");
printf("\nrm\t:Remove Files.");
printf("\nln\t:Create hard link.");
printf("\npwd\t:Display Presten Working Directory.");
printf("\ndecho\t:Display following strings.");
printf("\nwc\t:Count no.of words,lines and chars in a file" );
printf("\nmkdir\t:To make a new directory");
printf("\nrmdir\t:To remove a directory");
printf("\nsleep\t:To make shell inactive for specified amount of time");
printf("\nfattr\t:Display the attributes of a file");
printf("\nrename\t:Rename file.");
printf("\nman\t:To Display the man pages of the command");
printf("\ndate\t:Displays Time.");
printf("\ncat\t:Diplay file contents or create files using < as the redirection operation.");
printf("\nquit\t:Exit(or Quit) Simple Shell.\n\n");
}

int banner()
{
	printf("\n*************************************************************************\n");
	printf("\t\t\tThankyou for using the Daemon shell\n");
	 printf("**************************************************************************\n");
	return 9;
}

int main()
{
   
printf("\n\nWelcome to the Daemon shell...Type '?' or 'help' to get help.\n");
 int i,n,m,spc=0;
 char buf[BUFSIZE+1];
 char *clargs[ARGVSIZE];
 int returnval;
        for(;;)
        {
         n = 1;
           for(i=0;i<ARGVSIZE;i++)
              clargs[i] = "\0";
           for(i=0;i<=BUFSIZE;i++)
              buf[i] = '\0'; 
           write(STDOUT_FILENO,"[<DAEMON@>] ",12);
           read(STDIN_FILENO,buf,BUFSIZE);
           if(!strcmp(buf,"quit\n"))
	   {
            	 banner();
		 return(0);
	   }
	   if(!strcmp(buf,"?\n")||!strcmp(buf,"help\n"))
	   {
 		 help();
		 continue;
	   }
           clargs[0] = strtok(buf,DELIM);
           while(clargs[n] = strtok(NULL,DELIM))
           n++;
           switch(fork())
                 {
                  case 0 : m=execvp(clargs[0],&clargs[0]);
			   if(m==-1)
				printf("daemon: %s command not found.\n",clargs[0]);
                    	   exit(200);
                default: wait(&returnval);
			 break;                  
                 }     
        }  
}

⌨️ 快捷键说明

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