diff -Nru a/include/asterisk/utils.h b/include/asterisk/utils.h --- a/include/asterisk/utils.h 2005-03-20 10:54:15 -07:00 +++ b/include/asterisk/utils.h 2005-03-20 10:54:15 -07:00 @@ -172,4 +172,17 @@ extern char *ast_strcasestr(const char *, const char *); #endif /* __linux__ */ extern int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data, size_t stacksize); -#endif + +/*! Build a string in a buffer, designed to be called repeatedly */ +/*! + * This is a wrapper for snprintf, that properly handles the buffer pointer + * and buffer space available. + * Returns 0 on success, -1 on failure. + * + * \param buffer current position in buffer to place string into (will be updated on return) + * \param space remaining space in buffer (will be updated on return) + * \param fmt printf-style format string + */ +extern int ast_build_string(char **buffer, size_t *space, const char *fmt, ...) __attribute__ ((format (printf, 3, 4))); + +#endif /* _ASTERISK_UTIL_H */ diff -Nru a/utils.c b/utils.c --- a/utils.c 2005-03-20 10:54:15 -07:00 +++ b/utils.c 2005-03-20 10:54:15 -07:00 @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -458,3 +459,25 @@ } } #endif + +int ast_build_string(char **buffer, size_t *space, const char *fmt, ...) +{ + va_list ap; + int result; + + if (!buffer || !*buffer || !space || !*space) + return -1; + + va_start(ap, fmt); + result = vsnprintf(*buffer, *space, fmt, ap); + va_end(ap); + + if (result < 0) + return -1; + else if (result > *space) + result = *space; + + *buffer += result; + *space -= result; + return 0; +}