|
@@ -225,31 +225,31 @@ char *faux_str_mcat(char **str, ...)
|
|
|
}
|
|
|
|
|
|
|
|
|
-/** @brief Allocates memory and sprintf() to it.
|
|
|
+/** @brief Allocates memory and vsprintf() to it.
|
|
|
*
|
|
|
* Function tries to find out necessary amount of memory for specified format
|
|
|
- * string and arguments. Format is same as for sprintf() function. Then
|
|
|
- * function allocates memory for resulting string and sprintf() to it. So
|
|
|
+ * string and arguments. Format is same as for vsprintf() function. Then
|
|
|
+ * function allocates memory for resulting string and vsprintf() to it. So
|
|
|
* user doesn't need to allocate buffer himself. Function returns allocated
|
|
|
* string that need to be freed by faux_str_free() function later.
|
|
|
*
|
|
|
* @warning The returned pointer must be free by faux_str_free().
|
|
|
*
|
|
|
* @param [in] fmt Format string like the sprintf()'s fmt.
|
|
|
- * @param [in] arg Number of arguments.
|
|
|
+ * @param [in] ap The va_list argument.
|
|
|
* @return Allocated resulting string or NULL on error.
|
|
|
*/
|
|
|
-char *faux_str_sprintf(const char *fmt, ...)
|
|
|
+char *faux_str_vsprintf(const char *fmt, va_list ap)
|
|
|
{
|
|
|
int size = 1;
|
|
|
char calc_buf[1] = "";
|
|
|
char *line = NULL;
|
|
|
- va_list ap;
|
|
|
+ va_list ap2;
|
|
|
|
|
|
// Calculate buffer size
|
|
|
- va_start(ap, fmt);
|
|
|
- size = vsnprintf(calc_buf, size, fmt, ap);
|
|
|
- va_end(ap);
|
|
|
+ va_copy(ap2, ap);
|
|
|
+ size = vsnprintf(calc_buf, size, fmt, ap2);
|
|
|
+ va_end(ap2);
|
|
|
// The snprintf() prior to 2.0.6 glibc version returns -1 if string
|
|
|
// was truncated. The later glibc returns required buffer size.
|
|
|
// The calc_buf can be NULL and size can be 0 for recent glibc but
|
|
@@ -264,9 +264,7 @@ char *faux_str_sprintf(const char *fmt, ...)
|
|
|
return NULL;
|
|
|
|
|
|
// Format real string
|
|
|
- va_start(ap, fmt);
|
|
|
size = vsnprintf(line, size, fmt, ap);
|
|
|
- va_end(ap);
|
|
|
if (size < 0) { // Some problems
|
|
|
faux_str_free(line);
|
|
|
return NULL;
|
|
@@ -276,6 +274,33 @@ char *faux_str_sprintf(const char *fmt, ...)
|
|
|
}
|
|
|
|
|
|
|
|
|
+/** @brief Allocates memory and sprintf() to it.
|
|
|
+ *
|
|
|
+ * Function tries to find out necessary amount of memory for specified format
|
|
|
+ * string and arguments. Format is same as for sprintf() function. Then
|
|
|
+ * function allocates memory for resulting string and sprintf() to it. So
|
|
|
+ * user doesn't need to allocate buffer himself. Function returns allocated
|
|
|
+ * string that need to be freed by faux_str_free() function later.
|
|
|
+ *
|
|
|
+ * @warning The returned pointer must be free by faux_str_free().
|
|
|
+ *
|
|
|
+ * @param [in] fmt Format string like the sprintf()'s fmt.
|
|
|
+ * @param [in] arg Number of arguments.
|
|
|
+ * @return Allocated resulting string or NULL on error.
|
|
|
+ */
|
|
|
+char *faux_str_sprintf(const char *fmt, ...)
|
|
|
+{
|
|
|
+ char *line = NULL;
|
|
|
+ va_list ap;
|
|
|
+
|
|
|
+ va_start(ap, fmt);
|
|
|
+ line = faux_str_vsprintf(fmt, ap);
|
|
|
+ va_end(ap);
|
|
|
+
|
|
|
+ return line;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
/** @brief Service function to compare to chars in right way.
|
|
|
*
|
|
|
* The problem is char type can be signed or unsigned on different
|