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,  and the operation
44              would block.
45
46       EINVAL fd_in or fd_out does not refer to a pipe; or  fd_in  and  fd_out
47              refer to the same pipe.
48
49       ENOMEM Out of memory.
50

VERSIONS

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

CONFORMING TO

56       This system call is Linux-specific.
57

NOTES

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

EXAMPLE

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

SEE ALSO

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

COLOPHON

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