basestring_vsnprintf.c 1.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

17
/* define on IRIX to get posix compliant vsnprintf */
18 19
#define _XOPEN_SOURCE 500
#include <stdio.h>
20
#include <basestring_vsnprintf.h>
21
#include <my_config.h>
22 23 24 25

int
basestring_snprintf(char *str, size_t size, const char *format, ...)
{
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
26
  int ret;
27 28
  va_list ap;
  va_start(ap, format);
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
29
  ret= basestring_vsnprintf(str, size, format, ap);
30 31 32
  va_end(ap);
  return(ret);
}
33

34 35 36
#ifdef HAVE_SNPRINTF
  #define BASESTRING_VSNPRINTF_FUNC(a,b,c,d) vsnprintf(a,b,c,d)
#else
37
  #define SNPRINTF_RETURN_TRUNC
38 39 40
  #define BASESTRING_VSNPRINTF_FUNC(a,b,c,d) my_vsnprintf(a,b,c,d)
  extern int my_vsnprintf(char *str, size_t size, const char *format, va_list ap);
#endif
41
#ifdef SNPRINTF_RETURN_TRUNC
42 43
static char basestring_vsnprintf_buf[16*1024];
#endif
44 45 46
int
basestring_vsnprintf(char *str, size_t size, const char *format, va_list ap)
{
47
  int ret= BASESTRING_VSNPRINTF_FUNC(str, size, format, ap);
48 49
#ifdef SNPRINTF_RETURN_TRUNC
  if (ret == size-1) {
50 51 52 53 54 55
    ret= BASESTRING_VSNPRINTF_FUNC(basestring_vsnprintf_buf,
				   sizeof(basestring_vsnprintf_buf),
				   format, ap);
  }
#endif
  return ret;
56
}