su_sprintf.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@ingroup su_alloc
  25. *
  26. * @CFILE su_sprintf.c su_*sprintf() functions
  27. *
  28. * @author Pekka Pessi <Pekka.Pessi@nokia.com>
  29. *
  30. * @date Created: Tue Apr 17 20:05:21 2001 ppessi
  31. */
  32. #include "config.h"
  33. #include <stdlib.h>
  34. #include <stddef.h>
  35. #include <stdarg.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <limits.h>
  39. #if defined(va_copy)
  40. /* Xyzzy */
  41. #elif defined(__va_copy)
  42. #define va_copy(dst, src) __va_copy((dst), (src))
  43. #else
  44. #define va_copy(dst, src) (memcpy(&(dst), &(src), sizeof (va_list)))
  45. #endif
  46. #include "sofia-sip/su_alloc.h"
  47. /**Copy a formatted string.
  48. *
  49. * The function su_vsprintf() print a string according to a @a fmt like
  50. * vprintf() or vsnprintf(). The resulting string is copied to a memory area
  51. * fresly allocated from a memory @a home. The returned string is reclaimed
  52. * when @a home is destroyed. It can explicitly be freed with su_free() or
  53. * free() if @a home is NULL.
  54. *
  55. * @param home pointer to memory home (may be NULL)
  56. * @param fmt format string
  57. * @param ap @e stdarg argument list (must match with the @a fmt format string)
  58. *
  59. * @return A pointer to a fresh copy of formatting result, or NULL upon an
  60. * error.
  61. */
  62. char *su_vsprintf(su_home_t *home, char const *fmt, va_list ap)
  63. {
  64. int n;
  65. size_t len;
  66. char *rv, s[128];
  67. va_list aq;
  68. va_copy(aq, ap);
  69. n = vsnprintf(s, sizeof(s), fmt, aq);
  70. va_end(aq);
  71. if (n >= 0 && (size_t)n + 1 < sizeof(s))
  72. return su_strdup(home, s);
  73. len = n > 0 ? (size_t)n + 1 : 2 * sizeof(s);
  74. for (rv = su_alloc(home, len);
  75. rv;
  76. rv = su_realloc(home, rv, len)) {
  77. va_copy(aq, ap);
  78. n = vsnprintf(rv, len, fmt, aq);
  79. va_end(aq);
  80. if (n > -1 && (size_t)n < len)
  81. break;
  82. if (n > -1) /* glibc >2.1 */
  83. len = (size_t)n + 1;
  84. else /* glibc 2.0 */
  85. len *= 2;
  86. if (len > INT_MAX)
  87. return (void)su_free(home, rv), NULL;
  88. }
  89. return rv;
  90. }
  91. /**Copy a formatted string.
  92. *
  93. * The function su_sprintf() print a string according to a @a fmt like
  94. * printf() or snprintf(). The resulting string is copied to a memory area
  95. * freshly allocated from a memory @a home. The returned string is reclaimed
  96. * when @a home is destroyed. It can explicitly be freed with su_free() or
  97. * free() if @a home is NULL.
  98. *
  99. * @param home pointer to memory home (may be NULL)
  100. * @param fmt format string
  101. * @param ... argument list (must match with the @a fmt format string)
  102. *
  103. * @return A pointer to a fresh copy of formatting result, or NULL upon an
  104. * error.
  105. */
  106. char *su_sprintf(su_home_t *home, char const *fmt, ...)
  107. {
  108. va_list ap;
  109. char *rv;
  110. va_start(ap, fmt);
  111. rv = su_vsprintf(home, fmt, ap);
  112. va_end(ap);
  113. return rv;
  114. }