su_sprintf.c 3.47 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@ingroup su_alloc
 *
 * @CFILE su_sprintf.c  su_*sprintf() functions
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
30
 *
Pekka Pessi's avatar
Pekka Pessi committed
31 32 33 34 35 36 37 38 39 40
 * @date Created: Tue Apr 17 20:05:21 2001 ppessi
 */

#include "config.h"

#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
41
#include <limits.h>
Pekka Pessi's avatar
Pekka Pessi committed
42 43 44 45 46 47 48 49 50

#if defined(va_copy)
/* Xyzzy */
#elif defined(__va_copy)
#define va_copy(dst, src) __va_copy((dst), (src))
#else
#define va_copy(dst, src) (memcpy(&(dst), &(src), sizeof (va_list)))
#endif

51
#include "sofia-sip/su_alloc.h"
Pekka Pessi's avatar
Pekka Pessi committed
52

53
/**Copy a formatted string.
Pekka Pessi's avatar
Pekka Pessi committed
54 55 56 57 58 59 60 61 62 63
 *
 * The function su_vsprintf() print a string according to a @a fmt like
 * vprintf() or vsnprintf(). The resulting string is copied to a memory area
 * fresly allocated from a memory @a home. The returned string is reclaimed
 * when @a home is destroyed. It can explicitly be freed with su_free() or
 * free() if @a home is NULL.
 *
 * @param home pointer to memory home (may be NULL)
 * @param fmt format string
 * @param ap @e stdarg argument list (must match with the @a fmt format string)
64
 *
Pekka Pessi's avatar
Pekka Pessi committed
65 66 67 68 69
 * @return A pointer to a fresh copy of formatting result, or NULL upon an
 * error.
 */
char *su_vsprintf(su_home_t *home, char const *fmt, va_list ap)
{
70 71
  int n;
  size_t len;
Pekka Pessi's avatar
Pekka Pessi committed
72 73 74 75
  char *rv, s[128];
  va_list aq;

  va_copy(aq, ap);
76
  n = vsnprintf(s, sizeof(s), fmt, aq);
Pekka Pessi's avatar
Pekka Pessi committed
77 78
  va_end(aq);

79
  if (n >= 0 && (size_t)n + 1 < sizeof(s))
Pekka Pessi's avatar
Pekka Pessi committed
80
    return su_strdup(home, s);
81

82
  len = n > 0 ? (size_t)n + 1 : 2 * sizeof(s);
Pekka Pessi's avatar
Pekka Pessi committed
83 84 85 86 87 88 89

  for (rv = su_alloc(home, len);
       rv;
       rv = su_realloc(home, rv, len)) {
    va_copy(aq, ap);
    n = vsnprintf(rv, len, fmt, aq);
    va_end(aq);
90
    if (n > -1 && (size_t)n < len)
Pekka Pessi's avatar
Pekka Pessi committed
91 92
      break;
    if (n > -1)			/* glibc >2.1 */
93
      len = (size_t)n + 1;
Pekka Pessi's avatar
Pekka Pessi committed
94
    else			/* glibc 2.0 */
95 96 97 98
      len *= 2;

    if (len > INT_MAX)
      return (void)su_free(home, rv), NULL;
Pekka Pessi's avatar
Pekka Pessi committed
99 100 101 102 103
  }

  return rv;
}

104
/**Copy a formatted string.
Pekka Pessi's avatar
Pekka Pessi committed
105 106 107
 *
 * The function su_sprintf() print a string according to a @a fmt like
 * printf() or snprintf(). The resulting string is copied to a memory area
108
 * freshly allocated from a memory @a home. The returned string is reclaimed
Pekka Pessi's avatar
Pekka Pessi committed
109 110 111 112 113 114
 * when @a home is destroyed. It can explicitly be freed with su_free() or
 * free() if @a home is NULL.
 *
 * @param home pointer to memory home (may be NULL)
 * @param fmt format string
 * @param ... argument list (must match with the @a fmt format string)
115
 *
Pekka Pessi's avatar
Pekka Pessi committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129
 * @return A pointer to a fresh copy of formatting result, or NULL upon an
 * error.
 */
char *su_sprintf(su_home_t *home, char const *fmt, ...)
{
  va_list ap;
  char *rv;

  va_start(ap, fmt);
  rv = su_vsprintf(home, fmt, ap);
  va_end(ap);

  return rv;
}