/* Word-wrapping and line-truncating streams
- Copyright (C) 1997, 1998, 1999, 2001 Free Software Foundation, Inc.
+ Copyright (C) 1997-2019 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Written by Miles Bader <miles@gnu.ai.mit.edu>.
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
- License along with the GNU C Library; if not, write to the Free
- Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
- 02111-1307 USA. */
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
/* This package emulates glibc `line_wrap_stream' semantics for systems that
don't have that. */
#ifdef HAVE_CONFIG_H
-#include <config.h>
+# include <config.h>
#endif
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
-#include "argp-fmtstream.h"
+#include <argp-fmtstream.h>
#include "argp-namefrob.h"
#ifndef ARGP_FMTSTREAM_USE_LINEWRAP
#define isblank(ch) ((ch)==' ' || (ch)=='\t')
#endif
-#if defined _LIBC && defined USE_IN_LIBIO
+#ifdef _LIBC
# include <wchar.h>
# include <libio/libioP.h>
-# define __vsnprintf(s, l, f, a) _IO_vsnprintf (s, l, f, a)
#endif
#define INIT_BUF_SIZE 200
return fs;
}
+#if 0
+/* Not exported. */
#ifdef weak_alias
weak_alias (__argp_make_fmtstream, argp_make_fmtstream)
#endif
+#endif
/* Flush FS to its stream, and free it (but don't close the stream). */
void
__argp_fmtstream_update (fs);
if (fs->p > fs->buf)
{
-#ifdef USE_IN_LIBIO
- if (_IO_fwide (fs->stream, 0) > 0)
- fwprintf (fs->stream, L"%.*s", (int) (fs->p - fs->buf), fs->buf);
- else
+#ifdef _LIBC
+ __fxprintf (fs->stream, "%.*s", (int) (fs->p - fs->buf), fs->buf);
+#else
+ fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
#endif
- fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
}
free (fs->buf);
free (fs);
}
+#if 0
+/* Not exported. */
#ifdef weak_alias
weak_alias (__argp_fmtstream_free, argp_fmtstream_free)
#endif
+#endif
\f
/* Process FS's buffer so that line wrapping is done from POINT_OFFS to the
end of its buffer. This code is mostly from glibc stdio/linewrap.c. */
size_t i;
for (i = 0; i < pad; i++)
{
-#ifdef USE_IN_LIBIO
+#ifdef _LIBC
if (_IO_fwide (fs->stream, 0) > 0)
putwc_unlocked (L' ', fs->stream);
else
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)
+ if ((nextline == buf + len + 1
+ ? fs->end - nl < fs->wmargin + 1
+ : nextline - (nl + 1) < fs->wmargin)
+ && fs->p > nextline)
{
/* The margin needs more blanks than we removed. */
if (fs->end - fs->p > fs->wmargin + 1)
else
/* Output the first line so we can use the space. */
{
-#ifdef USE_IN_LIBIO
- if (_IO_fwide (fs->stream, 0) > 0)
- fwprintf (fs->stream, L"%.*s\n",
+#ifdef _LIBC
+ __fxprintf (fs->stream, "%.*s\n",
(int) (nl - fs->buf), fs->buf);
- else
+#else
+ if (nl > fs->buf)
+ fwrite_unlocked (fs->buf, 1, nl - fs->buf, fs->stream);
+ putc_unlocked ('\n', fs->stream);
#endif
- {
- if (nl > fs->buf)
- fwrite_unlocked (fs->buf, 1, nl - fs->buf, fs->stream);
- putc_unlocked ('\n', fs->stream);
- }
+
len += buf - fs->buf;
nl = buf = fs->buf;
}
*nl++ = ' ';
else
for (i = 0; i < fs->wmargin; ++i)
-#ifdef USE_IN_LIBIO
+#ifdef _LIBC
if (_IO_fwide (fs->stream, 0) > 0)
putwc_unlocked (L' ', fs->stream);
else
/* Flush FS's buffer. */
__argp_fmtstream_update (fs);
-#ifdef USE_IN_LIBIO
- if (_IO_fwide (fs->stream, 0) > 0)
- {
- __fwprintf (fs->stream, L"%.*s", (int) (fs->p - fs->buf), fs->buf);
- wrote = fs->p - fs->buf;
- }
- else
+#ifdef _LIBC
+ __fxprintf (fs->stream, "%.*s", (int) (fs->p - fs->buf), fs->buf);
+ wrote = fs->p - fs->buf;
+#else
+ wrote = fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
#endif
- wrote = fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
if (wrote == fs->p - fs->buf)
{
fs->p = fs->buf;
if ((size_t) (fs->end - fs->buf) < amount)
/* Gotta grow the buffer. */
{
- size_t new_size = fs->end - fs->buf + amount;
- char *new_buf = realloc (fs->buf, new_size);
+ size_t old_size = fs->end - fs->buf;
+ size_t new_size = old_size + amount;
+ char *new_buf;
- if (! new_buf)
+ if (new_size < old_size || ! (new_buf = realloc (fs->buf, new_size)))
{
__set_errno (ENOMEM);
return 0;
va_start (args, fmt);
avail = fs->end - fs->p;
- out = __vsnprintf (fs->p, avail, fmt, args);
+ out = __vsnprintf_internal (fs->p, avail, fmt, args, 0);
va_end (args);
- if (out >= avail)
+ if ((size_t) out >= avail)
size_guess = out + 1;
}
- while (out >= avail);
+ while ((size_t) out >= avail);
fs->p += out;
return out;
}
+#if 0
+/* Not exported. */
#ifdef weak_alias
weak_alias (__argp_fmtstream_printf, argp_fmtstream_printf)
#endif
+#endif
#endif /* !ARGP_FMTSTREAM_USE_LINEWRAP */