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 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.
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 <errno.h>
69 #include <limits.h>
70
71 int
72 main(int argc, char *argv[])
73 {
74 int fd;
75 int len, slen;
76
77 if (argc != 2) {
78 fprintf(stderr, "Usage: %s <file>\n", argv[0]);
79 exit(EXIT_FAILURE);
80 }
81
82 fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
83 if (fd == -1) {
84 perror("open");
85 exit(EXIT_FAILURE);
86 }
87
88 do {
89 /*
90 * tee stdin to stdout.
91 */
92 len = tee(STDIN_FILENO, STDOUT_FILENO,
93 INT_MAX, SPLICE_F_NONBLOCK);
94
95 if (len < 0) {
96 if (errno == EAGAIN)
97 continue;
98 perror("tee");
99 exit(EXIT_FAILURE);
100 } else
101 if (len == 0)
102 break;
103
104 /*
105 * Consume stdin by splicing it to a file.
106 */
107 while (len > 0) {
108 slen = splice(STDIN_FILENO, NULL, fd, NULL,
109 len, SPLICE_F_MOVE);
110 if (slen < 0) {
111 perror("splice");
112 break;
113 }
114 len -= slen;
115 }
116 } while (1);
117
118 close(fd);
119 exit(EXIT_SUCCESS);
120 }
121
123 splice(2), vmsplice(2), feature_test_macros(7)
124
126 This page is part of release 3.25 of the Linux man-pages project. A
127 description of the project, and information about reporting bugs, can
128 be found at http://www.kernel.org/doc/man-pages/.
129
130
131
132Linux 2009-09-15 TEE(2)