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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) 2020 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
// Use of this source is governed by Apache-2.0 License that can be found
// in the LICENSE file.
/// This code is rewritten of tee(2) example code.
/// ```C
/// #define _GNU_SOURCE
/// #include <errno.h>
/// #include <fcntl.h>
/// #include <limits.h>
/// #include <stdio.h>
/// #include <stdlib.h>
/// #include <unistd.h>
///
/// int main(int argc, char *argv[]) {
/// int fd;
/// int len, slen;
///
/// if (argc != 2) {
/// fprintf(stderr, "Usage: %s <file>\n", argv[0]);
/// exit(EXIT_FAILURE);
/// }
///
/// fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
/// if (fd == -1) {
/// perror("open");
/// exit(EXIT_FAILURE);
/// }
///
/// do {
/// /*
/// * tee stdin to stdout.
/// */
/// len = tee(STDIN_FILENO, STDOUT_FILENO, INT_MAX, SPLICE_F_NONBLOCK);
///
/// if (len < 0) {
/// if (errno == EAGAIN)
/// continue;
/// perror("tee");
/// exit(EXIT_FAILURE);
/// } else if (len == 0)
/// break;
///
/// /*
/// * Consume stdin by splicing it to a file.
/// */
/// while (len > 0) {
/// slen = splice(STDIN_FILENO, NULL, fd, NULL, len, SPLICE_F_MOVE);
/// if (slen < 0) {
/// perror("splice");
/// break;
/// }
/// len -= slen;
/// }
/// } while (1);
///
/// close(fd);
/// exit(EXIT_SUCCESS);
/// }
/// ```