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