/* Word-wrapping and line-truncating streams
- Copyright (C) 1997 Free Software Foundation, Inc.
+ Copyright (C) 1997, 1998 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Written by Miles Bader <miles@gnu.ai.mit.edu>.
#define isblank(ch) ((ch)==' ' || (ch)=='\t')
#endif
+#if defined _LIBC && defined USE_IN_LIBIO
+# include <libio/libioP.h>
+# define __vsnprintf(s, l, f, a) _IO_vsnprintf (s, l, f, a)
+#endif
+
#define INIT_BUF_SIZE 200
#define PRINTF_SIZE_GUESS 150
\f
{
__argp_fmtstream_update (fs);
if (fs->p > fs->buf)
- fwrite (fs->buf, 1, fs->p - fs->buf, fs->stream);
+ fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
free (fs->buf);
free (fs);
}
/* No buffer space for spaces. Must flush. */
size_t i;
for (i = 0; i < pad; i++)
- putc (' ', fs->stream);
+ putc_unlocked (' ', fs->stream);
}
fs->point_col = pad;
}
the end of the buffer. */
nl = fs->p;
}
- else if (fs->point_col + (nl - buf) < fs->rmargin)
+ else if (fs->point_col + (nl - buf) < (ssize_t) fs->rmargin)
{
/* The buffer contains a full line that fits within the maximum
line width. Reset point and scan the next line. */
nextline = p;
}
- if (nextline - (nl + 1) < fs->wmargin)
+ /* Note: There are a bunch of tests below for
+ NEXTLINE == BUF + LEN + 1; this case is where NL happens to fall
+ at the end of the buffer, and NEXTLINE is in fact empty (and so
+ we need not be careful to maintain its contents). */
+
+ if (nextline == buf + len + 1
+ ? fs->end - nl < fs->wmargin + 1
+ : nextline - (nl + 1) < fs->wmargin)
/* The margin needs more blanks than we removed. */
if (fs->end - fs->p > fs->wmargin + 1)
/* Make some space for them. */
/* Output the first line so we can use the space. */
{
if (nl > fs->buf)
- fwrite (fs->buf, 1, nl - fs->buf, fs->stream);
- putc ('\n', fs->stream);
+ fwrite_unlocked (fs->buf, 1, nl - fs->buf, fs->stream);
+ putc_unlocked ('\n', fs->stream);
len += buf - fs->buf;
nl = buf = fs->buf;
}
the next word. */
*nl++ = '\n';
- if (nextline - nl >= fs->wmargin)
+ if (nextline - nl >= fs->wmargin
+ || (nextline == buf + len + 1 && fs->end - nextline >= fs->wmargin))
/* Add blanks up to the wrap margin column. */
for (i = 0; i < fs->wmargin; ++i)
*nl++ = ' ';
else
for (i = 0; i < fs->wmargin; ++i)
- putc (' ', fs->stream);
+ putc_unlocked (' ', fs->stream);
/* Copy the tail of the original buffer into the current buffer
position. */
- if (nl != nextline)
+ if (nl < nextline)
memmove (nl, nextline, buf + len - nextline);
len -= nextline - buf;
int
__argp_fmtstream_ensure (struct argp_fmtstream *fs, size_t amount)
{
- if (fs->end - fs->p < amount)
+ if ((size_t) (fs->end - fs->p) < amount)
{
ssize_t wrote;
/* Flush FS's buffer. */
__argp_fmtstream_update (fs);
- wrote = fwrite (fs->buf, 1, fs->p - fs->buf, fs->stream);
+ wrote = fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
if (wrote == fs->p - fs->buf)
{
fs->p = fs->buf;
return 0;
}
- if (fs->end - fs->buf < amount)
+ if ((size_t) (fs->end - fs->buf) < amount)
/* Gotta grow the buffer. */
{
size_t new_size = fs->end - fs->buf + amount;
ssize_t
__argp_fmtstream_printf (struct argp_fmtstream *fs, const char *fmt, ...)
{
- size_t out;
+ int out;
size_t size_guess = PRINTF_SIZE_GUESS; /* How much space to reserve. */
do