OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man2 / tee.2
1 .\" This manpage is Copyright (C) 2006 Jens Axboe
2 .\" and Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
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.
13 .\"
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.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH TEE 2 2014-12-31 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 tee \- duplicating pipe content
29 .SH SYNOPSIS
30 .nf
31 .BR "#define _GNU_SOURCE" "         /* See feature_test_macros(7) */"
32 .B #include <fcntl.h>
33
34 .BI "ssize_t tee(int " fd_in ", int " fd_out ", size_t " len \
35 ", unsigned int " flags );
36 .fi
37 .\" Return type was long before glibc 2.7
38 .SH DESCRIPTION
39 .\" Example programs http://brick.kernel.dk/snaps
40 .\"
41 .\"
42 .\" add a "tee(in, out1, out2)" system call that duplicates the pages
43 .\" (again, incrementing their reference count, not copying the data) from
44 .\" one pipe to two other pipes.
45 .BR tee ()
46 duplicates up to
47 .I len
48 bytes of data from the pipe referred to by the file descriptor
49 .I fd_in
50 to the pipe referred to by the file descriptor
51 .IR fd_out .
52 It does not consume the data that is duplicated from
53 .IR fd_in ;
54 therefore, that data can be copied by a subsequent
55 .BR splice (2).
56
57 .I flags
58 is a series of modifier flags, which share the name space with
59 .BR splice (2)
60 and
61 .BR vmsplice (2):
62 .TP 1.9i
63 .B SPLICE_F_MOVE
64 Currently has no effect for
65 .BR tee ();
66 see
67 .BR splice (2).
68 .TP
69 .B SPLICE_F_NONBLOCK
70 Do not block on I/O; see
71 .BR splice (2)
72 for further details.
73 .TP
74 .B SPLICE_F_MORE
75 Currently has no effect for
76 .BR tee (),
77 but may be implemented in the future; see
78 .BR splice (2).
79 .TP
80 .B SPLICE_F_GIFT
81 Unused for
82 .BR tee ();
83 see
84 .BR vmsplice (2).
85 .SH RETURN VALUE
86 Upon successful completion,
87 .BR tee ()
88 returns the number of bytes that were duplicated between the input
89 and output.
90 A return value of 0 means that there was no data to transfer,
91 and it would not make sense to block, because there are no
92 writers connected to the write end of the pipe referred to by
93 .IR fd_in .
94
95 On error,
96 .BR tee ()
97 returns \-1 and
98 .I errno
99 is set to indicate the error.
100 .SH ERRORS
101 .TP
102 .B EAGAIN
103 .B SPLICE_F_NONBLOCK
104 was specified in
105 .IR flags ,
106 and the operation would block.
107 .TP
108 .B EINVAL
109 .I fd_in
110 or
111 .I fd_out
112 does not refer to a pipe; or
113 .I fd_in
114 and
115 .I fd_out
116 refer to the same pipe.
117 .TP
118 .B ENOMEM
119 Out of memory.
120 .SH VERSIONS
121 The
122 .BR tee ()
123 system call first appeared in Linux 2.6.17;
124 library support was added to glibc in version 2.5.
125 .SH CONFORMING TO
126 This system call is Linux-specific.
127 .SH NOTES
128 Conceptually,
129 .BR tee ()
130 copies the data between the two pipes.
131 In reality no real data copying takes place though:
132 under the covers,
133 .BR tee ()
134 assigns data in the output by merely grabbing
135 a reference to the input.
136 .SH EXAMPLE
137 The example below implements a basic
138 .BR tee (1)
139 program using the
140 .BR tee ()
141 system call.
142 Here is an example of its use:
143
144 .in +4n
145 .nf
146 $ \fBdate |./a.out out.log | cat\fP
147 Tue Oct 28 10:06:00 CET 2014
148 $ \fBcat out.log\fP
149 Tue Oct 28 10:06:00 CET 2014
150 .fi
151 .in
152 .SS Program source
153 .nf
154
155 #define _GNU_SOURCE
156 #include <fcntl.h>
157 #include <stdio.h>
158 #include <stdlib.h>
159 #include <unistd.h>
160 #include <errno.h>
161 #include <limits.h>
162
163 int
164 main(int argc, char *argv[])
165 {
166     int fd;
167     int len, slen;
168
169     if (argc != 2) {
170         fprintf(stderr, "Usage: %s <file>\\n", argv[0]);
171         exit(EXIT_FAILURE);
172     }
173
174     fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
175     if (fd == \-1) {
176         perror("open");
177         exit(EXIT_FAILURE);
178     }
179
180     do {
181         /*
182          * tee stdin to stdout.
183          */
184         len = tee(STDIN_FILENO, STDOUT_FILENO,
185                   INT_MAX, SPLICE_F_NONBLOCK);
186
187         if (len < 0) {
188             if (errno == EAGAIN)
189                 continue;
190             perror("tee");
191             exit(EXIT_FAILURE);
192         } else
193             if (len == 0)
194                 break;
195
196         /*
197          * Consume stdin by splicing it to a file.
198          */
199         while (len > 0) {
200             slen = splice(STDIN_FILENO, NULL, fd, NULL,
201                           len, SPLICE_F_MOVE);
202             if (slen < 0) {
203                 perror("splice");
204                 break;
205             }
206             len \-= slen;
207         }
208     } while (1);
209
210     close(fd);
211     exit(EXIT_SUCCESS);
212 }
213 .fi
214 .SH SEE ALSO
215 .BR splice (2),
216 .BR vmsplice (2)
217 .SH COLOPHON
218 This page is part of release 3.79 of the Linux
219 .I man-pages
220 project.
221 A description of the project,
222 information about reporting bugs,
223 and the latest version of this page,
224 can be found at
225 \%http://www.kernel.org/doc/man\-pages/.