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

📄 c_shell.c

📁 linux——shell nachos 的课程设计 第一个
💻 C
字号:
#include <unistd.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <sys/wait.h> 
#include <string.h> 
#include <errno.h> 

#define SHELL_NAME "sh1" 
#define PROMPT_ENVIRONMENT_VARIABLE "PROMPT" 

char *prompt; 

int main(int argc, char **argv)  
{ 
char cmd[80]; 
int statval; 
   
/* Determine prompt value. */ 
if ((prompt = getenv(PROMPT_ENVIRONMENT_VARIABLE)) == NULL) 
prompt = SHELL_NAME ":"; 

/* Process commands until exit, or death by signal. */ 
while (1)  
{ 
/* Prompt and read a command. */ 
printf(prompt); 
gets(cmd); 

/* Process built-in commands. */ 
if(strcasecmp(cmd, "exit") == 0) 
break; 

/* Process non-built-in commands. */ 
if(fork() == 0) { 
execlp(cmd, cmd, NULL); 
fprintf(stderr, "%s: Exec %s failed: %s\n", argv[0],  
cmd, strerror(errno)); 
exit(1); 
}  

wait(&statval); 
if(WIFEXITED(statval))  
{ 
if(WEXITSTATUS(statval)) 
{ 
fprintf(stderr, 
"%s: child exited with status %d.\n", 
argv[0], WEXITSTATUS(statval)); 
} 
} else { 
fprintf(stderr, "%s: child died unexpectedly.\n",  
argv[0]); 
} 
} 
}

⌨️ 快捷键说明

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