Shell project, to realize their command interpreter.
Commands are two types of built-in commands (cd, exit) and the general command.
- Built-in command
Built-in commands implemented in bash, no new process , to do their own operations.
cd by the chdir (cd) to achieve operation.
exit to exit the process, directly BREAK .
Screenshot part of the code:
Screenshot operating results:
cd
exit
- General command
General command by fork () + exec () implemented a new process .
Screenshot part of the code:
pwd : Displays the absolute path of the current file, by getcwd (pwd, len) can be obtained.
Screenshot part of the code:
Screenshot operating results:
mkdir : build directory file, via mkdir () generation
Screenshot part of the code:
Screenshot operating results:
rmdir : delete empty directories files through () rmdir Delete
Screenshot part of the code:
Screenshot operating results:
Touch : generate regular file. Direct use open () to open the file, if not then created.
Screenshot part of the code:
Screenshot operating results:
RM : general delete files through () unlink achieved.
Screenshot part of the code:
Screenshot operating results:
ls:通过getcwd()获取当前所在目录后,通过opendir(),readdir(),closedir()对目录流进行操作。打印非隐藏文件,并判断其文件权限后根据权限将文件名分颜色打印。
部分代码截图:
运行结果截图:
cp:文件拷贝,通过open(),read(),write(),close()等对文件的操作实现对新文件的打开/创建,并进行复制操作。
部分代码截图:
运行结果截图:
ps -e/-A:ps命令所需要的信息存储在/proc/pid/task/status。打开相应文件并进行读取打印即可实现该操作。
部分代码截图:
运行结果截图: