cvs: php-src(PHP_5_2) /main snprintf.c snprintf.h
| From: | Marcus Boerger | Date: | Sat, 24 Feb 2007 18:20:46 +0000 |
| Subject: | cvs: php-src(PHP_5_2) /main snprintf.c snprintf.h | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-43266@lists.php.net to get a copy of this message | ||
helly Sat Feb 24 18:20:46 2007 UTC
Modified files: (Branch: PHP_5_2)
/php-src/main snprintf.c snprintf.h
Log:
- Add [v]slprintf to overcome the returnvalues issue of snprintf
http://cvs.php.net/viewvc.cgi/php-src/main/snprintf.c?r1=1.37.2.4.2.8&r2=1.37.2.4.2.9&diff_format=u
Index: php-src/main/snprintf.c
diff -u php-src/main/snprintf.c:1.37.2.4.2.8 php-src/main/snprintf.c:1.37.2.4.2.9
--- php-src/main/snprintf.c:1.37.2.4.2.8 Mon Jan 1 09:36:11 2007
+++ php-src/main/snprintf.c Sat Feb 24 18:20:46 2007
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: snprintf.c,v 1.37.2.4.2.8 2007/01/01 09:36:11 sebastian Exp $ */
+/* $Id: snprintf.c,v 1.37.2.4.2.9 2007/02/24 18:20:46 helly Exp $ */
#include "php.h"
@@ -1161,6 +1161,34 @@
}
+PHPAPI int ap_php_slprintf(char *buf, size_t len, const char *format,...)
+{
+ int cc;
+ va_list ap;
+
+ va_start(ap, format);
+ strx_printv(&cc, buf, len, format, ap);
+ va_end(ap);
+ if (cc >= len) {
+ cc = len -1;
+ buf[cc] = '\0';
+ }
+ return cc;
+}
+
+
+PHPAPI int ap_php_vslprintf(char *buf, size_t len, const char *format, va_list ap)
+{
+ int cc;
+
+ strx_printv(&cc, buf, len, format, ap);
+ if (cc >= len) {
+ cc = len -1;
+ buf[cc] = '\0';
+ }
+ return cc;
+}
+
PHPAPI int ap_php_snprintf(char *buf, size_t len, const char *format,...)
{
int cc;
http://cvs.php.net/viewvc.cgi/php-src/main/snprintf.h?r1=1.32.2.3.2.3&r2=1.32.2.3.2.4&diff_format=u
Index: php-src/main/snprintf.h
diff -u php-src/main/snprintf.h:1.32.2.3.2.3 php-src/main/snprintf.h:1.32.2.3.2.4
--- php-src/main/snprintf.h:1.32.2.3.2.3 Mon Jan 1 09:36:11 2007
+++ php-src/main/snprintf.h Sat Feb 24 18:20:46 2007
@@ -17,11 +17,11 @@
+----------------------------------------------------------------------+
*/
-/* $Id: snprintf.h,v 1.32.2.3.2.3 2007/01/01 09:36:11 sebastian Exp $ */
+/* $Id: snprintf.h,v 1.32.2.3.2.4 2007/02/24 18:20:46 helly Exp $ */
/*
-Comparing: sprintf, snprintf, spprintf
+Comparing: sprintf, snprintf, slprintf, spprintf
sprintf offers the ability to make a lot of failures since it does not know
the size of the buffer it uses. Therefore usage of sprintf often
@@ -36,6 +36,11 @@
A bad thing is having a big maximum while in most cases you would
only need a small buffer. If the size of the resulting string is
longer or equal to the buffer size than the buffer is not terminated.
+ The function also returns the number of chars not including the
+ terminating \0 that were needed to fully comply to the print request.
+
+slprintf same as snprintf with the difference that it actually returns the
+ length printed not including the terminating \0.
spprintf is the dynamical version of snprintf. It allocates the buffer in size
as needed and allows a maximum setting as snprintf (turn this feature
@@ -73,6 +78,8 @@
BEGIN_EXTERN_C()
+PHPAPI int ap_php_slprintf(char *buf, size_t len, const char *format,...)
PHP_ATTRIBUTE_FORMAT(printf, 3, 4);
+PHPAPI int ap_php_vslprintf(char *buf, size_t len, const char *format, va_list ap)
PHP_ATTRIBUTE_FORMAT(printf, 3, 0);
PHPAPI int ap_php_snprintf(char *, size_t, const char *, ...) PHP_ATTRIBUTE_FORMAT(printf, 3, 4);
PHPAPI int ap_php_vsnprintf(char *, size_t, const char *, va_list ap) PHP_ATTRIBUTE_FORMAT(printf,
3, 0);
PHPAPI int php_sprintf (char* s, const char* format, ...) PHP_ATTRIBUTE_FORMAT(printf, 2, 3);
@@ -82,6 +89,16 @@
END_EXTERN_C()
+#ifdef slprintf
+#undef slprintf
+#endif
+#define slprintf ap_php_slprintf
+
+#ifdef vslprintf
+#undef vslprintf
+#endif
+#define vslprintf ap_php_vslprintf
+
#ifdef snprintf
#undef snprintf
#endif