思维导图:
使用多进程 + wait + exec + strtok 实现一个伪装的终端
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
//使用多进程 + wait + exec + strtok 实现一个伪装的终端
char* getLine(char* buf,int size){
fgets(buf,size,stdin);//从输入流最多获取size-1个字节的数据写入到buf里面
int len = strlen(buf);
if(buf[len-1]=='\n'){
buf[len-1] = 0;//如果读取到换行符'\n',将其替换为0,结束获取
}
}
int main(int argc, const char *argv[])
{
while(1){
int retval = fork();//创建子进程
if(retval > 0){
wait(0);//等待子进程结束,回收子进程的资源
}else{
char* username = getlogin();//获取当前用户的登录名
char hostname[64] = {0};
char pwd[128] = {0};
gethostname(hostname,64);//获取主机名存到hostname
getcwd(pwd,128);//获取当前工作目录存到pwd
printf("\033[1;32;10m%s@%s\033[0m:\033[1;34;10m%s\033[0m$ ",username,hostname,pwd);//输出用户名、主机名、工作目录
fflush(stdout);//刷新缓存区
char* arg[20] = {0};
// find /usr/include -name stdio.h
// cd ..
char cmd[128] = {0};
getLine(cmd,128);
char* retval = NULL;//定义一个字符指针
int i = 0;
do{
if(retval == NULL){
retval = strtok(cmd," ");//切割终端输入的shell指令
}else{
retval = strtok(NULL," ");
}
// printf("retval = %s\n",retval);
arg[i++] = retval;//将切割的每一块shell指令放在字符指针数组中
}while(retval != NULL);
if(strcmp(arg[0],"cd")==0){//如果输入cd命令,那就直接切换目录到切割好的argv[1]里面去
chdir(arg[1]);
}else{
execvp(arg[0],arg);//替换进程
printf("没有找到该指令\n");
}
//return 0;
}
}
return 0;
}