/*
* Helper function for `fprintf to unbuffered unix file': creates a
* temporary buffer. We only work on write-only files; this avoids
* worries about ungetc buffers and so forth.
*/
static int
_DEFUN(__sbprintf, (rptr, fp, fmt, ap),
struct _reent *rptr _AND
register FILE *fp _AND
_CONST char *fmt _AND
va_list ap)
{
int ret;
FILE fake;
unsigned char *buf;
/* copy the important variables */
fake._flags = fp->_flags & ~__SNBF;
fake._flags2 = fp->_flags2;
fake._file = fp->_file;
fake._cookie = fp->_cookie;
fake._write = fp->_write;
buf = _malloc_r(rptr, BUFSIZ);
if (buf == NULL)
return EOF;
/* set up the buffer */
fake._bf._base = fake._p = buf;
fake._bf._size = fake._w = BUFSIZ;
fake._lbfsize = 0; /* not actually used, but Just In Case */
#ifndef __SINGLE_THREAD__
__lock_init_recursive (fake._lock);
#endif
/* do the work, then copy any error status */
ret = _VFPRINTF_R (rptr, &fake, fmt, ap);
if (ret >= 0 && _fflush_r (rptr, &fake))
ret = EOF;
if (fake._flags & __SERR)
fp->_flags |= __SERR;
_free_r(rptr, buf);
#ifndef __SINGLE_THREAD__
__lock_close_recursive (fake._lock);
#endif
return (ret);
}