1.25 实现一个终端的功能

实现一个终端的功能,包含cd的功能。

cs 复制代码
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.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;

// 自定义的 fgets 函数,去除换行符
char* mygets(char* s, int size) {
    char* res = fgets(s, size, stdin);
    int len = strlen(s);
    if (s[len - 1] == '\n') {
        s[len - 1] = '\0';
    }
    return s;
}

int main(int argc, const char *argv[]) {
    while (1) {
        // 获取用户名
        char* username = getlogin();
        // 获取主机名
        char hostname[64] = {0};
        gethostname(hostname, 63);
        // 获取当前路径
        char cwd[1024] = "";
        getcwd(cwd, sizeof(cwd) - 1);

        // 打印命令提示符
        printf("\033[1;32;10m%s@%s\033[0m:\033[1;34;10m%s\033[0m$ ", username, hostname, cwd);
        fflush(stdout);

        // 读取用户输入的命令
        char shell[256] = {0};
        char* cmd[100] = {0};
        mygets(shell, 256);

        // 解析命令
        char* res = NULL;
        int i = 0;
        while (1) {
            if (res == NULL) {
                res = strtok(shell, " ");
            } else {
                res = strtok(NULL, " ");
            }

            if (res == NULL) {
                break;
            }
            cmd[i] = res;
            i++;
        }

        // 检查是否为 cd 命令
        if (cmd[0] != NULL && strcmp(cmd[0], "cd") == 0) {
            if (cmd[1] == NULL) {
                // 如果没有提供参数,默认切换到用户主目录
                const char* home = getenv("HOME");
                if (home == NULL) {
                    fprintf(stderr, "cd: 无法获取主目录\n");
                } else {
                    if (chdir(home) != 0) {
                        perror("cd");
                    }
                }
            } else {
                // 切换到指定目录
                if (chdir(cmd[1]) != 0) {
                    perror("cd");
                }
            }
            continue;
        }

        // 执行其他命令
        pid_t pid = fork();
        if (pid > 0) {
            wait(0);
        } else if (pid == 0) {
            if (execvp(cmd[0], cmd) == -1) {
                printf("%s: 未找到命令\n", cmd[0]);
                exit(EXIT_FAILURE);
            }
        } else {
            perror("fork");
        }
    }
    return 0;
}
相关推荐
白帽黑客沐瑶2 天前
【网络安全就业】信息安全专业的就业前景(非常详细)零基础入门到精通,收藏这篇就够了
网络·安全·web安全·计算机·程序员·编程·网络安全就业
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法
echoarts2 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
树码小子2 天前
Java网络编程:(socket API编程:TCP协议的 socket API -- 回显程序的服务器端程序的编写)
java·网络·tcp/ip
Aomnitrix2 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题2 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说2 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔2 天前
【51单片机】【protues仿真】基于51单片机的篮球计时计分器系统
c语言·stm32·单片机·嵌入式硬件·51单片机
小莞尔2 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
绿箭柠檬茶2 天前
Ubuntu 服务器配置转发网络访问
服务器·网络·ubuntu