summaryrefslogtreecommitdiff
path: root/usr/src/lib/libast/common/sfio/sfpurge.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr/src/lib/libast/common/sfio/sfpurge.c')
-rw-r--r--usr/src/lib/libast/common/sfio/sfpurge.c97
1 files changed, 97 insertions, 0 deletions
diff --git a/usr/src/lib/libast/common/sfio/sfpurge.c b/usr/src/lib/libast/common/sfio/sfpurge.c
new file mode 100644
index 0000000000..4c1716bcee
--- /dev/null
+++ b/usr/src/lib/libast/common/sfio/sfpurge.c
@@ -0,0 +1,97 @@
+/***********************************************************************
+* *
+* This software is part of the ast package *
+* Copyright (c) 1985-2007 AT&T Knowledge Ventures *
+* and is licensed under the *
+* Common Public License, Version 1.0 *
+* by AT&T Knowledge Ventures *
+* *
+* A copy of the License is available at *
+* http://www.opensource.org/licenses/cpl1.0.txt *
+* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
+* *
+* Information and Software Systems Research *
+* AT&T Research *
+* Florham Park NJ *
+* *
+* Glenn Fowler <gsf@research.att.com> *
+* David Korn <dgk@research.att.com> *
+* Phong Vo <kpv@research.att.com> *
+* *
+***********************************************************************/
+#include "sfhdr.h"
+
+/* Delete all pending data in the buffer
+**
+** Written by Kiem-Phong Vo.
+*/
+
+#if __STD_C
+int sfpurge(reg Sfio_t* f)
+#else
+int sfpurge(f)
+reg Sfio_t* f;
+#endif
+{
+ reg int mode;
+
+ SFMTXSTART(f,-1);
+
+ if((mode = f->mode&SF_RDWR) != (int)f->mode && _sfmode(f,mode|SF_SYNCED,0) < 0)
+ SFMTXRETURN(f, -1);
+
+ if((f->flags&SF_IOCHECK) && f->disc && f->disc->exceptf)
+ (void)(*f->disc->exceptf)(f,SF_PURGE,(Void_t*)((int)1),f->disc);
+
+ if(f->disc == _Sfudisc)
+ (void)sfclose((*_Sfstack)(f,NIL(Sfio_t*)));
+
+ /* cannot purge read string streams */
+ if((f->flags&SF_STRING) && (f->mode&SF_READ) )
+ goto done;
+
+ SFLOCK(f,0);
+
+ /* if memory map must be a read stream, pretend data is gone */
+#ifdef MAP_TYPE
+ if(f->bits&SF_MMAP)
+ { f->here -= f->endb - f->next;
+ if(f->data)
+ { SFMUNMAP(f,f->data,f->endb-f->data);
+ SFSK(f,f->here,SEEK_SET,f->disc);
+ }
+ SFOPEN(f,0);
+ SFMTXRETURN(f, 0);
+ }
+#endif
+
+ switch(f->mode&~SF_LOCK)
+ {
+ default :
+ SFOPEN(f,0);
+ SFMTXRETURN(f, -1);
+ case SF_WRITE :
+ f->next = f->data;
+ if(!f->proc || !(f->flags&SF_READ) || !(f->mode&SF_WRITE) )
+ break;
+
+ /* 2-way pipe, must clear read buffer */
+ (void)_sfmode(f,SF_READ,1);
+ /* fall through */
+ case SF_READ:
+ if(f->extent >= 0 && f->endb > f->next)
+ { f->here -= f->endb-f->next;
+ SFSK(f,f->here,SEEK_SET,f->disc);
+ }
+ f->endb = f->next = f->data;
+ break;
+ }
+
+ SFOPEN(f,0);
+
+done:
+ if((f->flags&SF_IOCHECK) && f->disc && f->disc->exceptf)
+ (void)(*f->disc->exceptf)(f,SF_PURGE,(Void_t*)((int)0),f->disc);
+
+ SFMTXRETURN(f, 0);
+}