1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
21 ***********************************************************************/
24 /* A discipline to tee the output to a stream to another stream.
25 ** This is similar to what the "tee" program does. As implemented
26 ** this discipline only works with file streams.
28 ** Written by Kiem-Phong Vo, kpv@research.att.com, 03/18/1998.
31 /* the discipline structure for tee-ing */
33 { Sfdisc_t disc
; /* the sfio discipline structure */
34 Sfio_t
* tee
; /* the stream to tee to */
35 int status
; /* if tee stream is still ok */
38 /* write to the teed stream. */
40 static ssize_t
teewrite(Sfio_t
* f
, const Void_t
* buf
, size_t size
, Sfdisc_t
* disc
)
42 static ssize_t
teewrite(f
,buf
,size
,disc
)
43 Sfio_t
* f
; /* the stream being written to */
44 Void_t
* buf
; /* the buffer of data being output */
45 size_t size
; /* the data size */
46 Sfdisc_t
* disc
; /* the tee discipline */
49 reg Tee_t
* te
= (Tee_t
*)disc
;
51 /* tee data if still ok */
52 if(te
->status
== 0 && sfwrite(te
->tee
,buf
,size
) != (ssize_t
)size
)
55 /* do the actual write */
56 return sfwr(f
,buf
,size
,disc
);
59 /* on close, remove the discipline */
61 static int teeexcept(Sfio_t
* f
, int type
, Void_t
* data
, Sfdisc_t
* disc
)
63 static int teeexcept(f
,type
,data
,disc
)
70 if(type
== SF_FINAL
|| type
== SF_DPOP
)
77 int sfdctee(Sfio_t
* f
, Sfio_t
* tee
)
80 Sfio_t
* f
; /* stream to tee from */
81 Sfio_t
* tee
; /* stream to tee to */
86 if(!(te
= (Tee_t
*)malloc(sizeof(Tee_t
))) )
89 te
->disc
.readf
= NIL(Sfread_f
);
90 te
->disc
.seekf
= NIL(Sfseek_f
);
91 te
->disc
.writef
= teewrite
;
92 te
->disc
.exceptf
= teeexcept
;
96 if(sfdisc(f
,(Sfdisc_t
*)te
) != (Sfdisc_t
*)te
)