UNIX命令在C中实现

时间:2012-02-18 07:31:08

标签: c linux unix operating-system

对于我的操作系统类,我有一个基于之前作业的作业。不幸的是,除了我不知道下一个项目需要从哪里开始之外,我之前的项目无法正常工作。我下面的代码假设模仿一个简单的UNIX / Linux shell,其中包含一些无法使用execvp执行的附加命令:通过&符号运算符执行后台处理,'jobs'shell命令:列出所有生命子进程的pids(即不是已经终止的那些,“收获”“僵尸”进程,以及'cd'hell命令:更改shell的工作目录。

我相信,除了“jobs”命令和“cd”命令之外的所有命令都有效,但我不确定为什么这两个命令都没有。

接下来的任务是以“mysh $ cmd arg1 arg2 argN> file.out”的形式添加一些I / O重定向,我不知道哪里可以真正开始......

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <wait.h>
#include <signal.h>
#include <sys/types.h>

int main(int argc, char **argv) {
    char bBuffer[BUFSIZ], *pArgs[10], *aPtr = NULL, *sPtr;
    int jobs[100];
    int jobList = 0;
    int background;
    ssize_t rBytes;
    int aCount;
    pid_t pid;
    int status;
    while(!feof(stdin)) {
        pid = waitpid(-1, &status, WNOHANG);
        if (pid > 0)
        printf("waitpid reaped child pid %d\n", pid);
        write(1, "\e[1;31mmyBash \e[1;32m# \e[0m", 27);
        rBytes = read(0, bBuffer, BUFSIZ-1);
        if(rBytes == -1) {
        perror("read");
        exit(1);
    }

    bBuffer[rBytes-1] = '\0';
    if(!strcasecmp(bBuffer, "exit")){ 
        exit(0);
    }

    sPtr = bBuffer;
    aCount = 0;
    do {
        aPtr = strsep(&sPtr, " ");
        pArgs[aCount++] = aPtr;
    } while(aPtr);
        background = (strcmp(pArgs[aCount-2], "&") == 0);
        if (background)
        pArgs[aCount-2] = NULL;

        if (strlen(pArgs[0]) > 1) {
            pid = fork();
            if (pid == -1) {
                perror("fork");
                exit(1);
            } else if (pid == 0) {
                jobs[jobList] = pid;
                jobList++;

                if(!strcasecmp(pArgs[0], "jobs")){                                         
                    for(int i; i<jobList; i++) {
                        if(kill(jobs[i],0)==0){
                            printf(jobs[i]);    
                        }
                        printf("these are jobs\n");
                        exit(1);
                    }
                    if(!strcasecmp(pArgs[0], "cd")){ 
                        int ret;
                        if (!pArgs[1])
                            strcpy(bBuffer, "pwd");
                        ret = chdir(pArgs[1]);
                        strcpy(bBuffer, "pwd");
                        exit(1);
                    }                                
                    fclose(stdin);
                    fopen("/dev/null", "r");
                    execvp(pArgs[0], pArgs);
                    exit(1);
                } else if (!background) {
                    pid = waitpid(pid, &status, 0);
                    if (pid > 0)
                        printf("waitpid reaped child pid %d\n", pid);
                }
        }
    }
    return 0;
}

2 个答案:

答案 0 :(得分:2)

首先,你想要解析你的行并检测你需要重定向到一个文件。所以,让我们说你使用strsep或其他什么,你发现输出是file.out或输入来自file.in

此时您想使用dup / dup2重定向输出。例如,要重定向STDOUT

int
do_redirect(int fileno, const char *name)
{
    int newfd;

    switch (fileno) {
    case STDOUT_FILENO:
        newfd = open(name, O_WRONLY | O_CREAT, S_IRUSR | S_IRUSR);
        break;
    }
    if (newfd == -1) {
        perror("open");
        return -1;
    }

    return dup2(fileno, newfd);
}
/* ... */


pid = fork();
do_redirect(STDOUT_FILENO, name);

注意事项:

  • 我没有测试代码 - 它甚至可能无法编译
  • 我没有做太多错误检查 - 你应该(我为open做的方式)
  • 您需要在自己的
  • 上实施STDIN_FILENO重定向
  • 请注意我是如何使用单独的功能的,main是大的,因为它是
  • 你的代码有7个级别的缩进 - 听说过arrow code

答案 1 :(得分:0)

由于这是作业,我不会直接给你代码。

dupdup2freopen很适合查看输入/输出重定向。

fork用于启动并发进程(&符号)

使用waitpid来获取子进程,你走在正确的轨道上。