xref: /titanic_41/usr/src/lib/libast/common/sfio/sfraise.c (revision bd0f52d78d701efcad2c460df61b45677d041c35)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
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
30 static int _sfraiseall(int type, Void_t* data)
31 #else
32 static int _sfraiseall(type, data)
33 int	type;	/* type of event	*/
34 Void_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
57 int sfraise(Sfio_t* f, int type, Void_t* data)
58 #else
59 int sfraise(f, type, data)
60 Sfio_t*	f;	/* stream		*/
61 int	type;	/* type of event	*/
62 Void_t*	data;	/* associated data	*/
63 #endif
64 {
65 	reg Sfdisc_t	*disc, *next, *d;
66 	reg int		local, rv;
67 
68 	if(!f)
69 		return _sfraiseall(type,data);
70 
71 	SFMTXSTART(f, -1);
72 
73 	GETLOCAL(f,local);
74 	if(!SFKILLED(f) &&
75 	   !(local &&
76 	     (type == SF_NEW || type == SF_CLOSING ||
77 	      type == SF_FINAL || type == SF_ATEXIT)) &&
78 	   SFMODE(f,local) != (f->mode&SF_RDWR) && _sfmode(f,0,local) < 0)
79 		SFMTXRETURN(f, -1);
80 	SFLOCK(f,local);
81 
82 	for(disc = f->disc; disc; )
83 	{	next = disc->disc;
84 		if(type == SF_FINAL)
85 			f->disc = next;
86 
87 		if(disc->exceptf)
88 		{	SFOPEN(f,0);
89 			if((rv = (*disc->exceptf)(f,type,data,disc)) != 0 )
90 				SFMTXRETURN(f, rv);
91 			SFLOCK(f,0);
92 		}
93 
94 		if((disc = next) )
95 		{	/* make sure that "next" hasn't been popped */
96 			for(d = f->disc; d; d = d->disc)
97 				if(d == disc)
98 					break;
99 			if(!d)
100 				disc = f->disc;
101 		}
102 	}
103 
104 	SFOPEN(f,local);
105 	SFMTXRETURN(f, 0);
106 }
107