1TEE(2)                     Linux Programmer's Manual                    TEE(2)
2
3
4

NAME

6       tee - duplicating pipe content
7

SYNOPSIS

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

DESCRIPTION

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 bit mask that is composed by ORing together zero or more  of
21       the following values:
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

RETURN VALUE

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

ERRORS

43       EAGAIN SPLICE_F_NONBLOCK  was  specified  in  flags  or one of the file
44              descriptors had been marked as nonblocking (O_NONBLOCK), and the
45              operation would block.
46
47       EINVAL fd_in  or  fd_out  does not refer to a pipe; or fd_in and fd_out
48              refer to the same pipe.
49
50       ENOMEM Out of memory.
51

VERSIONS

53       The tee() system call first appeared in Linux 2.6.17;  library  support
54       was added to glibc in version 2.5.
55

CONFORMING TO

57       This system call is Linux-specific.
58

NOTES

60       Conceptually,  tee() copies the data between the two pipes.  In reality
61       no real data copying  takes  place  though:  under  the  covers,  tee()
62       assigns data to the output by merely grabbing a reference to the input.
63

EXAMPLES

65       The  example  below  implements  a basic tee(1) program using the tee()
66       system call.  Here is an example of its use:
67
68           $ date |./a.out out.log | cat
69           Tue Oct 28 10:06:00 CET 2014
70           $ cat out.log
71           Tue Oct 28 10:06:00 CET 2014
72
73   Program source
74
75       #define _GNU_SOURCE
76       #include <fcntl.h>
77       #include <stdio.h>
78       #include <stdlib.h>
79       #include <unistd.h>
80       #include <errno.h>
81       #include <limits.h>
82
83       int
84       main(int argc, char *argv[])
85       {
86           int fd;
87           int len, slen;
88
89           if (argc != 2) {
90               fprintf(stderr, "Usage: %s <file>\n", argv[0]);
91               exit(EXIT_FAILURE);
92           }
93
94           fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
95           if (fd == -1) {
96               perror("open");
97               exit(EXIT_FAILURE);
98           }
99
100           do {
101               /*
102                * tee stdin to stdout.
103                */
104               len = tee(STDIN_FILENO, STDOUT_FILENO,
105                         INT_MAX, SPLICE_F_NONBLOCK);
106
107               if (len < 0) {
108                   if (errno == EAGAIN)
109                       continue;
110                   perror("tee");
111                   exit(EXIT_FAILURE);
112               } else
113                   if (len == 0)
114                       break;
115
116               /*
117                * Consume stdin by splicing it to a file.
118                */
119               while (len > 0) {
120                   slen = splice(STDIN_FILENO, NULL, fd, NULL,
121                                 len, SPLICE_F_MOVE);
122                   if (slen < 0) {
123                       perror("splice");
124                       break;
125                   }
126                   len -= slen;
127               }
128           } while (1);
129
130           close(fd);
131           exit(EXIT_SUCCESS);
132       }
133

SEE ALSO

135       splice(2), vmsplice(2), pipe(7)
136

COLOPHON

138       This page is part of release 5.07 of the Linux  man-pages  project.   A
139       description  of  the project, information about reporting bugs, and the
140       latest    version    of    this    page,    can     be     found     at
141       https://www.kernel.org/doc/man-pages/.
142
143
144
145Linux                             2020-06-09                            TEE(2)
Impressum