about summary refs log tree commit diff
path: root/argp/argp-fmtstream.c
diff options
context:
space:
mode:
Diffstat (limited to 'argp/argp-fmtstream.c')
-rw-r--r--argp/argp-fmtstream.c60
1 files changed, 49 insertions, 11 deletions
diff --git a/argp/argp-fmtstream.c b/argp/argp-fmtstream.c
index 9c029c265a..fca89a382d 100644
--- a/argp/argp-fmtstream.c
+++ b/argp/argp-fmtstream.c
@@ -1,5 +1,5 @@
 /* Word-wrapping and line-truncating streams
-   Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
+   Copyright (C) 1997, 1998, 1999, 2001 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Written by Miles Bader <miles@gnu.ai.mit.edu>.
 
@@ -41,6 +41,7 @@
 #endif
 
 #if defined _LIBC && defined USE_IN_LIBIO
+# include <wchar.h>
 # include <libio/libioP.h>
 # define __vsnprintf(s, l, f, a) _IO_vsnprintf (s, l, f, a)
 #endif
@@ -58,8 +59,10 @@ argp_fmtstream_t
 __argp_make_fmtstream (FILE *stream,
 		       size_t lmargin, size_t rmargin, ssize_t wmargin)
 {
-  argp_fmtstream_t fs = malloc (sizeof (struct argp_fmtstream));
-  if (fs)
+  argp_fmtstream_t fs;
+
+  fs = (struct argp_fmtstream *) malloc (sizeof (struct argp_fmtstream));
+  if (fs != NULL)
     {
       fs->stream = stream;
 
@@ -69,7 +72,7 @@ __argp_make_fmtstream (FILE *stream,
       fs->point_col = 0;
       fs->point_offs = 0;
 
-      fs->buf = malloc (INIT_BUF_SIZE);
+      fs->buf = (char *) malloc (INIT_BUF_SIZE);
       if (! fs->buf)
 	{
 	  free (fs);
@@ -94,7 +97,14 @@ __argp_fmtstream_free (argp_fmtstream_t fs)
 {
   __argp_fmtstream_update (fs);
   if (fs->p > fs->buf)
-    fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
+    {
+#ifdef USE_IN_LIBIO
+      if (_IO_fwide (fs->stream, 0) > 0)
+	fwprintf (fs->stream, L"%.*s", (int) (fs->p - fs->buf), fs->buf);
+      else
+#endif
+	fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
+    }
   free (fs->buf);
   free (fs);
 }
@@ -134,7 +144,14 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
 	      /* No buffer space for spaces.  Must flush.  */
 	      size_t i;
 	      for (i = 0; i < pad; i++)
-		putc_unlocked (' ', fs->stream);
+		{
+#ifdef USE_IN_LIBIO
+		  if (_IO_fwide (fs->stream, 0) > 0)
+		    putwc_unlocked (L' ', fs->stream);
+		  else
+#endif
+		    putc_unlocked (' ', fs->stream);
+		}
 	    }
 	  fs->point_col = pad;
 	}
@@ -267,9 +284,17 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
 	      else
 		/* Output the first line so we can use the space.  */
 		{
-		  if (nl > fs->buf)
-		    fwrite_unlocked (fs->buf, 1, nl - fs->buf, fs->stream);
-		  putc_unlocked ('\n', fs->stream);
+#ifdef USE_IN_LIBIO
+		  if (_IO_fwide (fs->stream, 0) > 0)
+		    fwprintf (fs->stream, L"%.*s\n",
+			      (int) (nl - fs->buf), fs->buf);
+		  else
+#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;
 		}
@@ -286,7 +311,12 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
 	      *nl++ = ' ';
 	  else
 	    for (i = 0; i < fs->wmargin; ++i)
-	      putc_unlocked (' ', fs->stream);
+#ifdef USE_IN_LIBIO
+	      if (_IO_fwide (fs->stream, 0) > 0)
+		putwc_unlocked (L' ', fs->stream);
+	      else
+#endif
+		putc_unlocked (' ', fs->stream);
 
 	  /* Copy the tail of the original buffer into the current buffer
 	     position.  */
@@ -323,7 +353,15 @@ __argp_fmtstream_ensure (struct argp_fmtstream *fs, size_t amount)
       /* Flush FS's buffer.  */
       __argp_fmtstream_update (fs);
 
-      wrote = fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
+#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
+#endif
+	wrote = fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
       if (wrote == fs->p - fs->buf)
 	{
 	  fs->p = fs->buf;