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 Conceptually, tee() copies the data between the two pipes. In reality
50 no real data copying takes place though: under the covers, tee()
51 assigns data in the output by merely grabbing a reference to the input.
52
54 The following example implements a basic tee(1) program using the
55 tee(2) system call.
56
57 #define _GNU_SOURCE
58 #include <fcntl.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <unistd.h>
62 #include <assert.h>
63 #include <errno.h>
64 #include <limits.h>
65
66 int
67 main(int argc, char *argv[])
68 {
69 int fd;
70 int len, slen;
71
72 assert(argc == 2);
73
74 fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
75 if (fd == -1) {
76 perror("open");
77 exit(EXIT_FAILURE);
78 }
79
80 do {
81 /*
82 * tee stdin to stdout.
83 */
84 len = tee(STDIN_FILENO, STDOUT_FILENO,
85 INT_MAX, SPLICE_F_NONBLOCK);
86
87 if (len < 0) {
88 if (errno == EAGAIN)
89 continue;
90 perror("tee");
91 exit(EXIT_FAILURE);
92 } else
93 if (len == 0)
94 break;
95
96 /*
97 * Consume stdin by splicing it to a file.
98 */
99 while (len > 0) {
100 slen = splice(STDIN_FILENO, NULL, fd, NULL,
101 len, SPLICE_F_MOVE);
102 if (slen < 0) {
103 perror("splice");
104 break;
105 }
106 len -= slen;
107 }
108 } while (1);
109
110 close(fd);
111 exit(EXIT_SUCCESS);
112 }
113
115 The tee(2) system call first appeared in Linux-2.6.17.
116
118 This system call is Linux specific.
119
121 splice(2), vmsplice(2), feature_test_macros(7)
122
123
124
125Linux 2.6.17 2006-04-28 tee(2)