]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/tee.2
Many pages: Fix style issues reported by `make lint-groff`
[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.\"
5fbde956 4.\" SPDX-License-Identifier: Linux-man-pages-copyleft
2bc4bb77 5.\"
bffbb22f 6.TH TEE 2 2020-06-09 "Linux" "Linux Programmer's Manual"
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>
dbfe9c70 16.PP
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).
efeece04 39.PP
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 .
efeece04 75.PP
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.
2dd578fd
MK
103.SH VERSIONS
104The
2777b1ca 105.BR tee ()
c95b6ae1
MK
106system call first appeared in Linux 2.6.17;
107library support was added to glibc in version 2.5.
47297adb 108.SH CONFORMING TO
8382f16d 109This system call is Linux-specific.
2bc4bb77
MK
110.SH NOTES
111Conceptually,
112.BR tee ()
113copies the data between the two pipes.
114In reality no real data copying takes place though:
115under the covers,
116.BR tee ()
5dec7b73 117assigns data to the output by merely grabbing
2bc4bb77 118a reference to the input.
a14af333 119.SH EXAMPLES
f7d11fda 120The example below implements a basic
2bc4bb77
MK
121.BR tee (1)
122program using the
2777b1ca 123.BR tee ()
c13182ef 124system call.
f7d11fda 125Here is an example of its use:
efeece04 126.PP
f7d11fda 127.in +4n
b8302363 128.EX
ba81327a 129$ \fBdate | ./a.out out.log | cat\fP
f7d11fda
MK
130Tue Oct 28 10:06:00 CET 2014
131$ \fBcat out.log\fP
132Tue Oct 28 10:06:00 CET 2014
b8302363 133.EE
f7d11fda
MK
134.in
135.SS Program source
c7885256 136\&
e7d0bb47 137.EX
2bc4bb77
MK
138#define _GNU_SOURCE
139#include <fcntl.h>
140#include <stdio.h>
141#include <stdlib.h>
142#include <unistd.h>
2bc4bb77
MK
143#include <errno.h>
144#include <limits.h>
145
146int
147main(int argc, char *argv[])
148{
149 int fd;
c9e80f29 150 ssize_t len, slen;
2bc4bb77 151
6b34fb3f 152 if (argc != 2) {
d1a71985 153 fprintf(stderr, "Usage: %s <file>\en", argv[0]);
99c1a999 154 exit(EXIT_FAILURE);
6b34fb3f 155 }
2bc4bb77
MK
156
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 }
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;
2bc4bb77
MK
177
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 }
2bc4bb77
MK
191
192 close(fd);
193 exit(EXIT_SUCCESS);
194}
e7d0bb47 195.EE
2bc4bb77
MK
196.SH SEE ALSO
197.BR splice (2),
6c97eb40
MK
198.BR vmsplice (2),
199.BR pipe (7)