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