Update.
[platform/upstream/glibc.git] / argp / argp-fmtstream.c
index c7203de..05e9e21 100644 (file)
@@ -1,5 +1,5 @@
 /* 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
@@ -89,7 +94,7 @@ __argp_fmtstream_free (argp_fmtstream_t fs)
 {
   __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);
 }
@@ -129,7 +134,7 @@ __argp_fmtstream_update (argp_fmtstream_t 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;
        }
@@ -157,7 +162,7 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
               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.  */
@@ -240,7 +245,14 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
              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.  */
@@ -255,8 +267,8 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
              /* 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;
              }
@@ -265,17 +277,18 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
               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;
 
@@ -301,14 +314,14 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
 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;
@@ -322,7 +335,7 @@ __argp_fmtstream_ensure (struct argp_fmtstream *fs, size_t amount)
          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;
@@ -346,7 +359,7 @@ __argp_fmtstream_ensure (struct argp_fmtstream *fs, size_t 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