我正在编写重定向函数,它将命令的输出写入给定的文件名。编写我自己的linux外壳I/O重定向'>'功能
例如:
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]
给出的文件?
编辑
这些都是我们已经给予了说明:
“完成功能1.解析为线后通过添加标准输出重定向到一个文件中提高shell的唯一尝试> ,取一切前面的命令,并将第一个字作为文件名(忽略<,>>,| etc)。
标准输出写出到文件描述符1(标准输入为0,标准错误为2)。这个任务可以通过打开文件并复制它的文件来实现使用dup2系统调用将描述符复制到1。
int f = open(filename , O_WRONLY|O_CREAT|O_TRUNC, 0666) ;
dup2(f , 1) ;
注:使用系统调用open不是库包装这里的fopen”
在我看来,你在这里要求完整的任务。你应该尝试自己想出一些东西,如果遇到困难,可以提出更具体的问题。 – Kenster
作为一个起点,一个真正的shell会调用fork()来创建一个子进程。在孩子的内部,它会使用类似'dup2()'样本的东西来打开输出文件并将其分配给stdout。然后它会调用'execve()'或[其他exec函数之一](http://linux.die.net/man/3/execv)来实际执行该命令(特别是看看execvp) 。 exec函数以参数列表的形式接受命令,所以不需要将它们连接成单个字符串。 – Kenster