1TEE(2) Linux Programmer's Manual TEE(2)
2
3
4
6 tee - duplicating pipe content
7
9 #define _GNU_SOURCE
10 #include <fcntl.h>
11
12 long tee(int fd_in, int fd_out, size_t len, unsigned int flags);
13
15 tee() duplicates up to len bytes of data from the pipe referred to by
16 the file descriptor fd_in to the pipe referred to by the file descrip‐
17 tor fd_out. It does not consume the data that is duplicated from
18 fd_in; therefore, that data can be copied by a subsequent splice(2).
19
20 flags is a series of modifier flags, which share the name space with
21 splice(2) and vmsplice(2):
22
23 SPLICE_F_MOVE Currently has no effect for tee(); see splice(2).
24
25 SPLICE_F_NONBLOCK Do not block on I/O; see splice(2) for further
26 details.
27
28 SPLICE_F_MORE Currently has no effect for tee(), but may be imple‐
29 mented in the future; see splice(2).
30
31 SPLICE_F_GIFT Unused for tee(); see vmsplice(2).
32
34 Upon successful completion, tee() returns the number of bytes that were
35 duplicated between the input and output. A return value of 0 means
36 that there was no data to transfer, and it would not make sense to
37 block, because there are no writers connected to the write end of the
38 pipe referred to by fd_in.
39
40 On error, tee() returns -1 and errno is set to indicate the error.
41
43 EINVAL fd_in or fd_out does not refer to a pipe; or fd_in and fd_out
44 refer to the same pipe.
45
46 ENOMEM Out of memory.
47
49 The tee() system call first appeared in Linux 2.6.17.
50
52 This system call is Linux-specific.
53
55 Conceptually, tee() copies the data between the two pipes. In reality
56 no real data copying takes place though: under the covers, tee()
57 assigns data in the output by merely grabbing a reference to the input.
58
60 The following example implements a basic tee(1) program using the tee()
61 system call.
62
63 #define _GNU_SOURCE
64 #include <fcntl.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <unistd.h>
68 #include <assert.h>
69 #include <errno.h>
70 #include <limits.h>
71
72 int
73 main(int argc, char *argv[])
74 {
75 int fd;
76 int len, slen;
77
78 assert(argc == 2);
79
80 fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
81 if (fd == -1) {
82 perror("open");
83 exit(EXIT_FAILURE);
84 }
85
86 do {
87 /*
88 * tee stdin to stdout.
89 */
90 len = tee(STDIN_FILENO, STDOUT_FILENO,
91 INT_MAX, SPLICE_F_NONBLOCK);
92
93 if (len < 0) {
94 if (errno == EAGAIN)
95 continue;
96 perror("tee");
97 exit(EXIT_FAILURE);
98 } else
99 if (len == 0)
100 break;
101
102 /*
103 * Consume stdin by splicing it to a file.
104 */
105 while (len > 0) {
106 slen = splice(STDIN_FILENO, NULL, fd, NULL,
107 len, SPLICE_F_MOVE);
108 if (slen < 0) {
109 perror("splice");
110 break;
111 }
112 len -= slen;
113 }
114 } while (1);
115
116 close(fd);
117 exit(EXIT_SUCCESS);
118 }
119
121 splice(2), vmsplice(2), feature_test_macros(7)
122
124 This page is part of release 3.22 of the Linux man-pages project. A
125 description of the project, and information about reporting bugs, can
126 be found at http://www.kernel.org/doc/man-pages/.
127
128
129
130Linux 2006-04-28 TEE(2)