xref: /illumos-gate/usr/src/contrib/ast/src/lib/libast/sfio/sfpurge.c (revision b30d193948be5a7794d7ae3ba0ed9c2f72c88e0f)
1*b30d1939SAndy Fiddaman /***********************************************************************
2*b30d1939SAndy Fiddaman *                                                                      *
3*b30d1939SAndy Fiddaman *               This software is part of the ast package               *
4*b30d1939SAndy Fiddaman *          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5*b30d1939SAndy Fiddaman *                      and is licensed under the                       *
6*b30d1939SAndy Fiddaman *                 Eclipse Public License, Version 1.0                  *
7*b30d1939SAndy Fiddaman *                    by AT&T Intellectual Property                     *
8*b30d1939SAndy Fiddaman *                                                                      *
9*b30d1939SAndy Fiddaman *                A copy of the License is available at                 *
10*b30d1939SAndy Fiddaman *          http://www.eclipse.org/org/documents/epl-v10.html           *
11*b30d1939SAndy Fiddaman *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12*b30d1939SAndy Fiddaman *                                                                      *
13*b30d1939SAndy Fiddaman *              Information and Software Systems Research               *
14*b30d1939SAndy Fiddaman *                            AT&T Research                             *
15*b30d1939SAndy Fiddaman *                           Florham Park NJ                            *
16*b30d1939SAndy Fiddaman *                                                                      *
17*b30d1939SAndy Fiddaman *                 Glenn Fowler <gsf@research.att.com>                  *
18*b30d1939SAndy Fiddaman *                  David Korn <dgk@research.att.com>                   *
19*b30d1939SAndy Fiddaman *                   Phong Vo <kpv@research.att.com>                    *
20*b30d1939SAndy Fiddaman *                                                                      *
21*b30d1939SAndy Fiddaman ***********************************************************************/
22*b30d1939SAndy Fiddaman #include	"sfhdr.h"
23*b30d1939SAndy Fiddaman 
24*b30d1939SAndy Fiddaman /*	Delete all pending data in the buffer
25*b30d1939SAndy Fiddaman **
26*b30d1939SAndy Fiddaman **	Written by Kiem-Phong Vo.
27*b30d1939SAndy Fiddaman */
28*b30d1939SAndy Fiddaman 
29*b30d1939SAndy Fiddaman #if __STD_C
sfpurge(Sfio_t * f)30*b30d1939SAndy Fiddaman int sfpurge(Sfio_t* f)
31*b30d1939SAndy Fiddaman #else
32*b30d1939SAndy Fiddaman int sfpurge(f)
33*b30d1939SAndy Fiddaman Sfio_t*	f;
34*b30d1939SAndy Fiddaman #endif
35*b30d1939SAndy Fiddaman {
36*b30d1939SAndy Fiddaman 	reg int	mode;
37*b30d1939SAndy Fiddaman 	SFMTXDECL(f);
38*b30d1939SAndy Fiddaman 
39*b30d1939SAndy Fiddaman 	SFMTXENTER(f,-1);
40*b30d1939SAndy Fiddaman 
41*b30d1939SAndy Fiddaman 	if((mode = f->mode&SF_RDWR) != (int)f->mode && _sfmode(f,mode|SF_SYNCED,0) < 0)
42*b30d1939SAndy Fiddaman 		SFMTXRETURN(f, -1);
43*b30d1939SAndy Fiddaman 
44*b30d1939SAndy Fiddaman 	if((f->flags&SF_IOCHECK) && f->disc && f->disc->exceptf)
45*b30d1939SAndy Fiddaman 		(void)(*f->disc->exceptf)(f,SF_PURGE,(Void_t*)((int)1),f->disc);
46*b30d1939SAndy Fiddaman 
47*b30d1939SAndy Fiddaman 	if(f->disc == _Sfudisc)
48*b30d1939SAndy Fiddaman 		(void)sfclose((*_Sfstack)(f,NIL(Sfio_t*)));
49*b30d1939SAndy Fiddaman 
50*b30d1939SAndy Fiddaman 	/* cannot purge read string streams */
51*b30d1939SAndy Fiddaman 	if((f->flags&SF_STRING) && (f->mode&SF_READ) )
52*b30d1939SAndy Fiddaman 		goto done;
53*b30d1939SAndy Fiddaman 
54*b30d1939SAndy Fiddaman 	SFLOCK(f,0);
55*b30d1939SAndy Fiddaman 
56*b30d1939SAndy Fiddaman 	/* if memory map must be a read stream, pretend data is gone */
57*b30d1939SAndy Fiddaman #ifdef MAP_TYPE
58*b30d1939SAndy Fiddaman 	if(f->bits&SF_MMAP)
59*b30d1939SAndy Fiddaman 	{	f->here -= f->endb - f->next;
60*b30d1939SAndy Fiddaman 		if(f->data)
61*b30d1939SAndy Fiddaman 		{	SFMUNMAP(f,f->data,f->endb-f->data);
62*b30d1939SAndy Fiddaman 			(void)SFSK(f,f->here,SEEK_SET,f->disc);
63*b30d1939SAndy Fiddaman 		}
64*b30d1939SAndy Fiddaman 		SFOPEN(f,0);
65*b30d1939SAndy Fiddaman 		SFMTXRETURN(f, 0);
66*b30d1939SAndy Fiddaman 	}
67*b30d1939SAndy Fiddaman #endif
68*b30d1939SAndy Fiddaman 
69*b30d1939SAndy Fiddaman 	switch(f->mode&~SF_LOCK)
70*b30d1939SAndy Fiddaman 	{
71*b30d1939SAndy Fiddaman 	default :
72*b30d1939SAndy Fiddaman 		SFOPEN(f,0);
73*b30d1939SAndy Fiddaman 		SFMTXRETURN(f, -1);
74*b30d1939SAndy Fiddaman 	case SF_WRITE :
75*b30d1939SAndy Fiddaman 		f->next = f->data;
76*b30d1939SAndy Fiddaman 		if(!f->proc || !(f->flags&SF_READ) || !(f->mode&SF_WRITE) )
77*b30d1939SAndy Fiddaman 			break;
78*b30d1939SAndy Fiddaman 
79*b30d1939SAndy Fiddaman 		/* 2-way pipe, must clear read buffer */
80*b30d1939SAndy Fiddaman 		(void)_sfmode(f,SF_READ,1);
81*b30d1939SAndy Fiddaman 		/* fall through */
82*b30d1939SAndy Fiddaman 	case SF_READ:
83*b30d1939SAndy Fiddaman 		if(f->extent >= 0 && f->endb > f->next)
84*b30d1939SAndy Fiddaman 		{	f->here -= f->endb-f->next;
85*b30d1939SAndy Fiddaman 			(void)SFSK(f,f->here,SEEK_SET,f->disc);
86*b30d1939SAndy Fiddaman 		}
87*b30d1939SAndy Fiddaman 		f->endb = f->next = f->data;
88*b30d1939SAndy Fiddaman 		break;
89*b30d1939SAndy Fiddaman 	}
90*b30d1939SAndy Fiddaman 
91*b30d1939SAndy Fiddaman 	SFOPEN(f,0);
92*b30d1939SAndy Fiddaman 
93*b30d1939SAndy Fiddaman done:
94*b30d1939SAndy Fiddaman 	if((f->flags&SF_IOCHECK) && f->disc && f->disc->exceptf)
95*b30d1939SAndy Fiddaman 		(void)(*f->disc->exceptf)(f,SF_PURGE,(Void_t*)((int)0),f->disc);
96*b30d1939SAndy Fiddaman 
97*b30d1939SAndy Fiddaman 	SFMTXRETURN(f, 0);
98*b30d1939SAndy Fiddaman }
99