man-pages/man2/tee.2.html
2021-03-31 01:06:50 +01:00

308 lines
8.8 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of TEE</TITLE>
</HEAD><BODY>
<H1>TEE</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
tee - duplicating pipe content
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#define _GNU_SOURCE</B> /* See <A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A>(7) */
<B>#include &lt;<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>&gt;</B>
<B>ssize_t tee(int </B><I>fd_in</I><B>, int </B><I>fd_out</I><B>, size_t </B><I>len</I><B>, unsigned int </B><I>flags</I><B>);</B>
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>tee</B>()
duplicates up to
<I>len</I>
bytes of data from the pipe referred to by the file descriptor
<I>fd_in</I>
to the pipe referred to by the file descriptor
<I>fd_out</I>.
It does not consume the data that is duplicated from
<I>fd_in</I>;
therefore, that data can be copied by a subsequent
<B><A HREF="/cgi-bin/man/man2html?2+splice">splice</A></B>(2).
<P>
<I>flags</I>
is a bit mask that is composed by ORing together
zero or more of the following values:
<DL COMPACT>
<DT id="1"><B>SPLICE_F_MOVE</B>
<DD>
Currently has no effect for
<B>tee</B>();
see
<B><A HREF="/cgi-bin/man/man2html?2+splice">splice</A></B>(2).
<DT id="2"><B>SPLICE_F_NONBLOCK</B>
<DD>
Do not block on I/O; see
<B><A HREF="/cgi-bin/man/man2html?2+splice">splice</A></B>(2)
for further details.
<DT id="3"><B>SPLICE_F_MORE</B>
<DD>
Currently has no effect for
<B>tee</B>(),
but may be implemented in the future; see
<B><A HREF="/cgi-bin/man/man2html?2+splice">splice</A></B>(2).
<DT id="4"><B>SPLICE_F_GIFT</B>
<DD>
Unused for
<B>tee</B>();
see
<B><A HREF="/cgi-bin/man/man2html?2+vmsplice">vmsplice</A></B>(2).
</DL>
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
Upon successful completion,
<B>tee</B>()
returns the number of bytes that were duplicated between the input
and output.
A return value of 0 means that there was no data to transfer,
and it would not make sense to block, because there are no
writers connected to the write end of the pipe referred to by
<I>fd_in</I>.
<P>
On error,
<B>tee</B>()
returns -1 and
<I>errno</I>
is set to indicate the error.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="5"><B>EAGAIN</B>
<DD>
<B>SPLICE_F_NONBLOCK</B>
was specified in
<I>flags</I>
or one of the file descriptors had been marked as nonblocking
(<B>O_NONBLOCK</B>)<B>,</B>
and the operation would block.
<DT id="6"><B>EINVAL</B>
<DD>
<I>fd_in</I>
or
<I>fd_out</I>
does not refer to a pipe; or
<I>fd_in</I>
and
<I>fd_out</I>
refer to the same pipe.
<DT id="7"><B>ENOMEM</B>
<DD>
Out of memory.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
The
<B>tee</B>()
system call first appeared in Linux 2.6.17;
library support was added to glibc in version 2.5.
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
This system call is Linux-specific.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
Conceptually,
<B>tee</B>()
copies the data between the two pipes.
In reality no real data copying takes place though:
under the covers,
<B>tee</B>()
assigns data to the output by merely grabbing
a reference to the input.
<A NAME="lbAJ">&nbsp;</A>
<H2>EXAMPLE</H2>
The example below implements a basic
<B><A HREF="/cgi-bin/man/man2html?1+tee">tee</A></B>(1)
program using the
<B>tee</B>()
system call.
Here is an example of its use:
<P>
$ <B>date |./a.out out.log | cat</B>
Tue Oct 28 10:06:00 CET 2014
$ <B>cat out.log</B>
Tue Oct 28 10:06:00 CET 2014
<A NAME="lbAK">&nbsp;</A>
<H3>Program source</H3>
#define _GNU_SOURCE
#include &lt;<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/errno.h">errno.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/limits.h">limits.h</A>&gt;
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;fd;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;len,&nbsp;slen;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;!=&nbsp;2)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Usage:&nbsp;%s&nbsp;&lt;file&gt;\n&quot;,&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fd&nbsp;=&nbsp;open(argv[1],&nbsp;O_WRONLY&nbsp;|&nbsp;O_CREAT&nbsp;|&nbsp;O_TRUNC,&nbsp;0644);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(fd&nbsp;==&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;open&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&n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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;splice&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len&nbsp;-=&nbsp;slen;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;while&nbsp;(1);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;close(fd);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAL">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+splice">splice</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+vmsplice">vmsplice</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?7+pipe">pipe</A></B>(7)
<A NAME="lbAM">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="8"><A HREF="#lbAB">NAME</A><DD>
<DT id="9"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="10"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="11"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="12"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="13"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="14"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="15"><A HREF="#lbAI">NOTES</A><DD>
<DT id="16"><A HREF="#lbAJ">EXAMPLE</A><DD>
<DL>
<DT id="17"><A HREF="#lbAK">Program source</A><DD>
</DL>
<DT id="18"><A HREF="#lbAL">SEE ALSO</A><DD>
<DT id="19"><A HREF="#lbAM">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:35 GMT, March 31, 2021
</BODY>
</HTML>