]> granicus.if.org Git - php/commitdiff
Add vasprintf() so the buffer can be automatically calculated, you need to efree...
authorScott MacVicar <scottmac@php.net>
Fri, 21 Nov 2008 21:49:52 +0000 (21:49 +0000)
committerScott MacVicar <scottmac@php.net>
Fri, 21 Nov 2008 21:49:52 +0000 (21:49 +0000)
main/snprintf.c
main/snprintf.h

index 2744a104a5ee15a5f37a85c0207d2ae8cab002da..ada64ac268bd8b3b00ed4b53bcc20e3b1d9fb96b 100644 (file)
@@ -1315,6 +1315,30 @@ PHPAPI int ap_php_vsnprintf(char *buf, size_t len, const char *format, va_list a
 }
 /* }}} */
 
+PHPAPI int ap_php_vasprintf(char **buf, const char *format, va_list ap) /* {{{ */
+{
+       va_list ap2;
+       int cc;
+
+       va_copy(ap2, ap);
+       cc = ap_php_vsnprintf(NULL, 0, format, ap2);
+       va_end(ap2);
+
+       *buf = NULL;
+
+       if (cc >= 0) {
+               if ((*buf = emalloc(++cc)) != NULL) {
+                       if ((cc = ap_php_vsnprintf(*buf, cc, format, ap)) < 0) {
+                               efree(*buf);
+                               *buf = NULL;
+                       }
+               }
+       }
+
+       return cc;
+}
+/* }}} */
+
 /*
  * Local variables:
  * tab-width: 4
index 3ba50259ffadd02c972dee029de51c3c6c5d8fc5..c55d5cb35a15a8642ad686bbe9af005fa17df620 100644 (file)
@@ -82,6 +82,7 @@ PHPAPI int ap_php_slprintf(char *buf, size_t len, const char *format,...);
 PHPAPI int ap_php_vslprintf(char *buf, size_t len, const char *format, va_list ap);
 PHPAPI int ap_php_snprintf(char *, size_t, const char *, ...);
 PHPAPI int ap_php_vsnprintf(char *, size_t, const char *, va_list ap);
+PHPAPI int ap_php_vasprintf(char **buf, const char *format, va_list ap);
 PHPAPI int php_sprintf (char* s, const char* format, ...) PHP_ATTRIBUTE_FORMAT(printf, 2, 3);
 PHPAPI char * php_gcvt(double value, int ndigit, char dec_point, char exponent, char *buf);
 PHPAPI char * php_conv_fp(register char format, register double num,
@@ -109,6 +110,11 @@ END_EXTERN_C()
 #endif
 #define vsnprintf ap_php_vsnprintf
 
+#ifdef vasprintf
+#undef vasprintf
+#endif
+#define vasprintf ap_php_vasprintf
+
 #ifdef sprintf
 #undef sprintf
 #endif