]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man/man2/tee.2
man/: EXAMPLES: Add missing includes
[thirdparty/man-pages.git] / man / man2 / tee.2
CommitLineData
2bc4bb77 1.\" This manpage is Copyright (C) 2006 Jens Axboe
c11b1abf 2.\" and Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
2bc4bb77 3.\"
5fbde956 4.\" SPDX-License-Identifier: Linux-man-pages-copyleft
2bc4bb77 5.\"
4c1c5274 6.TH tee 2 (date) "Linux man-pages (unreleased)"
2bc4bb77
MK
7.SH NAME
8tee \- duplicating pipe content
62e4321c
AC
9.SH LIBRARY
10Standard C library
8fc3b2cf 11.RI ( libc ", " \-lc )
2bc4bb77
MK
12.SH SYNOPSIS
13.nf
b80f966b 14.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
2bc4bb77 15.B #include <fcntl.h>
c6d039a3 16.P
52520fb5 17.BI "ssize_t tee(int " fd_in ", int " fd_out ", size_t " len \
2bc4bb77
MK
18", unsigned int " flags );
19.fi
52520fb5 20.\" Return type was long before glibc 2.7
2bc4bb77
MK
21.SH DESCRIPTION
22.\" Example programs http://brick.kernel.dk/snaps
23.\"
24.\"
c13182ef
MK
25.\" add a "tee(in, out1, out2)" system call that duplicates the pages
26.\" (again, incrementing their reference count, not copying the data) from
2bc4bb77
MK
27.\" one pipe to two other pipes.
28.BR tee ()
29duplicates up to
30.I len
31bytes of data from the pipe referred to by the file descriptor
32.I fd_in
33to the pipe referred to by the file descriptor
34.IR fd_out .
35It does not consume the data that is duplicated from
c13182ef
MK
36.IR fd_in ;
37therefore, that data can be copied by a subsequent
2bc4bb77 38.BR splice (2).
c6d039a3 39.P
2bc4bb77 40.I flags
3111b6a6 41is a bit mask that is composed by ORing together
0bd79129 42zero or more of the following values:
2bc4bb77
MK
43.TP 1.9i
44.B SPLICE_F_MOVE
c13182ef 45Currently has no effect for
2bc4bb77
MK
46.BR tee ();
47see
48.BR splice (2).
49.TP
50.B SPLICE_F_NONBLOCK
c13182ef
MK
51Do not block on I/O; see
52.BR splice (2)
2bc4bb77
MK
53for further details.
54.TP
55.B SPLICE_F_MORE
c13182ef 56Currently has no effect for
2bc4bb77
MK
57.BR tee (),
58but may be implemented in the future; see
59.BR splice (2).
60.TP
61.B SPLICE_F_GIFT
62Unused for
63.BR tee ();
64see
65.BR vmsplice (2).
66.SH RETURN VALUE
67Upon successful completion,
68.BR tee ()
69returns the number of bytes that were duplicated between the input
c13182ef
MK
70and output.
71A return value of 0 means that there was no data to transfer,
72and it would not make sense to block, because there are no
73writers connected to the write end of the pipe referred to by
2bc4bb77 74.IR fd_in .
c6d039a3 75.P
c13182ef 76On error,
2bc4bb77
MK
77.BR tee ()
78returns \-1 and
79.I errno
80is set to indicate the error.
81.SH ERRORS
c13182ef 82.TP
b7224af9
MK
83.B EAGAIN
84.B SPLICE_F_NONBLOCK
85was specified in
1ae6b2c7 86.I flags
5ca397fa
SK
87or one of the file descriptors had been marked as nonblocking
88.RB ( O_NONBLOCK ) ,
b7224af9
MK
89and the operation would block.
90.TP
2bc4bb77
MK
91.B EINVAL
92.I fd_in
93or
94.I fd_out
95does not refer to a pipe; or
96.I fd_in
97and
98.I fd_out
99refer to the same pipe.
100.TP
101.B ENOMEM
102Out of memory.
3113c7f3 103.SH STANDARDS
4131356c
AC
104Linux.
105.SH HISTORY
106Linux 2.6.17,
107glibc 2.5.
2bc4bb77
MK
108.SH NOTES
109Conceptually,
110.BR tee ()
111copies the data between the two pipes.
112In reality no real data copying takes place though:
113under the covers,
114.BR tee ()
5dec7b73 115assigns data to the output by merely grabbing
2bc4bb77 116a reference to the input.
a14af333 117.SH EXAMPLES
f7d11fda 118The example below implements a basic
2bc4bb77
MK
119.BR tee (1)
120program using the
2777b1ca 121.BR tee ()
c13182ef 122system call.
f7d11fda 123Here is an example of its use:
c6d039a3 124.P
f7d11fda 125.in +4n
b8302363 126.EX
ba81327a 127$ \fBdate | ./a.out out.log | cat\fP
f7d11fda
MK
128Tue Oct 28 10:06:00 CET 2014
129$ \fBcat out.log\fP
130Tue Oct 28 10:06:00 CET 2014
b8302363 131.EE
f7d11fda
MK
132.in
133.SS Program source
c7885256 134\&
33857069 135.\" SRC BEGIN (tee.c)
e7d0bb47 136.EX
2bc4bb77 137#define _GNU_SOURCE
4ae706b0 138#include <errno.h>
2bc4bb77 139#include <fcntl.h>
4ae706b0 140#include <limits.h>
2bc4bb77
MK
141#include <stdio.h>
142#include <stdlib.h>
0320049e 143#include <sys/types.h>
2bc4bb77 144#include <unistd.h>
fe5dba13 145\&
2bc4bb77
MK
146int
147main(int argc, char *argv[])
148{
0b94bd78
AC
149 int fd;
150 ssize_t len, slen;
fe5dba13 151\&
6b34fb3f 152 if (argc != 2) {
d1a71985 153 fprintf(stderr, "Usage: %s <file>\en", argv[0]);
99c1a999 154 exit(EXIT_FAILURE);
6b34fb3f 155 }
fe5dba13 156\&
2bc4bb77 157 fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
29059a65 158 if (fd == \-1) {
2bc4bb77
MK
159 perror("open");
160 exit(EXIT_FAILURE);
161 }
fe5dba13 162\&
3be51e47 163 for (;;) {
2bc4bb77
MK
164 /*
165 * tee stdin to stdout.
166 */
167 len = tee(STDIN_FILENO, STDOUT_FILENO,
168 INT_MAX, SPLICE_F_NONBLOCK);
2bc4bb77
MK
169 if (len < 0) {
170 if (errno == EAGAIN)
171 continue;
172 perror("tee");
173 exit(EXIT_FAILURE);
4208f16e
AC
174 }
175 if (len == 0)
176 break;
fe5dba13 177\&
2bc4bb77
MK
178 /*
179 * Consume stdin by splicing it to a file.
180 */
181 while (len > 0) {
182 slen = splice(STDIN_FILENO, NULL, fd, NULL,
183 len, SPLICE_F_MOVE);
184 if (slen < 0) {
185 perror("splice");
706ff40f 186 exit(EXIT_FAILURE);
2bc4bb77 187 }
29059a65 188 len \-= slen;
2bc4bb77 189 }
3be51e47 190 }
fe5dba13 191\&
2bc4bb77
MK
192 close(fd);
193 exit(EXIT_SUCCESS);
194}
e7d0bb47 195.EE
33857069 196.\" SRC END
2bc4bb77
MK
197.SH SEE ALSO
198.BR splice (2),
6c97eb40
MK
199.BR vmsplice (2),
200.BR pipe (7)