sfflsbuf.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2011 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* 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"
/* Write a buffer out to a file descriptor or
** extending a buffer for a SF_STRING stream.
**
** Written by Kiem-Phong Vo
*/
#if __STD_C
#else
int _sfflsbuf(f,c)
Sfio_t* f; /* write out the buffered content of this stream */
int c; /* if c>=0, c is also written out */
#endif
{
int inpc = c;
SFMTXDECL(f); /* declare a local stream variable for multithreading */
SFMTXENTER(f,-1);
{ /* check stream mode */
SFMTXRETURN(f, -1);
/* current data extent */
{ /* call sfwr() to extend string buffer and process events */
/* !(f->flags&SF_STRING) is required because exception
handlers may turn a string stream to a file stream */
else
SFMTXRETURN(f, -1);
}
}
if(c >= 0)
{ /* write into buffer */
{ *f->next++ = c;
if(c == '\n' &&
{ c = -1;
n += 1;
}
else break;
}
else if(n == 0)
{ /* unbuffered io */
c = -1;
n = 1;
}
}
break;
{ if((n -= w) > 0) /* save unwritten data, then resume */
written += w;
if(c < 0 && (!isall || n == 0))
break;
}
else if(w == 0)
{ if(written > 0) /* some buffer was cleared */
break; /* do normal exit below */
else /* nothing was done, returning failure */
SFMTXRETURN(f, -1);
}
}
else /* w < 0 means SF_EDISC or SF_ESTACK in sfwr() */
{ if(c < 0) /* back to the calling write operation */
break;
else continue; /* try again to write out c */
}
}
if(inpc < 0)
SFMTXRETURN(f,inpc);
}