308 lines
8.8 KiB
HTML
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"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
tee - duplicating pipe content
|
|
<A NAME="lbAC"> </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 <<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>></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"> </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"> </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"> </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"> </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"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
This system call is Linux-specific.
|
|
<A NAME="lbAI"> </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"> </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"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
#include <<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
#include <<A HREF="file:///usr/include/errno.h">errno.h</A>>
|
|
#include <<A HREF="file:///usr/include/limits.h">limits.h</A>>
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> int fd;
|
|
<BR> int len, slen;
|
|
<P>
|
|
<BR> if (argc != 2) {
|
|
<BR> fprintf(stderr, "Usage: %s <file>\n", argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
|
<BR> if (fd == -1) {
|
|
<BR> perror("open");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> do {
|
|
<BR> /*
|
|
<BR> * tee stdin to stdout.
|
|
<BR> */
|
|
<BR> len = tee(STDIN_FILENO, STDOUT_FILENO,
|
|
<BR> INT_MAX, SPLICE_F_NONBLOCK);
|
|
<P>
|
|
<BR> if (len < 0) {
|
|
<BR> if (errno == EAGAIN)
|
|
<BR> continue;
|
|
<BR> perror("tee");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> } else
|
|
<BR> if (len == 0)
|
|
<BR> break;
|
|
<P>
|
|
<BR> /*
|
|
<BR> * Consume stdin by splicing it to a file.
|
|
<BR> */
|
|
<BR> while (len > 0) {
|
|
<BR> slen = splice(STDIN_FILENO, NULL, fd, NULL,
|
|
<BR> len, SPLICE_F_MOVE);
|
|
<BR> if (slen < 0) {
|
|
<BR> perror("splice");
|
|
<BR> break;
|
|
<BR> }
|
|
<BR> len -= slen;
|
|
<BR> }
|
|
<BR> } while (1);
|
|
<P>
|
|
<BR> close(fd);
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAL"> </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"> </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"> </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>
|