]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - argp/argp-fmtstream.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / argp / argp-fmtstream.c
index fca89a382da1fa7b41b95f22e68ad3130946533c..1fff85cba059e5b4315d5f9dfcaf814c84184b8a 100644 (file)
@@ -1,5 +1,5 @@
 /* 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>
@@ -31,7 +30,7 @@
 #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
@@ -87,9 +85,12 @@ __argp_make_fmtstream (FILE *stream,
 
   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
@@ -98,19 +99,21 @@ __argp_fmtstream_free (argp_fmtstream_t fs)
   __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.  */
@@ -145,7 +148,7 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
              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
@@ -267,9 +270,10 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
             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)
@@ -284,17 +288,15 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
              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;
                }
@@ -311,7 +313,7 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
              *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
@@ -353,15 +355,12 @@ __argp_fmtstream_ensure (struct argp_fmtstream *fs, size_t amount)
       /* 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;
@@ -378,10 +377,11 @@ __argp_fmtstream_ensure (struct argp_fmtstream *fs, size_t amount)
       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;
@@ -412,19 +412,22 @@ __argp_fmtstream_printf (struct argp_fmtstream *fs, const char *fmt, ...)
 
       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 */