1*4887Schin /*********************************************************************** 2*4887Schin * * 3*4887Schin * This software is part of the ast package * 4*4887Schin * Copyright (c) 1985-2007 AT&T Knowledge Ventures * 5*4887Schin * and is licensed under the * 6*4887Schin * Common Public License, Version 1.0 * 7*4887Schin * by AT&T Knowledge Ventures * 8*4887Schin * * 9*4887Schin * A copy of the License is available at * 10*4887Schin * http://www.opensource.org/licenses/cpl1.0.txt * 11*4887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 12*4887Schin * * 13*4887Schin * Information and Software Systems Research * 14*4887Schin * AT&T Research * 15*4887Schin * Florham Park NJ * 16*4887Schin * * 17*4887Schin * Glenn Fowler <gsf@research.att.com> * 18*4887Schin * David Korn <dgk@research.att.com> * 19*4887Schin * Phong Vo <kpv@research.att.com> * 20*4887Schin * * 21*4887Schin ***********************************************************************/ 22*4887Schin #include "sfdchdr.h" 23*4887Schin 24*4887Schin /* A discipline to tee the output to a stream to another stream. 25*4887Schin ** This is similar to what the "tee" program does. As implemented 26*4887Schin ** this discipline only works with file streams. 27*4887Schin ** 28*4887Schin ** Written by Kiem-Phong Vo, kpv@research.att.com, 03/18/1998. 29*4887Schin */ 30*4887Schin 31*4887Schin /* the discipline structure for tee-ing */ 32*4887Schin typedef struct _tee_s 33*4887Schin { Sfdisc_t disc; /* the sfio discipline structure */ 34*4887Schin Sfio_t* tee; /* the stream to tee to */ 35*4887Schin int status; /* if tee stream is still ok */ 36*4887Schin } Tee_t; 37*4887Schin 38*4887Schin /* write to the teed stream. */ 39*4887Schin #if __STD_C 40*4887Schin static ssize_t teewrite(Sfio_t* f, const Void_t* buf, size_t size, Sfdisc_t* disc) 41*4887Schin #else 42*4887Schin static ssize_t teewrite(f,buf,size,disc) 43*4887Schin Sfio_t* f; /* the stream being written to */ 44*4887Schin Void_t* buf; /* the buffer of data being output */ 45*4887Schin size_t size; /* the data size */ 46*4887Schin Sfdisc_t* disc; /* the tee discipline */ 47*4887Schin #endif 48*4887Schin { 49*4887Schin reg Tee_t* te = (Tee_t*)disc; 50*4887Schin 51*4887Schin /* tee data if still ok */ 52*4887Schin if(te->status == 0 && sfwrite(te->tee,buf,size) != (ssize_t)size) 53*4887Schin te->status = -1; 54*4887Schin 55*4887Schin /* do the actual write */ 56*4887Schin return sfwr(f,buf,size,disc); 57*4887Schin } 58*4887Schin 59*4887Schin /* on close, remove the discipline */ 60*4887Schin #if __STD_C 61*4887Schin static int teeexcept(Sfio_t* f, int type, Void_t* data, Sfdisc_t* disc) 62*4887Schin #else 63*4887Schin static int teeexcept(f,type,data,disc) 64*4887Schin Sfio_t* f; 65*4887Schin int type; 66*4887Schin Void_t* data; 67*4887Schin Sfdisc_t* disc; 68*4887Schin #endif 69*4887Schin { 70*4887Schin if(type == SF_FINAL || type == SF_DPOP) 71*4887Schin free(disc); 72*4887Schin 73*4887Schin return 0; 74*4887Schin } 75*4887Schin 76*4887Schin #if __STD_C 77*4887Schin int sfdctee(Sfio_t* f, Sfio_t* tee) 78*4887Schin #else 79*4887Schin int sfdctee(f, tee) 80*4887Schin Sfio_t* f; /* stream to tee from */ 81*4887Schin Sfio_t* tee; /* stream to tee to */ 82*4887Schin #endif 83*4887Schin { 84*4887Schin reg Tee_t* te; 85*4887Schin 86*4887Schin if(!(te = (Tee_t*)malloc(sizeof(Tee_t))) ) 87*4887Schin return -1; 88*4887Schin 89*4887Schin te->disc.readf = NIL(Sfread_f); 90*4887Schin te->disc.seekf = NIL(Sfseek_f); 91*4887Schin te->disc.writef = teewrite; 92*4887Schin te->disc.exceptf = teeexcept; 93*4887Schin te->tee = tee; 94*4887Schin te->status = 0; 95*4887Schin 96*4887Schin if(sfdisc(f,(Sfdisc_t*)te) != (Sfdisc_t*)te) 97*4887Schin { free(te); 98*4887Schin return -1; 99*4887Schin } 100*4887Schin 101*4887Schin return 0; 102*4887Schin } 103