Linux终端实现自己的命令解释器----mybash
生活随笔
收集整理的這篇文章主要介紹了
Linux终端实现自己的命令解释器----mybash
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
首先我們得知道Linux下產(chǎn)生新進(jìn)程的過程是--->先復(fù)制---->再替換子進(jìn)程(fork + exec)
來看代碼? 代碼中都有解釋的
下面代碼實(shí)現(xiàn)的主要功能就是內(nèi)置命令cd的簡單操作和exit的作用
#include <stdio.h>#include <assert.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/wait.h>#include <pwd.h>#include <dirent.h>char * get_cmd(char buff[],char * myargv[]){if(buff == NULL || myargv == NULL){return NULL;}int i = 0;char * s = strtok(buff," ");//對命令進(jìn)行分割 例如ps -f這種while( s!= NULL){myargv[i++] = s;s = strtok(NULL," ");}return myargv[0];//識別輸入的命令 作出對應(yīng)的舉動}void printf_info() //這個(gè)函數(shù)的作用就是在執(zhí)行mybash的時(shí)候前面那一行話{int id = getuid();//獲取用戶uidchar* s = "$";if (id == 0)//當(dāng)是管理員權(quán)限的時(shí)候 getuid()返回值是0{s = "#";}struct passwd * ptr = getpwuid(id);//根據(jù)uid返回用戶真實(shí)信息if (ptr == NULL) //獲取失敗{printf("$");fflush(stdout);return;}char hostname[64] = {0};if(gethostname(hostname,64) == -1){printf("$");fflush(stdout);return;}char pwd_buff[128] = {0};if(getcwd(pwd_buff,128) == NULL){printf("$");fflush(stdout);return;}printf("%s@%s %s:%s",ptr->pw_name,hostname,pwd_buff,s);fflush(stdout);}int main(){while(1){char buff[128] = {0}; //存放鍵盤輸入命令和參數(shù)printf_info();fgets(buff,128,stdin);//fgets會把\n也給讀進(jìn)去 buff[strlen(buff)-1] = 0; //這行就是將\n這個(gè)東西給去掉char * myargv[10] = {0};//這行相當(dāng)于是命令庫char * cmd = get_cmd(buff,myargv);if(cmd == NULL)//像原來系統(tǒng)的bash也是 沒命令回車時(shí)會繼續(xù){continue;}else if(strcmp(cmd,"exit") == 0) //exit是內(nèi)置命令 不需要產(chǎn)生新進(jìn)程 也就不需要fork復(fù)制然后exec替換{break;}//這里說一下 內(nèi)置命令和外部命令的區(qū)別 先說外部命令:需要產(chǎn)生一個(gè)新進(jìn)程,所以就會fork+exec//內(nèi)置命令就每這么麻煩 效率也更高 更快else if(strcmp(cmd,"cd") == 0) //cd也是內(nèi)置命令{if (myargv[1] == NULL) //單純的輸入cd{continue;}if (chdir(myargv[1]) == -1)//chdir錯(cuò)誤之后的返回值就是-1 成功是0{printf("cd err");}continue;}else //到這塊else的話 就是外部命令了 需要fork+exec產(chǎn)生新進(jìn)程{pid_t pid = fork();if(pid == -1){printf("fork err \n");continue;}if(pid == 0)//這塊就是運(yùn)行mybash需要先對進(jìn)程進(jìn)行復(fù)制fork 然后exec進(jìn)行替換 因?yàn)檫@是我們自己寫的程序 需要產(chǎn)生新進(jìn)程{execvp(cmd,myargv);printf("execvp err\n");//替換成功的話 不會執(zhí)行到這一行的exit(0);}wait(NULL);//將父進(jìn)程阻塞住 并且也可以讓子進(jìn)程變成僵死進(jìn)程}}}
? 在實(shí)現(xiàn)外部命令的時(shí)候是系統(tǒng)直接調(diào)用/usr/bin下的
也就是說在bash中執(zhí)行外部命令的話? ?是和bash沒什么關(guān)系的? 都是從系統(tǒng)中調(diào)用的
下面再來自己編寫個(gè)程序?qū)崿F(xiàn)兩個(gè)外部命令
pwd的程序?qū)崿F(xiàn):
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>int main()
{char buff[128] = {0};if (getcwd(buff,128) != NULL){printf("%s \n",buff);}return 0;
}
ls的程序?qū)崿F(xiàn)
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>int main()
{char buff[128] = {0};if (getcwd(buff,128) == NULL){return 0;}DIR * p = opendir(buff);if ( p == NULL){return 0;}struct dirent * s = NULL;while((s = readdir(p)) != NULL){if(strncmp(s->d_name,".",1)==0){continue;}printf("%s \n",s->d_name);}closedir(p);return 0;}
總結(jié)
以上是生活随笔為你收集整理的Linux终端实现自己的命令解释器----mybash的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BF算法优化-------KMP算法
- 下一篇: 冒泡排序和选择排序