my_vsnprintf.c 3.27 KB
Newer Older
unknown's avatar
unknown committed
1 2 3 4 5 6 7 8
/* Copyright (C) 2000 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,
unknown's avatar
unknown committed
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
unknown's avatar
unknown committed
10 11 12 13 14 15
   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 */
unknown's avatar
unknown committed
16 17 18 19 20 21

#include "mysys_priv.h"
#include "mysys_err.h"
#include <m_string.h>
#include <stdarg.h>
#include <m_ctype.h>
unknown's avatar
unknown committed
22
#include <assert.h>
unknown's avatar
unknown committed
23

24 25 26 27 28 29 30
int my_snprintf(char* to, size_t n, const char* fmt, ...)
{
  va_list args;
  va_start(args,fmt);
  return my_vsnprintf(to, n, fmt, args);
}

31
int my_vsnprintf(char *to, size_t n, const char* fmt, va_list ap)
unknown's avatar
unknown committed
32
{
33 34
  char *start=to, *end=to+n-1;
  for (; *fmt ; fmt++)
unknown's avatar
unknown committed
35
  {
36
    if (fmt[0] != '%')
unknown's avatar
unknown committed
37
    {
38
      if (to == end)			/* End of buffer */
unknown's avatar
unknown committed
39
	break;
40
      *to++= *fmt;			/* Copy ordinary char */
unknown's avatar
unknown committed
41 42
      continue;
    }
43
    /* Skip if max size is used (to be compatible with printf) */
44
    fmt++;
45
    while (my_isdigit(system_charset_info,*fmt) || *fmt == '.' || *fmt == '-')
46
      fmt++;
unknown's avatar
unknown committed
47
    if (*fmt == 'l')
unknown's avatar
unknown committed
48
      fmt++;
49
    if (*fmt == 's')				/* String parameter */
unknown's avatar
unknown committed
50
    {
51
      reg2 char	*par = va_arg(ap, char *);
52
      uint plen,left_len = (uint)(end-to);
unknown's avatar
unknown committed
53
      if (!par) par = (char*)"(null)";
unknown's avatar
unknown committed
54
      plen = (uint) strlen(par);
55 56
      if (left_len <= plen)
	plen = left_len - 1;
unknown's avatar
unknown committed
57
      to=strnmov(to,par,plen);
58
      continue;
unknown's avatar
unknown committed
59
    }
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    else if (*fmt == 'd' || *fmt == 'u')	/* Integer parameter */
    {
      register int iarg;
      if ((uint) (end-to) < 16)
	break;
      iarg = va_arg(ap, int);
      if (*fmt == 'd')
	to=int10_to_str((long) iarg,to, -10);
      else
	to=int10_to_str((long) (uint) iarg,to,10);
      continue;
    }
    /* We come here on '%%', unknown code or too long parameter */
    if (to == end)
      break;
    *to++='%';				/* % used as % or unknown code */
unknown's avatar
unknown committed
76
  }
unknown's avatar
unknown committed
77
  DBUG_ASSERT(to <= end);
78 79
  *to='\0';				/* End of errmessage */
  return (uint) (to - start);
unknown's avatar
unknown committed
80
}
unknown's avatar
unknown committed
81 82

#ifdef MAIN
unknown's avatar
unknown committed
83
#define OVERRUN_SENTRY  250
unknown's avatar
unknown committed
84 85
static void my_printf(const char * fmt, ...)
{
unknown's avatar
unknown committed
86
  char buf[33];
unknown's avatar
unknown committed
87 88 89
  int n;
  va_list ar;
  va_start(ar, fmt);
unknown's avatar
unknown committed
90 91
  buf[sizeof(buf)-1]=OVERRUN_SENTRY;
  n = my_vsnprintf(buf, sizeof(buf)-1,fmt, ar);
unknown's avatar
unknown committed
92 93
  printf(buf);
  printf("n=%d, strlen=%d\n", n, strlen(buf));
unknown's avatar
unknown committed
94 95 96 97 98
  if (buf[sizeof(buf)-1] != OVERRUN_SENTRY)
  {
    fprintf(stderr, "Buffer overrun\n");
    abort();
  }
unknown's avatar
unknown committed
99 100 101 102 103
  va_end(ar);
}

int main()
{
unknown's avatar
unknown committed
104

unknown's avatar
unknown committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118
  my_printf("Hello\n");
  my_printf("Hello int, %d\n", 1);
  my_printf("Hello string '%s'\n", "I am a string");
  my_printf("Hello hack hack hack hack hack hack hack %d\n", 1);
  my_printf("Hello %d hack  %d\n", 1, 4);
  my_printf("Hello %d hack hack hack hack hack %d\n", 1, 4);
  my_printf("Hello '%s' hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\n", "hack");
  my_printf("Hello hhhhhhhhhhhhhh %d sssssssssssssss\n", 1);
  my_printf("Hello  %u\n", 1);
  my_printf("conn %ld to: '%-.64s' user: '%-.32s' host:\
 `%-.64s' (%-.64s)", 1, 0,0,0,0);
  return 0;
}
#endif