-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tee.cpp
More file actions
38 lines (30 loc) · 954 Bytes
/
Copy pathtest_tee.cpp
File metadata and controls
38 lines (30 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//
// Created by s on 19-5-3.
//
/*
* 与splice相比不消耗数据,源文件描述符上的数据仍可以用于后续操作
*/
#include "myutili.h"
int main (int argc, char *argv[])
{
if (argc <= 1)
{
printf ("usage: %s file_name\n", basename (argv[0]));
exit (EXIT_FAILURE);
}
int filefd = Open (argv[1], O_CREAT | O_WRONLY | O_TRUNC, 0666);
int pipefd_stdout[2];
Pipe (pipefd_stdout);
int pipefd_file[2];
Pipe (pipefd_file);
Splice (STDIN_FILENO, NULL, pipefd_stdout[1], NULL, 32768, SPLICE_F_MORE | SPLICE_F_MOVE);
Tee (pipefd_stdout[0], pipefd_file[1], 32768, SPLICE_F_NONBLOCK);
Splice (pipefd_file[0], NULL, filefd, NULL, 32768, SPLICE_F_MORE | SPLICE_F_MOVE);
Splice (pipefd_stdout[0], NULL, STDOUT_FILENO, NULL, 32768, SPLICE_F_MORE | SPLICE_F_MOVE);
close (filefd);
close (pipefd_stdout[0]);
close (pipefd_stdout[1]);
close (pipefd_file[0]);
close (pipefd_file[1]);
return 0;
}