编写我自己的 linux shell I/O 重定向 '>' 函数

KOB*_*KOB 5 c linux shell io-redirection

我正在编写将命令输出写入给定文件名的重定向函数。

例如:

echo Hello World > hello.txt 将“Hello World”写入 hello.txt。

ls -al > file_list.txt 会将当前目录中所有文件/目录名称的列表写入 file_list.txt。

到目前为止,我的功能定义为:

int my_redirect(char **args, int count) {
    if (count == 0 || args[count + 1] == NULL) {
        printf("The redirect function must follow a command and be followed by a target filename.\n");
        return 1;
    }
    char *filename = args[count + 1];

    //Concatenates each argument into a string separated by spaces to form the command
    char *command = (char *) malloc(256);
    for (int i = 0; i < (count); i++) {
        if (i == 0) {
            strcpy(command, args[i]);
            strcat(command, " ");
        }
        else if (i == count - 1) {
            strcat(command, args[i]);
        }
        else {
            strcat(command, args[i]);
            strcat(command, " ");
        }
    }

    //command execution to file goes here

    free(command);
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

哪里args[count]">"

如何从执行由字符串指定的命令args[0]args[count - 1]为在给定的文件args[count + 1]

编辑

这些是我们得到的指示:

“通过将 stdout 的重定向添加到文件来改进您的 shell。仅在完成功能 1 后尝试。解析 > 行,将之前的所有内容作为命令,并将之后的第一个单词作为文件名(忽略 <、>>、| 等)。

标准输出写入文件描述符 1(stdin 为 0,stderr 为 2)。因此,可以通过打开文件并使用 dup2 系统调用将其文件描述符复制到 1 来完成此任务。

int f = open( filename , O_WRONLY|O_CREAT|O_TRUNC, 0666) ;
dup2( f , 1 ) ;
Run Code Online (Sandbox Code Playgroud)

注意:这里使用系统调用 open 而不是库包装 fopen。”

Jen*_*ens 0

如果允许您以特殊方式解决此问题,那么它仅适用于一小部分问题,例如将命令的 stdout 捕获到文件中,您可以避免使用 中的函数重新发明popen()轮子<stdio.h>

程序草图:

  1. 确定输出文件名
  2. 打开输出文件进行写入
  3. 确定命令和参数
  4. 构造从 args 到>.
  5. 称呼FILE *cmd = popen(command, "r");
  6. 从流中读取行cmd,写入输出文件
  7. 当流上没有 EOF 时转到 6 cmd
  8. pclose(cmd)fclose输出流

仅当您的教练不希望您使用 fork、dup 和friends 时才执行此操作。