/***********************************************************************1* *2* This software is part of the ast package *3* Copyright (c) 1985-2011 AT&T Intellectual Property *4* and is licensed under the *5* Eclipse Public License, Version 1.0 *6* by AT&T Intellectual Property *7* *8* A copy of the License is available at *9* http://www.eclipse.org/org/documents/epl-v10.html *10* (with md5 checksum b35adb5213ca9657e911e9befb180842) *11* *12* Information and Software Systems Research *13* AT&T Research *14* Florham Park NJ *15* *16* Glenn Fowler <[email protected]> *17* David Korn <[email protected]> *18* Phong Vo <[email protected]> *19* *20***********************************************************************/21#include "sfdchdr.h"2223/* A discipline to tee the output to a stream to another stream.24** This is similar to what the "tee" program does. As implemented25** this discipline only works with file streams.26**27** Written by Kiem-Phong Vo, [email protected], 03/18/1998.28*/2930/* the discipline structure for tee-ing */31typedef struct _tee_s32{ Sfdisc_t disc; /* the sfio discipline structure */33Sfio_t* tee; /* the stream to tee to */34int status; /* if tee stream is still ok */35} Tee_t;3637/* write to the teed stream. */38#if __STD_C39static ssize_t teewrite(Sfio_t* f, const Void_t* buf, size_t size, Sfdisc_t* disc)40#else41static ssize_t teewrite(f,buf,size,disc)42Sfio_t* f; /* the stream being written to */43Void_t* buf; /* the buffer of data being output */44size_t size; /* the data size */45Sfdisc_t* disc; /* the tee discipline */46#endif47{48reg Tee_t* te = (Tee_t*)disc;4950/* tee data if still ok */51if(te->status == 0 && sfwrite(te->tee,buf,size) != (ssize_t)size)52te->status = -1;5354/* do the actual write */55return sfwr(f,buf,size,disc);56}5758/* on close, remove the discipline */59#if __STD_C60static int teeexcept(Sfio_t* f, int type, Void_t* data, Sfdisc_t* disc)61#else62static int teeexcept(f,type,data,disc)63Sfio_t* f;64int type;65Void_t* data;66Sfdisc_t* disc;67#endif68{69if(type == SF_FINAL || type == SF_DPOP)70free(disc);7172return 0;73}7475#if __STD_C76int sfdctee(Sfio_t* f, Sfio_t* tee)77#else78int sfdctee(f, tee)79Sfio_t* f; /* stream to tee from */80Sfio_t* tee; /* stream to tee to */81#endif82{83reg Tee_t* te;8485if(!(te = (Tee_t*)malloc(sizeof(Tee_t))) )86return -1;8788te->disc.readf = NIL(Sfread_f);89te->disc.seekf = NIL(Sfseek_f);90te->disc.writef = teewrite;91te->disc.exceptf = teeexcept;92te->tee = tee;93te->status = 0;9495if(sfdisc(f,(Sfdisc_t*)te) != (Sfdisc_t*)te)96{ free(te);97return -1;98}99100return 0;101}102103104