tee 接受 stdin 并将数据流复制到 stdout 以及作为选项给出的文件,它可以在许多不同的情况下使用。
C 中的实现非常简单,只需编写一个程序,将所有数据从 stdin 复制到 stdout,而且还对基于命令行参数打开的文件使用与 stdout 相同的输出语句。
基本上是伪代码:
file f = open(argv[1])
while (! end of file stdin) {
buffer = read stdin
write stdout buffer
write f buffer
}
close(f)
Run Code Online (Sandbox Code Playgroud)
请注意,您实际上不必对管道执行任何操作,您的 shell 会对管道进行排序,程序只需将数据从一个流复制到其他两个流。