]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/tee.2
fuse.4: ffix
[thirdparty/man-pages.git] / 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.\"
93015253 4.\" %%%LICENSE_START(VERBATIM)
2bc4bb77
MK
5.\" Permission is granted to make and distribute verbatim copies of this
6.\" manual provided the copyright notice and this permission notice are
7.\" preserved on all copies.
8.\"
9.\" Permission is granted to copy and distribute modified versions of this
10.\" manual under the conditions for verbatim copying, provided that the
11.\" entire resulting derived work is distributed under the terms of a
12.\" permission notice identical to this one.
c13182ef 13.\"
2bc4bb77
MK
14.\" Since the Linux kernel and libraries are constantly changing, this
15.\" manual page may be incorrect or out-of-date. The author(s) assume no
16.\" responsibility for errors or omissions, or for damages resulting from
17.\" the use of the information contained herein. The author(s) may not
18.\" have taken the same level of care in the production of this manual,
19.\" which is licensed free of charge, as they might when working
20.\" professionally.
c13182ef 21.\"
2bc4bb77
MK
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 24.\" %%%LICENSE_END
2bc4bb77 25.\"
0649afd4 26.TH TEE 2 2014-12-31 "Linux" "Linux Programmer's Manual"
2bc4bb77
MK
27.SH NAME
28tee \- duplicating pipe content
29.SH SYNOPSIS
30.nf
b80f966b 31.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
2bc4bb77
MK
32.B #include <fcntl.h>
33
52520fb5 34.BI "ssize_t tee(int " fd_in ", int " fd_out ", size_t " len \
2bc4bb77
MK
35", unsigned int " flags );
36.fi
52520fb5 37.\" Return type was long before glibc 2.7
2bc4bb77
MK
38.SH DESCRIPTION
39.\" Example programs http://brick.kernel.dk/snaps
40.\"
41.\"
c13182ef
MK
42.\" add a "tee(in, out1, out2)" system call that duplicates the pages
43.\" (again, incrementing their reference count, not copying the data) from
2bc4bb77
MK
44.\" one pipe to two other pipes.
45.BR tee ()
46duplicates up to
47.I len
48bytes of data from the pipe referred to by the file descriptor
49.I fd_in
50to the pipe referred to by the file descriptor
51.IR fd_out .
52It does not consume the data that is duplicated from
c13182ef
MK
53.IR fd_in ;
54therefore, that data can be copied by a subsequent
2bc4bb77
MK
55.BR splice (2).
56
57.I flags
3111b6a6 58is a bit mask that is composed by ORing together
0bd79129 59zero or more of the following values:
2bc4bb77
MK
60.TP 1.9i
61.B SPLICE_F_MOVE
c13182ef 62Currently has no effect for
2bc4bb77
MK
63.BR tee ();
64see
65.BR splice (2).
66.TP
67.B SPLICE_F_NONBLOCK
c13182ef
MK
68Do not block on I/O; see
69.BR splice (2)
2bc4bb77
MK
70for further details.
71.TP
72.B SPLICE_F_MORE
c13182ef 73Currently has no effect for
2bc4bb77
MK
74.BR tee (),
75but may be implemented in the future; see
76.BR splice (2).
77.TP
78.B SPLICE_F_GIFT
79Unused for
80.BR tee ();
81see
82.BR vmsplice (2).
83.SH RETURN VALUE
84Upon successful completion,
85.BR tee ()
86returns the number of bytes that were duplicated between the input
c13182ef
MK
87and output.
88A return value of 0 means that there was no data to transfer,
89and it would not make sense to block, because there are no
90writers connected to the write end of the pipe referred to by
2bc4bb77
MK
91.IR fd_in .
92
c13182ef 93On error,
2bc4bb77
MK
94.BR tee ()
95returns \-1 and
96.I errno
97is set to indicate the error.
98.SH ERRORS
c13182ef 99.TP
b7224af9
MK
100.B EAGAIN
101.B SPLICE_F_NONBLOCK
102was specified in
103.IR flags ,
104and the operation would block.
105.TP
2bc4bb77
MK
106.B EINVAL
107.I fd_in
108or
109.I fd_out
110does not refer to a pipe; or
111.I fd_in
112and
113.I fd_out
114refer to the same pipe.
115.TP
116.B ENOMEM
117Out of memory.
2dd578fd
MK
118.SH VERSIONS
119The
2777b1ca 120.BR tee ()
c95b6ae1
MK
121system call first appeared in Linux 2.6.17;
122library support was added to glibc in version 2.5.
47297adb 123.SH CONFORMING TO
8382f16d 124This system call is Linux-specific.
2bc4bb77
MK
125.SH NOTES
126Conceptually,
127.BR tee ()
128copies the data between the two pipes.
129In reality no real data copying takes place though:
130under the covers,
131.BR tee ()
5dec7b73 132assigns data to the output by merely grabbing
2bc4bb77
MK
133a reference to the input.
134.SH EXAMPLE
f7d11fda 135The example below implements a basic
2bc4bb77
MK
136.BR tee (1)
137program using the
2777b1ca 138.BR tee ()
c13182ef 139system call.
f7d11fda
MK
140Here is an example of its use:
141
142.in +4n
143.nf
144$ \fBdate |./a.out out.log | cat\fP
145Tue Oct 28 10:06:00 CET 2014
146$ \fBcat out.log\fP
147Tue Oct 28 10:06:00 CET 2014
148.fi
149.in
150.SS Program source
2bc4bb77
MK
151.nf
152
153#define _GNU_SOURCE
154#include <fcntl.h>
155#include <stdio.h>
156#include <stdlib.h>
157#include <unistd.h>
2bc4bb77
MK
158#include <errno.h>
159#include <limits.h>
160
161int
162main(int argc, char *argv[])
163{
164 int fd;
165 int len, slen;
166
6b34fb3f 167 if (argc != 2) {
99c1a999
SL
168 fprintf(stderr, "Usage: %s <file>\\n", argv[0]);
169 exit(EXIT_FAILURE);
6b34fb3f 170 }
2bc4bb77
MK
171
172 fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
29059a65 173 if (fd == \-1) {
2bc4bb77
MK
174 perror("open");
175 exit(EXIT_FAILURE);
176 }
177
178 do {
179 /*
180 * tee stdin to stdout.
181 */
182 len = tee(STDIN_FILENO, STDOUT_FILENO,
183 INT_MAX, SPLICE_F_NONBLOCK);
184
185 if (len < 0) {
186 if (errno == EAGAIN)
187 continue;
188 perror("tee");
189 exit(EXIT_FAILURE);
190 } else
191 if (len == 0)
192 break;
193
194 /*
195 * Consume stdin by splicing it to a file.
196 */
197 while (len > 0) {
198 slen = splice(STDIN_FILENO, NULL, fd, NULL,
199 len, SPLICE_F_MOVE);
200 if (slen < 0) {
201 perror("splice");
202 break;
203 }
29059a65 204 len \-= slen;
2bc4bb77
MK
205 }
206 } while (1);
207
208 close(fd);
209 exit(EXIT_SUCCESS);
210}
211.fi
2bc4bb77
MK
212.SH SEE ALSO
213.BR splice (2),
0a4f8b7b 214.BR vmsplice (2)