]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/tee.2
splice.2, tee.2, vmsplice.2: Fix return type
[thirdparty/man-pages.git] / man2 / tee.2
CommitLineData
2bc4bb77
MK
1.\" Hey Emacs! This file is -*- nroff -*- source.
2.\"
3.\" This manpage is Copyright (C) 2006 Jens Axboe
c11b1abf 4.\" and Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
2bc4bb77
MK
5.\"
6.\" Permission is granted to make and distribute verbatim copies of this
7.\" manual provided the copyright notice and this permission notice are
8.\" preserved on all copies.
9.\"
10.\" Permission is granted to copy and distribute modified versions of this
11.\" manual under the conditions for verbatim copying, provided that the
12.\" entire resulting derived work is distributed under the terms of a
13.\" permission notice identical to this one.
c13182ef 14.\"
2bc4bb77
MK
15.\" Since the Linux kernel and libraries are constantly changing, this
16.\" manual page may be incorrect or out-of-date. The author(s) assume no
17.\" responsibility for errors or omissions, or for damages resulting from
18.\" the use of the information contained herein. The author(s) may not
19.\" have taken the same level of care in the production of this manual,
20.\" which is licensed free of charge, as they might when working
21.\" professionally.
c13182ef 22.\"
2bc4bb77
MK
23.\" Formatted or processed versions of this manual, if unaccompanied by
24.\" the source, must acknowledge the copyright and authors of this work.
25.\"
52520fb5 26.TH TEE 2 2009-09-15 "Linux" "Linux Programmer's Manual"
2bc4bb77
MK
27.SH NAME
28tee \- duplicating pipe content
29.SH SYNOPSIS
30.nf
31.B #define _GNU_SOURCE
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
58is a series of modifier flags, which share the name space with
59.BR splice (2)
60and
61.BR vmsplice (2):
62.TP 1.9i
63.B SPLICE_F_MOVE
c13182ef 64Currently has no effect for
2bc4bb77
MK
65.BR tee ();
66see
67.BR splice (2).
68.TP
69.B SPLICE_F_NONBLOCK
c13182ef
MK
70Do not block on I/O; see
71.BR splice (2)
2bc4bb77
MK
72for further details.
73.TP
74.B SPLICE_F_MORE
c13182ef 75Currently has no effect for
2bc4bb77
MK
76.BR tee (),
77but may be implemented in the future; see
78.BR splice (2).
79.TP
80.B SPLICE_F_GIFT
81Unused for
82.BR tee ();
83see
84.BR vmsplice (2).
85.SH RETURN VALUE
86Upon successful completion,
87.BR tee ()
88returns the number of bytes that were duplicated between the input
c13182ef
MK
89and output.
90A return value of 0 means that there was no data to transfer,
91and it would not make sense to block, because there are no
92writers connected to the write end of the pipe referred to by
2bc4bb77
MK
93.IR fd_in .
94
c13182ef 95On error,
2bc4bb77
MK
96.BR tee ()
97returns \-1 and
98.I errno
99is set to indicate the error.
100.SH ERRORS
c13182ef 101.TP
2bc4bb77
MK
102.B EINVAL
103.I fd_in
104or
105.I fd_out
106does not refer to a pipe; or
107.I fd_in
108and
109.I fd_out
110refer to the same pipe.
111.TP
112.B ENOMEM
113Out of memory.
2dd578fd
MK
114.SH VERSIONS
115The
2777b1ca 116.BR tee ()
ceaeac24 117system call first appeared in Linux 2.6.17.
2dd578fd 118.SH "CONFORMING TO"
8382f16d 119This system call is Linux-specific.
2bc4bb77
MK
120.SH NOTES
121Conceptually,
122.BR tee ()
123copies the data between the two pipes.
124In reality no real data copying takes place though:
125under the covers,
126.BR tee ()
c13182ef 127assigns data in the output by merely grabbing
2bc4bb77
MK
128a reference to the input.
129.SH EXAMPLE
130The following example implements a basic
131.BR tee (1)
132program using the
2777b1ca 133.BR tee ()
c13182ef 134system call.
2bc4bb77
MK
135.nf
136
137#define _GNU_SOURCE
138#include <fcntl.h>
139#include <stdio.h>
140#include <stdlib.h>
141#include <unistd.h>
142#include <assert.h>
143#include <errno.h>
144#include <limits.h>
145
146int
147main(int argc, char *argv[])
148{
149 int fd;
150 int len, slen;
151
152 assert(argc == 2);
153
154 fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
29059a65 155 if (fd == \-1) {
2bc4bb77
MK
156 perror("open");
157 exit(EXIT_FAILURE);
158 }
159
160 do {
161 /*
162 * tee stdin to stdout.
163 */
164 len = tee(STDIN_FILENO, STDOUT_FILENO,
165 INT_MAX, SPLICE_F_NONBLOCK);
166
167 if (len < 0) {
168 if (errno == EAGAIN)
169 continue;
170 perror("tee");
171 exit(EXIT_FAILURE);
172 } else
173 if (len == 0)
174 break;
175
176 /*
177 * Consume stdin by splicing it to a file.
178 */
179 while (len > 0) {
180 slen = splice(STDIN_FILENO, NULL, fd, NULL,
181 len, SPLICE_F_MOVE);
182 if (slen < 0) {
183 perror("splice");
184 break;
185 }
29059a65 186 len \-= slen;
2bc4bb77
MK
187 }
188 } while (1);
189
190 close(fd);
191 exit(EXIT_SUCCESS);
192}
193.fi
2bc4bb77
MK
194.SH SEE ALSO
195.BR splice (2),
0a90178c
MK
196.BR vmsplice (2),
197.BR feature_test_macros (7)