* buffer.c (Fgenerate_new_buffer_name): Fix type mismatch.
[bpt/emacs.git] / src / doprnt.c
index b7c1e4e..07bbcff 100644 (file)
@@ -1,7 +1,7 @@
 /* Output like sprintf to a buffer of specified size.
    Also takes args differently: pass one pointer to the end
    of the format string in addition to the format string itself.
-   Copyright (C) 1985, 2001-2011  Free Software Foundation, Inc.
+   Copyright (C) 1985, 2001-2012  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -26,7 +26,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
      of the (`int') argument, suitable for display in an Emacs buffer.
 
    . For %s and %c, when field width is specified (e.g., %25s), it accounts for
-     the diplay width of each character, according to char-width-table.  That
+     the display width of each character, according to char-width-table.  That
      is, it does not assume that each character takes one column on display.
 
    . If the size of the buffer is not enough to produce the formatted string in
@@ -275,32 +275,32 @@ doprnt (char *buffer, ptrdiff_t bufsize, const char *format,
                case no_modifier:
                  {
                    int v = va_arg (ap, int);
-                   sprintf (sprintf_buffer, fmtcpy, v);
+                   tem = sprintf (sprintf_buffer, fmtcpy, v);
                  }
                  break;
                case long_modifier:
                  {
                    long v = va_arg (ap, long);
-                   sprintf (sprintf_buffer, fmtcpy, v);
+                   tem = sprintf (sprintf_buffer, fmtcpy, v);
                  }
                  break;
                case pD_modifier:
                signed_pD_modifier:
                  {
                    ptrdiff_t v = va_arg (ap, ptrdiff_t);
-                   sprintf (sprintf_buffer, fmtcpy, v);
+                   tem = sprintf (sprintf_buffer, fmtcpy, v);
                  }
                  break;
                case pI_modifier:
                  {
                    EMACS_INT v = va_arg (ap, EMACS_INT);
-                   sprintf (sprintf_buffer, fmtcpy, v);
+                   tem = sprintf (sprintf_buffer, fmtcpy, v);
                  }
                  break;
                case pM_modifier:
                  {
                    intmax_t v = va_arg (ap, intmax_t);
-                   sprintf (sprintf_buffer, fmtcpy, v);
+                   tem = sprintf (sprintf_buffer, fmtcpy, v);
                  }
                  break;
                }
@@ -315,13 +315,13 @@ doprnt (char *buffer, ptrdiff_t bufsize, const char *format,
                case no_modifier:
                  {
                    unsigned v = va_arg (ap, unsigned);
-                   sprintf (sprintf_buffer, fmtcpy, v);
+                   tem = sprintf (sprintf_buffer, fmtcpy, v);
                  }
                  break;
                case long_modifier:
                  {
                    unsigned long v = va_arg (ap, unsigned long);
-                   sprintf (sprintf_buffer, fmtcpy, v);
+                   tem = sprintf (sprintf_buffer, fmtcpy, v);
                  }
                  break;
                case pD_modifier:
@@ -329,13 +329,13 @@ doprnt (char *buffer, ptrdiff_t bufsize, const char *format,
                case pI_modifier:
                  {
                    EMACS_UINT v = va_arg (ap, EMACS_UINT);
-                   sprintf (sprintf_buffer, fmtcpy, v);
+                   tem = sprintf (sprintf_buffer, fmtcpy, v);
                  }
                  break;
                case pM_modifier:
                  {
                    uintmax_t v = va_arg (ap, uintmax_t);
-                   sprintf (sprintf_buffer, fmtcpy, v);
+                   tem = sprintf (sprintf_buffer, fmtcpy, v);
                  }
                  break;
                }
@@ -348,7 +348,7 @@ doprnt (char *buffer, ptrdiff_t bufsize, const char *format,
            case 'g':
              {
                double d = va_arg (ap, double);
-               sprintf (sprintf_buffer, fmtcpy, d);
+               tem = sprintf (sprintf_buffer, fmtcpy, d);
                /* Now copy into final output, truncating as necessary.  */
                string = sprintf_buffer;
                goto doit;
@@ -369,7 +369,6 @@ doprnt (char *buffer, ptrdiff_t bufsize, const char *format,
              /* Copy string into final output, truncating if no room.  */
            doit:
              /* Coming here means STRING contains ASCII only.  */
-             tem = strlen (string);
              if (STRING_BYTES_BOUND < tem)
                error ("Format width or precision too large");
              width = tem;
@@ -392,15 +391,19 @@ doprnt (char *buffer, ptrdiff_t bufsize, const char *format,
                {
                  /* Truncate the string at character boundary.  */
                  tem = bufsize;
-                 while (!CHAR_HEAD_P (string[tem - 1])) tem--;
-                 /* If the multibyte sequence of this character is
-                    too long for the space we have left in the
-                    buffer, truncate before it.  */
-                 if (tem > 0
-                     && BYTES_BY_CHAR_HEAD (string[tem - 1]) > bufsize)
-                   tem--;
-                 if (tem > 0)
-                   memcpy (bufptr, string, tem);
+                 do
+                   {
+                     tem--;
+                     if (CHAR_HEAD_P (string[tem]))
+                       {
+                         if (BYTES_BY_CHAR_HEAD (string[tem]) <= bufsize - tem)
+                           tem = bufsize;
+                         break;
+                       }
+                   }
+                 while (tem != 0);
+
+                 memcpy (bufptr, string, tem);
                  bufptr[tem] = 0;
                  /* Trigger exit from the loop, but make sure we
                     return to the caller a value which will indicate
@@ -409,8 +412,7 @@ doprnt (char *buffer, ptrdiff_t bufsize, const char *format,
                  bufsize = 0;
                  continue;
                }
-             else
-               memcpy (bufptr, string, tem);
+             memcpy (bufptr, string, tem);
              bufptr += tem;
              bufsize -= tem;
              if (minlen < 0)