1TEE(2) Linux Programmer's Manual TEE(2)
2
3
4
6 tee - duplicating pipe content
7
9 #define _GNU_SOURCE /* See feature_test_macros(7) */
10 #include <fcntl.h>
11
12 ssize_t 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; library support
50 was added to glibc in version 2.5.
51
53 This system call is Linux-specific.
54
56 Conceptually, tee() copies the data between the two pipes. In reality
57 no real data copying takes place though: under the covers, tee()
58 assigns data in the output by merely grabbing a reference to the input.
59
61 The following example implements a basic tee(1) program using the tee()
62 system call.
63
64 #define _GNU_SOURCE
65 #include <fcntl.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <unistd.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 if (argc != 2) {
79 fprintf(stderr, "Usage: %s <file>\n", argv[0]);
80 exit(EXIT_FAILURE);
81 }
82
83 fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
84 if (fd == -1) {
85 perror("open");
86 exit(EXIT_FAILURE);
87 }
88
89 do {
90 /*
91 * tee stdin to stdout.
92 */
93 len = tee(STDIN_FILENO, STDOUT_FILENO,
94 INT_MAX, SPLICE_F_NONBLOCK);
95
96 if (len < 0) {
97 if (errno == EAGAIN)
98 continue;
99 perror("tee");
100 exit(EXIT_FAILURE);
101 } else
102 if (len == 0)
103 break;
104
105 /*
106 * Consume stdin by splicing it to a file.
107 */
108 while (len > 0) {
109 slen = splice(STDIN_FILENO, NULL, fd, NULL,
110 len, SPLICE_F_MOVE);
111 if (slen < 0) {
112 perror("splice");
113 break;
114 }
115 len -= slen;
116 }
117 } while (1);
118
119 close(fd);
120 exit(EXIT_SUCCESS);
121 }
122
124 splice(2), vmsplice(2)
125
127 This page is part of release 3.53 of the Linux man-pages project. A
128 description of the project, and information about reporting bugs, can
129 be found at http://www.kernel.org/doc/man-pages/.
130
131
132
133Linux 2012-05-04 TEE(2)