summaryrefslogtreecommitdiff
path: root/strings/my_vsnprintf.c
diff options
context:
space:
mode:
Diffstat (limited to 'strings/my_vsnprintf.c')
-rw-r--r--strings/my_vsnprintf.c50
1 files changed, 22 insertions, 28 deletions
diff --git a/strings/my_vsnprintf.c b/strings/my_vsnprintf.c
index 784c4762724..268f7d18f2a 100644
--- a/strings/my_vsnprintf.c
+++ b/strings/my_vsnprintf.c
@@ -27,27 +27,16 @@
%#[l]d
%#[l]u
%#[l]x
- %#.#s Note #.# is skiped
+ %#.#s Note first # is ignored
RETURN
length of result string
*/
-int my_snprintf(char* to, size_t n, const char* fmt, ...)
-{
- int result;
- va_list args;
- va_start(args,fmt);
- result= my_vsnprintf(to, n, fmt, args);
- va_end(args);
- return result;
-}
-
-
int my_vsnprintf(char *to, size_t n, const char* fmt, va_list ap)
{
char *start=to, *end=to+n-1;
- uint length, num_state, pre_zero, have_long;
+ uint length, width, pre_zero, have_long;
for (; *fmt ; fmt++)
{
@@ -62,23 +51,18 @@ int my_vsnprintf(char *to, size_t n, const char* fmt, va_list ap)
/* Read max fill size (only used with %d and %u) */
if (*fmt == '-')
fmt++;
- length= num_state= pre_zero= have_long= 0;
- for (;; fmt++)
+ length= width= pre_zero= have_long= 0;
+ for (;my_isdigit(&my_charset_latin1,*fmt); fmt++)
{
- if (my_isdigit(&my_charset_latin1,*fmt))
- {
- if (!num_state)
- {
- length=length*10+ (uint) (*fmt-'0');
- if (!length)
- pre_zero= 1; /* first digit was 0 */
- }
- continue;
- }
- if (*fmt != '.' || num_state)
- break;
- num_state= 1;
+ length=length*10+ (uint) (*fmt-'0');
+ if (!length)
+ pre_zero= 1; /* first digit was 0 */
}
+ if (*fmt == '.')
+ for (fmt++;my_isdigit(&my_charset_latin1,*fmt); fmt++)
+ width=width*10+ (uint) (*fmt-'0');
+ else
+ width= ~0;
if (*fmt == 'l')
{
fmt++;
@@ -90,6 +74,7 @@ int my_vsnprintf(char *to, size_t n, const char* fmt, va_list ap)
uint plen,left_len = (uint)(end-to)+1;
if (!par) par = (char*)"(null)";
plen = (uint) strlen(par);
+ set_if_smaller(plen,width);
if (left_len <= plen)
plen = left_len - 1;
to=strnmov(to,par,plen);
@@ -145,6 +130,15 @@ int my_vsnprintf(char *to, size_t n, const char* fmt, va_list ap)
return (uint) (to - start);
}
+int my_snprintf(char* to, size_t n, const char* fmt, ...)
+{
+ int result;
+ va_list args;
+ va_start(args,fmt);
+ result= my_vsnprintf(to, n, fmt, args);
+ va_end(args);
+ return result;
+}
#ifdef MAIN
#define OVERRUN_SENTRY 250