【发布时间】:2016-07-15 04:19:56
【问题描述】:
我正在编写将命令的输出写入给定文件名的重定向函数。
例如:
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;
}
其中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 ) ;
注意:这里使用系统调用 open 而不是库包装 fopen。"
【问题讨论】:
-
在我看来,您在这里要求完成全部作业。您应该尝试自己想出一些东西,如果遇到困难,请提出更具体的问题。
-
作为一个起点,一个真正的shell会调用
fork()来创建一个子进程。在孩子内部,它会使用类似dup2()样本的东西来打开输出文件并将其分配给标准输出。然后它会调用execve()或one of the other exec functions 来实际执行命令(特别是看看 execvp)。 exec 函数以参数列表的形式接收命令,因此不必将它们连接成单个字符串。
标签: c linux shell io-redirection