xref: /titanic_50/usr/src/lib/libast/common/sfio/sfpurge.c (revision 80b80bf0416a7eb4be4215b2e192cafd03ca80b7)
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 /*	Delete all pending data in the buffer
25 **
26 **	Written by Kiem-Phong Vo.
27 */
28 
29 #if __STD_C
30 int sfpurge(reg Sfio_t* f)
31 #else
32 int sfpurge(f)
33 reg Sfio_t*	f;
34 #endif
35 {
36 	reg int	mode;
37 
38 	SFMTXSTART(f,-1);
39 
40 	if((mode = f->mode&SF_RDWR) != (int)f->mode && _sfmode(f,mode|SF_SYNCED,0) < 0)
41 		SFMTXRETURN(f, -1);
42 
43 	if((f->flags&SF_IOCHECK) && f->disc && f->disc->exceptf)
44 		(void)(*f->disc->exceptf)(f,SF_PURGE,(Void_t*)((int)1),f->disc);
45 
46 	if(f->disc == _Sfudisc)
47 		(void)sfclose((*_Sfstack)(f,NIL(Sfio_t*)));
48 
49 	/* cannot purge read string streams */
50 	if((f->flags&SF_STRING) && (f->mode&SF_READ) )
51 		goto done;
52 
53 	SFLOCK(f,0);
54 
55 	/* if memory map must be a read stream, pretend data is gone */
56 #ifdef MAP_TYPE
57 	if(f->bits&SF_MMAP)
58 	{	f->here -= f->endb - f->next;
59 		if(f->data)
60 		{	SFMUNMAP(f,f->data,f->endb-f->data);
61 			SFSK(f,f->here,SEEK_SET,f->disc);
62 		}
63 		SFOPEN(f,0);
64 		SFMTXRETURN(f, 0);
65 	}
66 #endif
67 
68 	switch(f->mode&~SF_LOCK)
69 	{
70 	default :
71 		SFOPEN(f,0);
72 		SFMTXRETURN(f, -1);
73 	case SF_WRITE :
74 		f->next = f->data;
75 		if(!f->proc || !(f->flags&SF_READ) || !(f->mode&SF_WRITE) )
76 			break;
77 
78 		/* 2-way pipe, must clear read buffer */
79 		(void)_sfmode(f,SF_READ,1);
80 		/* fall through */
81 	case SF_READ:
82 		if(f->extent >= 0 && f->endb > f->next)
83 		{	f->here -= f->endb-f->next;
84 			SFSK(f,f->here,SEEK_SET,f->disc);
85 		}
86 		f->endb = f->next = f->data;
87 		break;
88 	}
89 
90 	SFOPEN(f,0);
91 
92 done:
93 	if((f->flags&SF_IOCHECK) && f->disc && f->disc->exceptf)
94 		(void)(*f->disc->exceptf)(f,SF_PURGE,(Void_t*)((int)0),f->disc);
95 
96 	SFMTXRETURN(f, 0);
97 }
98