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

📄 shell.c

📁 在Linux环境下模拟shell 操作系统实验
💻 C
字号:
/******************************include files*******************************/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <sys/wait.h>/*****************************string structure*****************************/struct mystr {  int length;  char *content;};int k,is_back = 0;char buffer[50];char *argc;char *argv[20];/*****************************print noticeuse get_current_dir_name()******************************/void print_notice(void) {  int path;  path = get_current_dir_name();  printf("@%s>",path);}/*****************************get user's inputstore the input in the buf string******************************/struct mystr get_user_instruction(void) {  int i=0;  char ch;  char buf[80];  struct mystr temp;  scanf("%c",&ch);  while(ch!='\n'&&i<50) {    buf[i++]=ch;    scanf("%c",&ch);  }//while  if(i>=80) {    printf("instruction too long\n");    i=0;  }  buf[i]='\0';  temp.length=i;  temp.content=buf;  return temp;} /*****************************analys instructionsplit the string into *arg[]return arg*****************************/void analy_instruct(struct mystr temp) {  int i,j;  k=0;  char input[100];  int li_inputlen=temp.length;  strcpy(input,temp.content);  for(i=0,j=0;i<=li_inputlen;i++) {	   if(input[i]==' '||input[i]=='\t'||input[i]=='\0') {		   if(j==0) continue;		   else {			   buffer[j++] = '\0';			   if(k==0) {				   argc = (char *) malloc (sizeof(char)*j);				   strcpy(argc,buffer);			   }			   argv[k] = (char *) malloc(sizeof(char)*j);			   strcpy(argv[k],buffer);			   j=0;			   k++;		   }	   }else {		   if(input[i] == '&' && input[i+1] == '\0') {			   is_back = 1;			   continue;		   }		   buffer[j++] = input[i];	   }  }}/********************************************************/int is_fileexist(char *c) {  char *path,*p;  int i=0;  path = getenv("PATH");  p=path;  while(*p !='\0') {    if(*p != ':')      buffer[i++] = *p;    else {      buffer[i++] = '/';      buffer[i] = '\0';      strcat(buffer,c);      if(access(buffer,F_OK) == 0)        return 0;      else         i = 0;    }    p++;  }  return -1;}/**********************************************************/void find_file(void) {  if(strcmp(argc,"quit") == 0)    printf("bye-bye\n");  else {	  argv[k] = (char *)0;	  if(is_fileexist(argc) == -1)		  printf("This command is not found!\n");  }}/**********************************************************/void exe_instruct(void) {  pid_t pid;  int status;  if((pid = fork()) == 0)    execv(buffer,argv);  else     if(is_back == 0)      waitpid(pid,&status,0);}/*****************************main******************************/int main () {  struct mystr str;  while(1) {	  print_notice();	  str = get_user_instruction();	  if(str.length!=0) {		  analy_instruct(str);		  find_file();		  if(strcmp(argc,"quit")==0)break;		  exe_instruct();	  }  }  return 0;}

⌨️ 快捷键说明

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