tee

Section: System Calls (2)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

tee - パイプの中身を複製する  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#define _GNU_SOURCE         /* feature_test_macros(7) 参照 */
#include <fcntl.h>

ssize_t tee(int fd_in, int fd_out, size_t size, unsigned int flags);
 

説明

tee() duplicates up to size bytes of data from the pipe referred to by the file descriptor fd_in to the pipe referred to by the file descriptor fd_out. It does not consume the data that is duplicated from fd_in; therefore, that data can be copied by a subsequent splice(2).

flags is a bit mask that is composed by ORing together zero or more of the following values:

SPLICE_F_MOVE
現在のところ tee() では何の効果もない。 splice(2) 参照。
SPLICE_F_NONBLOCK
入出力で停止 (block) しない。詳細は splice(2) 参照。
SPLICE_F_MORE
現在のところ tee() では何の効果もないが、将来的には実装される可能性がある。 splice(2) 参照。
SPLICE_F_GIFT
tee() では未使用。 vmsplice(2) 参照。
 

返り値

成功して完了すると、 tee() は入出力間で複製されたバイト数を返す。 返り値 0 はデータの転送が行われなかったことを示す。 この場合、処理を停止 (block) しても無意味である。 なぜなら、 fd_in が参照するパイプの書き込み側に接続されている者がいないからである。

エラーの場合、 tee() は -1 を返し、 errno にエラーを示す値を設定する。  

エラー

EAGAIN
SPLICE_F_NONBLOCK was specified in flags or one of the file descriptors had been marked as nonblocking (O_NONBLOCK), and the operation would block.
EINVAL
fd_in と fd_out のどちらかがパイプを参照していない。もしくは fd_in と fd_out が同じパイプを参照している。
ENOMEM
メモリー不足。
 

標準規格

Linux.  

歴史

Linux 2.6.17, glibc 2.5.  

注意

概念としては、 tee() は二つのパイプ間でデータのコピーを行う。 しかし、実際には実データのコピーは行われない。 内部では、 tee() は入力側に対する参照だけを作成することで出力側にデータを 追加する。  

例

以下の例は、 tee() システムコールを使って、 基本的な tee(1) プログラムを実装したものである。 以下は利用例である。

$ date | ./a.out out.log | cat;
Tue Oct 28 10:06:00 CET 2014
$ cat out.log;
Tue Oct 28 10:06:00 CET 2014
 

プログラムのソース

#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
    int      fd;
    ssize_t  size, ssize;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s <file>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    for (;;) {
        /*
         * tee stdin to stdout.
         */
        size = tee(STDIN_FILENO, STDOUT_FILENO,
                  INT_MAX, SPLICE_F_NONBLOCK);
        if (size < 0) {
            if (errno == EAGAIN)
                continue;
            perror("tee");
            exit(EXIT_FAILURE);
        }
        if (size == 0)
            break;

        /*
         * Consume stdin by splicing it to a file.
         */
        while (size > 0) {
            ssize = splice(STDIN_FILENO, NULL, fd, NULL,
                          size, SPLICE_F_MOVE);
            if (ssize < 0) {
                perror("splice");
                exit(EXIT_FAILURE);
            }
            size -= ssize;
        }
    }

    close(fd);
    exit(EXIT_SUCCESS);
}
 

関連項目

splice(2), vmsplice(2), pipe(7)


 

Index

名前
ライブラリ
書式
説明
返り値
エラー
標準規格
歴史
注意
例
プログラムのソース
関連項目

This document was created by man2html, using the manual pages.