1/***********************************************************************
2*                                                                      *
3*               This software is part of the ast package               *
4*          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5*                      and is licensed under the                       *
6*                  Common Public License, Version 1.0                  *
7*                    by AT&T Intellectual Property                     *
8*                                                                      *
9*                A copy of the License is available at                 *
10*            http://www.opensource.org/licenses/cpl1.0.txt             *
11*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*                                                                      *
13*              Information and Software Systems Research               *
14*                            AT&T Research                             *
15*                           Florham Park NJ                            *
16*                                                                      *
17*                 Glenn Fowler <gsf@research.att.com>                  *
18*                  David Korn <dgk@research.att.com>                   *
19*                   Phong Vo <kpv@research.att.com>                    *
20*                                                                      *
21***********************************************************************/
22#include	"sfhdr.h"
23
24/*	Invoke event handlers for a stream
25**
26**	Written by Kiem-Phong Vo.
27*/
28
29#if __STD_C
30static int _sfraiseall(int type, Void_t* data)
31#else
32static int _sfraiseall(type, data)
33int	type;	/* type of event	*/
34Void_t*	data;	/* associated data	*/
35#endif
36{
37	Sfio_t		*f;
38	Sfpool_t	*p, *next;
39	int		n, rv;
40
41	rv = 0;
42	for(p = &_Sfpool; p; p = next)
43	{
44		for(next = p->next; next; next = next->next)
45			if(next->n_sf > 0)
46				break;
47		for(n = 0; n < p->n_sf; ++n)
48		{	f = p->sf[n];
49			if(sfraise(f, type, data) < 0)
50				rv -= 1;
51		}
52	}
53	return rv;
54}
55
56#if __STD_C
57int sfraise(Sfio_t* f, int type, Void_t* data)
58#else
59int sfraise(f, type, data)
60Sfio_t*	f;	/* stream		*/
61int	type;	/* type of event	*/
62Void_t*	data;	/* associated data	*/
63#endif
64{
65	reg Sfdisc_t	*disc, *next, *d;
66	reg int		local, rv;
67	SFMTXDECL(f);
68
69	if(!f)
70		return _sfraiseall(type,data);
71
72	SFMTXENTER(f, -1);
73
74	GETLOCAL(f,local);
75	if(!SFKILLED(f) &&
76	   !(local &&
77	     (type == SF_NEW || type == SF_CLOSING ||
78	      type == SF_FINAL || type == SF_ATEXIT)) &&
79	   SFMODE(f,local) != (f->mode&SF_RDWR) && _sfmode(f,0,local) < 0)
80		SFMTXRETURN(f, -1);
81	SFLOCK(f,local);
82
83	for(disc = f->disc; disc; )
84	{	next = disc->disc;
85		if(type == SF_FINAL)
86			f->disc = next;
87
88		if(disc->exceptf)
89		{	SFOPEN(f,0);
90			if((rv = (*disc->exceptf)(f,type,data,disc)) != 0 )
91				SFMTXRETURN(f, rv);
92			SFLOCK(f,0);
93		}
94
95		if((disc = next) )
96		{	/* make sure that "next" hasn't been popped */
97			for(d = f->disc; d; d = d->disc)
98				if(d == disc)
99					break;
100			if(!d)
101				disc = f->disc;
102		}
103	}
104
105	SFOPEN(f,local);
106	SFMTXRETURN(f, 0);
107}
108