Fix autoreconf invocation.
[gnulib.git] / lib / snprintf.c
index 667ed94..5870f8d 100644 (file)
@@ -14,7 +14,7 @@
 
    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation,
-   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
@@ -42,19 +42,19 @@ snprintf (char *str, size_t size, const char *format, ...)
   va_list args;
 
   va_start (args, format);
-  output = vasnprintf (NULL, &len, format, args);
+  len = size;
+  output = vasnprintf (str, &len, format, args);
   va_end (args);
 
   if (!output)
     return -1;
 
-  if (str && size > 0)
-    {
-      memcpy (str, output, MIN (len + 1, size));
+  if (str != NULL)
+    if (len > size - 1) /* equivalent to: (size > 0 && len >= size) */
       str[size - 1] = '\0';
-    }
 
-  free (output);
+  if (output != str)
+    free (output);
 
   return len;
 }