进程之实现linux的伪终端

思维导图:

使用多进程 + 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值