资源简介
操作系统第七版 第三章进程 课后编程项目 操作系统实验二_shell的简单实现。
实现了以下功能:
(1)解析用户提交的命令行;按照环境变量搜索目录系统;执行命令。
(2)提供ls、mkdir rmdir、pwd、ps等命令。
(3)提供历史查询功能。如用户按下Ctr1+C,信号处理器将输出最近的10个命令列表。
代码片段和文件信息
/**
*Name: Command Shell History
*Author: Misky
*Date: 2013/10/25
*Explanation: This is a project which can get the shell command from user and execute it in the child process
and the latest 10 commands will be saved. Some special requirements as follows have also be completed in this project.
1.When user enters in “Ctrl+D“ the project will be terminated.
2.When user enters in “Ctrl+c“ the list of 10 historys will be displayed. I here displayed it like a queue or like a Gluttonous Snake.
3.If a command uses a “&“ as its last character the parent process will not wait for the child process
i.e. user will be asked to type in another command without the completion of the last one.
4.When user uses command “r“ only the last command in history list will be executed again.
And in this case if the last command is wrong it will not be put in the history. Otherwise it will.
5.When user uses command “r x“ the nearset command with first character “x“ in history list will be executed again.
And also in this case if that command is wrong it will not be put in the history. Otherwise it will.
*PS:Before running this project you need to create a txt file named “success.txt“ in directory “/home/ieee/“.
If you don‘t have that kind of directory you need to change the path to your path respectively in line 136159180266280 and 288.
I know that we can create a file atomatically in C but since this is between the parent process and the child process if I do that a new file will be created in both child process memory and the father‘s which will cause the inconformity although it is inconvenient.
And remember to make your path destination already get the root to be read and write. Thank you!
*/
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX_LINE 80
#define BUFFER_SIZE 50
void getPrompt();
char buffer[BUFFER_SIZE];
char *history[10][10]; /*save 10 latest commands*/
int nextPosition=0; /*the position where next command could be put in*/
int CommandLenth[10]={0}; /*the lenth of each command in history*/
void ProcessRCommand(char *args[]); /*the function to deal with the “r“ command*/
int success[10]={1111111111}; /*indicate each command in history is right or wrong 1 means right*/
int flag=1; /*used to tell the child process whether the nextPosition needs to be changed or not */
FILE *fp;
static char promptbuf[MAX_LINE]; // store prompt
/*setup() reads in the next command line separating it into distinct tokens using whitespace as delimiters. */
void setup(char inputBuffer[] char *args[]int *bac
评论
共有 条评论