From 0b6459cb28e5f0f71b4ea73c53a0ad511d2a255f Mon Sep 17 00:00:00 2001 From: patacongo Date: Fri, 21 Sep 2012 22:05:41 +0000 Subject: [PATCH] More sscanf: Long flag (as in %ld) not be used in all of the places it should be git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5172 42af7a65-404d-4744-a932-0658087f49c3 --- lib/stdio/lib_sscanf.c | 54 +++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/lib/stdio/lib_sscanf.c b/lib/stdio/lib_sscanf.c index 032164ba1e..7e1fae276d 100644 --- a/lib/stdio/lib_sscanf.c +++ b/lib/stdio/lib_sscanf.c @@ -38,9 +38,11 @@ ****************************************************************************/ #include + #include #include #include +#include #include #include #include @@ -173,11 +175,11 @@ int vsscanf(FAR char *buf, FAR const char *fmt, va_list ap) FAR char *bufstart; FAR char *tv; FAR const char *tc; + bool lflag; + bool noassign; int count; - int noassign; int width; int base = 10; - int lflag; char tmp[MAXLN]; lvdbg("vsscanf: buf=\"%s\" fmt=\"%s\"\n", buf, fmt); @@ -191,9 +193,9 @@ int vsscanf(FAR char *buf, FAR const char *fmt, va_list ap) /* Parse the format, extracting values from the input buffer as needed */ count = 0; - noassign = 0; width = 0; - lflag = 0; + noassign = false; + lflag = false; while (*fmt && *buf) { @@ -223,11 +225,13 @@ int vsscanf(FAR char *buf, FAR const char *fmt, va_list ap) if (*fmt == '*') { - noassign = 1; + noassign = true; } else if (*fmt == 'l' || *fmt == 'L') { - lflag = 1; + /* NOTE: Missing check for long long ('ll') */ + + lflag = true; } else if (*fmt >= '1' && *fmt <= '9') { @@ -349,15 +353,24 @@ int vsscanf(FAR char *buf, FAR const char *fmt, va_list ap) buf += width; if (!noassign) { - int *pint = va_arg(ap, int*); #ifdef SDCC char *endptr; - int tmpint = strtol(tmp, &endptr, base); + long tmplong = strtol(tmp, &endptr, base); #else - int tmpint = strtol(tmp, NULL, base); + long tmplong = strtol(tmp, NULL, base); #endif - lvdbg("vsscanf: Return %d to 0x%p\n", tmpint, pint); - *pint = tmpint; + if (lflag) + { + long *plong = va_arg(ap, long*); + lvdbg("vsscanf: Return %ld to 0x%p\n", tmplong, plong); + *plong = tmplong; + } + else + { + int *pint = va_arg(ap, int*); + lvdbg("vsscanf: Return %ld to 0x%p\n", tmplong, pint); + *pint = (int)tmplong; + } } } @@ -440,8 +453,18 @@ int vsscanf(FAR char *buf, FAR const char *fmt, va_list ap) if (!noassign) { - int *pint = va_arg(ap, int*); - *pint = (int)(buf - bufstart); + size_t nchars = (size_t)(buf - bufstart); + + if (lflag) + { + long *plong = va_arg(ap, long*); + *plong = (long)nchars; + } + else + { + int *pint = va_arg(ap, int*); + *pint = (int)nchars; + } } } @@ -452,7 +475,10 @@ int vsscanf(FAR char *buf, FAR const char *fmt, va_list ap) count++; } - width = noassign = lflag = 0; + width = 0; + noassign = false; + lflag = false; + fmt++; }