~mysql/mysql-server/mysql-6.0

« back to all changes in this revision

Viewing changes to mysys/my_vsnprintf.c

  • Committer: monty at mysql
  • Date: 2000-11-15 22:24:11 UTC
  • mfrom: (217.1.16)
  • mto: This revision was merged to the branch mainline in revision 249.
  • Revision ID: sp1r-monty@narttu.mysql.fi-20001115222411-59719
merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include <stdarg.h>
22
22
#include <m_ctype.h>
23
23
 
24
 
 
25
 
 
26
 
int my_vsnprintf(char* str, size_t n, const char* fmt, va_list ap)
 
24
int my_vsnprintf(char *to, size_t n, const char* fmt, va_list ap)
27
25
{
28
 
  uint          olen = 0, plen;
29
 
  const char *tpos;
30
 
  reg1 char     *endpos;
31
 
  reg2 char             * par;
32
 
  char* ebuff = str;
33
 
  
34
 
  endpos=ebuff;
35
 
  tpos = fmt;
 
26
  char *start=to, *end=to+n-1;
36
27
 
37
 
  while (*tpos)
 
28
  for (; *fmt ; fmt++)
38
29
  {
39
 
    if (tpos[0] != '%')
40
 
    {
41
 
      if(olen + 1 >= n)
42
 
        break;
43
 
      
44
 
      *endpos++= *tpos++;       /* Copy ordinary char */
45
 
      olen++;
46
 
      continue;
47
 
    }
48
 
    if (*++tpos == '%')         /* test if %% */
49
 
    {
50
 
      olen--;
51
 
    }
52
 
    else
53
 
    {
54
 
      /* Skipp if max size is used (to be compatible with printf) */
55
 
      while (isdigit(*tpos) || *tpos == '.' || *tpos == '-')
56
 
        tpos++;
57
 
      if (*tpos == 's')                         /* String parameter */
58
 
      {
59
 
        par = va_arg(ap, char *);
60
 
        plen = (uint) strlen(par);
61
 
        if (olen + plen < n)            /* Replace if possible */
62
 
        {
63
 
          endpos=strmov(endpos,par);
64
 
          tpos++;
65
 
          olen+=plen;
66
 
          continue;
67
 
        }
68
 
      }
69
 
      else if (*tpos == 'd' || *tpos == 'u')    /* Integer parameter */
70
 
      {
71
 
        register int iarg;
72
 
        iarg = va_arg(ap, int);
73
 
        if(olen + 16 >= n) break;
74
 
        
75
 
        if (*tpos == 'd')
76
 
          plen= (uint) (int2str((long) iarg,endpos, -10) - endpos);
77
 
        else
78
 
          plen= (uint) (int2str((long) (uint) iarg,endpos,10)- endpos);
79
 
        if (olen + plen < n) /* Replace parameter if possible */
80
 
        {
81
 
          endpos+=plen;
82
 
          tpos++;
83
 
          olen+=plen;
84
 
          continue;
85
 
        }
86
 
      }
87
 
    }
88
 
    *endpos++='%';              /* % used as % or unknown code */
 
30
    if (fmt[0] != '%')
 
31
    {
 
32
      if (to == end)                    /* End of buffer */
 
33
        break;
 
34
      *to++= *fmt;                      /* Copy ordinary char */
 
35
      continue;
 
36
    }
 
37
    /* Skipp if max size is used (to be compatible with printf) */
 
38
    while (isdigit(*fmt) || *fmt == '.' || *fmt == '-')
 
39
      fmt++;
 
40
    if (*fmt == 's')                            /* String parameter */
 
41
    {
 
42
      reg2 char *par = va_arg(ap, char *);
 
43
      uint plen = (uint) strlen(par);
 
44
      if ((uint) (end-to) > plen)       /* Replace if possible */
 
45
      {
 
46
        to=strmov(to,par);
 
47
        continue;
 
48
      }
 
49
    }
 
50
    else if (*fmt == 'd' || *fmt == 'u')        /* Integer parameter */
 
51
    {
 
52
      register int iarg;
 
53
      if ((uint) (end-to) < 16)
 
54
        break;
 
55
      iarg = va_arg(ap, int);
 
56
      if (*fmt == 'd')
 
57
        to=int10_to_str((long) iarg,to, -10);
 
58
      else
 
59
        to=int10_to_str((long) (uint) iarg,to,10);
 
60
      continue;
 
61
    }
 
62
    /* We come here on '%%', unknown code or too long parameter */
 
63
    if (to == end)
 
64
      break;
 
65
    *to++='%';                          /* % used as % or unknown code */
89
66
  }
90
 
  *endpos='\0';
91
 
  /* End of errmessage */
92
 
 return olen;
 
67
  *to='\0';                             /* End of errmessage */
 
68
  return (uint) (to - start);
93
69
}
94