2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2012-11-10 17:34:46 +01:00
|
|
|
* libc/stdio/lib_sscanf.c
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2014-02-18 22:38:44 +01:00
|
|
|
* Copyright (C) 2007, 2008, 2011-2014 Gregory Nutt. All rights reserved.
|
2012-11-10 17:06:01 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/compiler.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
scanf() fixes from kfrolov: 1) sscanf() function hangs in the following example: sscanf(2, %u,%u,%u,%u, ...), 2) sscanf() returns incorrect number of parsed numbers if some arguments can't be parsed: sscanf(=2, %u,%u,%u,%u,...)==1 instead of 0, and 3) using of char* instead of const char* in vsscanf function leads to warnings from GCC.
2013-10-21 18:41:15 +02:00
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
2014-01-14 20:30:22 +01:00
|
|
|
#include <stdio.h>
|
2012-11-10 17:06:01 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
scanf() fixes from kfrolov: 1) sscanf() function hangs in the following example: sscanf(2, %u,%u,%u,%u, ...), 2) sscanf() returns incorrect number of parsed numbers if some arguments can't be parsed: sscanf(=2, %u,%u,%u,%u,...)==1 instead of 0, and 3) using of char* instead of const char* in vsscanf function leads to warnings from GCC.
2013-10-21 18:41:15 +02:00
|
|
|
#include <errno.h>
|
2012-11-10 17:06:01 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-02-18 21:14:07 +01:00
|
|
|
* Pre-processor Definitions
|
2012-11-10 17:06:01 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define MAXLN 128
|
|
|
|
|
2014-02-18 21:14:07 +01:00
|
|
|
#ifndef MIN
|
2014-02-18 22:38:44 +01:00
|
|
|
# define MIN(a,b) (((a) < (b)) ? (a) : (b))
|
2014-02-18 21:14:07 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MAX
|
2014-02-18 22:38:44 +01:00
|
|
|
# define MAX(a,b) (((a) > (b)) ? (a) : (b))
|
2014-02-18 21:14:07 +01:00
|
|
|
#endif
|
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Type Declarations
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-03-28 22:17:43 +01:00
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Function Prototypes
|
2012-11-10 17:06:01 +01:00
|
|
|
****************************************************************************/
|
2014-03-28 22:17:43 +01:00
|
|
|
|
scanf() fixes from kfrolov: 1) sscanf() function hangs in the following example: sscanf(2, %u,%u,%u,%u, ...), 2) sscanf() returns incorrect number of parsed numbers if some arguments can't be parsed: sscanf(=2, %u,%u,%u,%u,...)==1 instead of 0, and 3) using of char* instead of const char* in vsscanf function leads to warnings from GCC.
2013-10-21 18:41:15 +02:00
|
|
|
int vsscanf(FAR const char *buf, FAR const char *fmt, va_list ap);
|
2014-03-28 22:17:43 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Constant Data
|
2014-03-28 22:17:43 +01:00
|
|
|
****************************************************************************/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Data
|
2012-11-10 17:06:01 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-03-28 22:17:43 +01:00
|
|
|
/****************************************************************************
|
2012-11-10 17:06:01 +01:00
|
|
|
* Private Constant Data
|
2014-03-28 22:17:43 +01:00
|
|
|
****************************************************************************/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
static const char spaces[] = " \t\n\r\f\v";
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Function: findwidth
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Try to figure out the width of the input data.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int findwidth(FAR const char *buf, FAR const char *fmt)
|
|
|
|
{
|
|
|
|
FAR const char *next = fmt + 1;
|
|
|
|
|
|
|
|
/* No... is there a space after the format? Or does the format string end
|
|
|
|
* here?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (isspace(*next) || *next == 0)
|
|
|
|
{
|
|
|
|
/* Use the input up until the first white space is encountered. */
|
|
|
|
|
|
|
|
return strcspn(buf, spaces);
|
|
|
|
}
|
|
|
|
|
2013-08-27 17:40:19 +02:00
|
|
|
/* No.. Another possibility is the format character is followed by
|
2012-11-10 17:06:01 +01:00
|
|
|
* some recognizable delimiting value.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (*next != '%')
|
|
|
|
{
|
|
|
|
/* If so we will say that the string ends there if we can find that
|
|
|
|
* delimiter in the input string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
FAR const char *ptr = strchr(buf, *next);
|
|
|
|
if (ptr)
|
|
|
|
{
|
|
|
|
return (int)(ptr - buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 22:17:43 +01:00
|
|
|
/* No... the format has no delimiter and is back-to-back with the next
|
|
|
|
* format (or is followed by a delimiter that does not exist in the
|
2012-11-10 17:06:01 +01:00
|
|
|
* input string). At this point we just bail and Use the input up until
|
|
|
|
* the first white space is encountered.
|
|
|
|
*
|
|
|
|
* NOTE: This means that values from the following format may be
|
|
|
|
* concatenated with the first. This is a bug. We have no generic way of
|
2014-03-28 22:17:43 +01:00
|
|
|
* determining the width of the data if there is no fieldwidth, no space
|
2012-11-10 17:06:01 +01:00
|
|
|
* separating the input, and no usable delimiter character.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return strcspn(buf, spaces);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-02-22 01:08:58 +01:00
|
|
|
* Private Data
|
2012-11-10 17:06:01 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Function: sscanf
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* ANSI standard sscanf implementation.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int sscanf(FAR const char *buf, FAR const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2015-10-12 15:45:02 +02:00
|
|
|
count = vsscanf((FAR const char *)buf, fmt, ap);
|
2012-11-10 17:06:01 +01:00
|
|
|
va_end(ap);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Function: vsscanf
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* ANSI standard vsscanf implementation.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
scanf() fixes from kfrolov: 1) sscanf() function hangs in the following example: sscanf(2, %u,%u,%u,%u, ...), 2) sscanf() returns incorrect number of parsed numbers if some arguments can't be parsed: sscanf(=2, %u,%u,%u,%u,...)==1 instead of 0, and 3) using of char* instead of const char* in vsscanf function leads to warnings from GCC.
2013-10-21 18:41:15 +02:00
|
|
|
int vsscanf(FAR const char *buf, FAR const char *fmt, va_list ap)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
scanf() fixes from kfrolov: 1) sscanf() function hangs in the following example: sscanf(2, %u,%u,%u,%u, ...), 2) sscanf() returns incorrect number of parsed numbers if some arguments can't be parsed: sscanf(=2, %u,%u,%u,%u,...)==1 instead of 0, and 3) using of char* instead of const char* in vsscanf function leads to warnings from GCC.
2013-10-21 18:41:15 +02:00
|
|
|
FAR const char *bufstart;
|
2012-11-10 17:06:01 +01:00
|
|
|
FAR char *tv;
|
|
|
|
FAR const char *tc;
|
|
|
|
bool lflag;
|
|
|
|
bool noassign;
|
|
|
|
int count;
|
|
|
|
int width;
|
|
|
|
int base = 10;
|
|
|
|
char tmp[MAXLN];
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
linfo("vsscanf: buf=\"%s\" fmt=\"%s\"\n", buf, fmt);
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/* Remember the start of the input buffer. We will need this for %n
|
|
|
|
* calculations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bufstart = buf;
|
|
|
|
|
|
|
|
/* Parse the format, extracting values from the input buffer as needed */
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
width = 0;
|
|
|
|
noassign = false;
|
|
|
|
lflag = false;
|
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
/* Loop until all characters in the fmt string have been processed. We
|
|
|
|
* may have to continue loop after reaching the end the input data in
|
|
|
|
* order to handle trailing %n format specifiers.
|
2013-03-15 19:07:34 +01:00
|
|
|
*/
|
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
while (*fmt)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
|
|
|
/* Skip over white space */
|
|
|
|
|
|
|
|
while (isspace(*fmt))
|
|
|
|
{
|
|
|
|
fmt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for a conversion specifier */
|
|
|
|
|
|
|
|
if (*fmt == '%')
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
linfo("vsscanf: Specifier found\n");
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/* Check for qualifiers on the conversion specifier */
|
2013-03-18 01:41:42 +01:00
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
fmt++;
|
|
|
|
for (; *fmt; fmt++)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
linfo("vsscanf: Processing %c\n", *fmt);
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
if (strchr("dibouxcsefgn%", *fmt))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*fmt == '*')
|
|
|
|
{
|
|
|
|
noassign = true;
|
|
|
|
}
|
|
|
|
else if (*fmt == 'l' || *fmt == 'L')
|
|
|
|
{
|
|
|
|
/* NOTE: Missing check for long long ('ll') */
|
|
|
|
|
|
|
|
lflag = true;
|
|
|
|
}
|
|
|
|
else if (*fmt >= '1' && *fmt <= '9')
|
|
|
|
{
|
|
|
|
for (tc = fmt; isdigit(*fmt); fmt++);
|
|
|
|
strncpy(tmp, tc, fmt - tc);
|
|
|
|
tmp[fmt - tc] = '\0';
|
|
|
|
width = atoi(tmp);
|
|
|
|
fmt--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process %s: String conversion */
|
|
|
|
|
|
|
|
if (*fmt == 's')
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
linfo("vsscanf: Performing string conversion\n");
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
/* Get a pointer to the char * value. We need to do this even
|
|
|
|
* if we have reached the end of the input data in order to
|
|
|
|
* update the 'ap' variable.
|
|
|
|
*/
|
|
|
|
|
2014-03-30 22:24:38 +02:00
|
|
|
tv = NULL; /* To avoid warnings about begin uninitialized */
|
2013-03-18 01:41:42 +01:00
|
|
|
if (!noassign)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2015-10-12 15:45:02 +02:00
|
|
|
tv = va_arg(ap, FAR char *);
|
2013-03-18 01:41:42 +01:00
|
|
|
tv[0] = '\0';
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
/* But we only perform the data conversion is we still have
|
|
|
|
* bytes remaining in the input data stream.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
if (*buf)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2014-03-28 22:17:43 +01:00
|
|
|
/* Skip over white space */
|
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
while (isspace(*buf))
|
|
|
|
{
|
|
|
|
buf++;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
/* Was a fieldwidth specified? */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
if (!width)
|
|
|
|
{
|
|
|
|
/* No... Guess a field width using some heuristics */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2014-02-18 21:14:07 +01:00
|
|
|
int tmpwidth = findwidth(buf, fmt);
|
|
|
|
width = MIN(sizeof(tmp) - 1, tmpwidth);
|
2013-03-18 01:41:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the string (if we are making an assignment) */
|
|
|
|
|
|
|
|
if (!noassign)
|
|
|
|
{
|
2014-03-28 22:17:43 +01:00
|
|
|
strncpy(tv, buf, width);
|
2013-03-18 01:41:42 +01:00
|
|
|
tv[width] = '\0';
|
2014-03-30 22:24:38 +02:00
|
|
|
count++;
|
2013-03-18 01:41:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the buffer pointer past the string in the input */
|
|
|
|
|
|
|
|
buf += width;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process %c: Character conversion */
|
|
|
|
|
|
|
|
else if (*fmt == 'c')
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
linfo("vsscanf: Performing character conversion\n");
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
/* Get a pointer to the char * value. We need to do this even
|
|
|
|
* if we have reached the end of the input data in order to
|
|
|
|
* update the 'ap' variable.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
tv = NULL; /* To avoid warnings about beign uninitialized */
|
|
|
|
if (!noassign)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2015-10-12 15:45:02 +02:00
|
|
|
tv = va_arg(ap, FAR char *);
|
2013-03-18 01:41:42 +01:00
|
|
|
tv[0] = '\0';
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
/* But we only perform the data conversion is we still have
|
|
|
|
* bytes remaining in the input data stream.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (*buf)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2014-03-30 22:24:38 +02:00
|
|
|
/* Was a field width specified? */
|
2013-03-18 01:41:42 +01:00
|
|
|
|
|
|
|
if (!width)
|
|
|
|
{
|
|
|
|
/* No, then width is this one single character */
|
|
|
|
|
|
|
|
width = 1;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
/* Copy the character(s) (if we are making an assignment) */
|
|
|
|
|
|
|
|
if (!noassign)
|
|
|
|
{
|
|
|
|
strncpy(tv, buf, width);
|
|
|
|
tv[width] = '\0';
|
2014-03-30 22:24:38 +02:00
|
|
|
count++;
|
2013-03-18 01:41:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the buffer pointer past the character(s) in the
|
|
|
|
* input
|
|
|
|
*/
|
|
|
|
|
|
|
|
buf += width;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process %d, %o, %b, %x, %u: Various integer conversions */
|
|
|
|
|
|
|
|
else if (strchr("dobxu", *fmt))
|
|
|
|
{
|
2013-11-28 22:57:41 +01:00
|
|
|
FAR long *plong = NULL;
|
|
|
|
FAR int *pint = NULL;
|
2014-04-02 17:15:40 +02:00
|
|
|
bool sign;
|
2013-11-28 22:57:41 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
linfo("vsscanf: Performing integer conversion\n");
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
/* Get a pointer to the integer value. We need to do this even
|
|
|
|
* if we have reached the end of the input data in order to
|
|
|
|
* update the 'ap' variable.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
if (!noassign)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2013-03-18 01:41:42 +01:00
|
|
|
/* We have to check whether we need to return a long or an
|
|
|
|
* int.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (lflag)
|
|
|
|
{
|
2015-10-12 15:45:02 +02:00
|
|
|
plong = va_arg(ap, FAR long *);
|
2013-03-18 01:41:42 +01:00
|
|
|
*plong = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-12 15:45:02 +02:00
|
|
|
pint = va_arg(ap, FAR int *);
|
2013-03-18 01:41:42 +01:00
|
|
|
*pint = 0;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
|
2014-03-30 22:24:38 +02:00
|
|
|
/* But we only perform the data conversion if we still have
|
2013-03-18 01:41:42 +01:00
|
|
|
* bytes remaining in the input data stream.
|
2012-11-10 17:06:01 +01:00
|
|
|
*/
|
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
if (*buf)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2014-08-18 15:33:17 +02:00
|
|
|
FAR char *endptr;
|
|
|
|
int errsave;
|
|
|
|
long tmplong;
|
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
/* Skip over any white space before the integer string */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
while (isspace(*buf))
|
|
|
|
{
|
|
|
|
buf++;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
/* The base of the integer conversion depends on the
|
|
|
|
* specific conversion specification.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2014-04-02 17:15:40 +02:00
|
|
|
sign = false;
|
|
|
|
switch (*fmt)
|
2013-03-18 01:41:42 +01:00
|
|
|
{
|
2014-04-02 17:15:40 +02:00
|
|
|
default:
|
|
|
|
case 'd':
|
|
|
|
sign = true;
|
|
|
|
case 'u':
|
2013-03-18 01:41:42 +01:00
|
|
|
base = 10;
|
2014-04-02 17:15:40 +02:00
|
|
|
break;
|
|
|
|
|
2014-12-12 14:46:46 +01:00
|
|
|
case 'X':
|
2014-04-02 17:15:40 +02:00
|
|
|
case 'x':
|
2013-03-18 01:41:42 +01:00
|
|
|
base = 16;
|
2014-04-02 17:15:40 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o':
|
2013-03-18 01:41:42 +01:00
|
|
|
base = 8;
|
2014-04-02 17:15:40 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
2013-03-18 01:41:42 +01:00
|
|
|
base = 2;
|
2014-04-02 17:15:40 +02:00
|
|
|
break;
|
2013-03-18 01:41:42 +01:00
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2014-04-02 17:15:40 +02:00
|
|
|
/* Was a field width specified? */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
if (!width)
|
|
|
|
{
|
|
|
|
/* No... Guess a field width using some heuristics */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2014-02-18 22:38:44 +01:00
|
|
|
int tmpwidth = findwidth(buf, fmt);
|
2014-02-18 21:14:07 +01:00
|
|
|
width = MIN(sizeof(tmp) - 1, tmpwidth);
|
2013-03-18 01:41:42 +01:00
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
/* Copy the numeric string into a temporary working
|
|
|
|
* buffer.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
strncpy(tmp, buf, width);
|
|
|
|
tmp[width] = '\0';
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
linfo("vsscanf: tmp[]=\"%s\"\n", tmp);
|
2013-03-18 01:41:42 +01:00
|
|
|
|
|
|
|
/* Perform the integer conversion */
|
2014-08-18 15:33:17 +02:00
|
|
|
/* Preserve the errno value */
|
2013-03-18 01:41:42 +01:00
|
|
|
|
2014-08-18 15:33:17 +02:00
|
|
|
errsave = get_errno();
|
|
|
|
set_errno(0);
|
|
|
|
if (sign)
|
|
|
|
{
|
|
|
|
tmplong = strtol(tmp, &endptr, base);
|
|
|
|
}
|
|
|
|
else
|
2013-03-18 01:41:42 +01:00
|
|
|
{
|
2014-08-18 15:33:17 +02:00
|
|
|
tmplong = strtoul(tmp, &endptr, base);
|
|
|
|
}
|
scanf() fixes from kfrolov: 1) sscanf() function hangs in the following example: sscanf(2, %u,%u,%u,%u, ...), 2) sscanf() returns incorrect number of parsed numbers if some arguments can't be parsed: sscanf(=2, %u,%u,%u,%u,...)==1 instead of 0, and 3) using of char* instead of const char* in vsscanf function leads to warnings from GCC.
2013-10-21 18:41:15 +02:00
|
|
|
|
2014-08-18 15:33:17 +02:00
|
|
|
/* Check if the number was successfully converted */
|
2014-03-30 19:33:47 +02:00
|
|
|
|
2014-08-18 15:33:17 +02:00
|
|
|
if (tmp == endptr || get_errno() == ERANGE)
|
|
|
|
{
|
|
|
|
return count;
|
|
|
|
}
|
scanf() fixes from kfrolov: 1) sscanf() function hangs in the following example: sscanf(2, %u,%u,%u,%u, ...), 2) sscanf() returns incorrect number of parsed numbers if some arguments can't be parsed: sscanf(=2, %u,%u,%u,%u,...)==1 instead of 0, and 3) using of char* instead of const char* in vsscanf function leads to warnings from GCC.
2013-10-21 18:41:15 +02:00
|
|
|
|
2014-08-18 15:33:17 +02:00
|
|
|
/* Move by the actual number of characters converted */
|
scanf() fixes from kfrolov: 1) sscanf() function hangs in the following example: sscanf(2, %u,%u,%u,%u, ...), 2) sscanf() returns incorrect number of parsed numbers if some arguments can't be parsed: sscanf(=2, %u,%u,%u,%u,...)==1 instead of 0, and 3) using of char* instead of const char* in vsscanf function leads to warnings from GCC.
2013-10-21 18:41:15 +02:00
|
|
|
|
2014-08-18 15:33:17 +02:00
|
|
|
buf += (endptr - tmp);
|
|
|
|
set_errno(errsave);
|
|
|
|
if (!noassign)
|
|
|
|
{
|
scanf() fixes from kfrolov: 1) sscanf() function hangs in the following example: sscanf(2, %u,%u,%u,%u, ...), 2) sscanf() returns incorrect number of parsed numbers if some arguments can't be parsed: sscanf(=2, %u,%u,%u,%u,...)==1 instead of 0, and 3) using of char* instead of const char* in vsscanf function leads to warnings from GCC.
2013-10-21 18:41:15 +02:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
/* We have to check whether we need to return a long
|
|
|
|
* or an int.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (lflag)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
linfo("vsscanf: Return %ld to 0x%p\n",
|
2013-03-18 01:41:42 +01:00
|
|
|
tmplong, plong);
|
|
|
|
*plong = tmplong;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
linfo("vsscanf: Return %ld to 0x%p\n",
|
2013-03-18 01:41:42 +01:00
|
|
|
tmplong, pint);
|
|
|
|
*pint = (int)tmplong;
|
|
|
|
}
|
2014-03-30 22:24:38 +02:00
|
|
|
|
|
|
|
count++;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 15:41:33 +02:00
|
|
|
/* Process %a, %A, %f, %F, %e, %E, %g, and %G: Floating point
|
|
|
|
* conversions
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2015-09-17 15:41:33 +02:00
|
|
|
else if (strchr("aAfFeEgG", *fmt) != NULL)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2013-11-28 22:57:41 +01:00
|
|
|
#ifdef CONFIG_HAVE_DOUBLE
|
|
|
|
FAR double_t *pd = NULL;
|
|
|
|
#endif
|
|
|
|
FAR float *pf = NULL;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
linfo("vsscanf: Performing floating point conversion\n");
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
/* Get a pointer to the double value. We need to do this even
|
|
|
|
* if we have reached the end of the input data in order to
|
|
|
|
* update the 'ap' variable.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
if (!noassign)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2013-03-18 01:41:42 +01:00
|
|
|
/* We have to check whether we need to return a float or a
|
|
|
|
* double.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_HAVE_DOUBLE
|
|
|
|
if (lflag)
|
|
|
|
{
|
2015-10-12 15:45:02 +02:00
|
|
|
pd = va_arg(ap, FAR double_t *);
|
2013-03-18 01:41:42 +01:00
|
|
|
*pd = 0.0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2015-10-12 15:45:02 +02:00
|
|
|
pf = va_arg(ap, FAR float *);
|
2013-03-18 01:41:42 +01:00
|
|
|
*pf = 0.0;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
#ifdef CONFIG_LIBC_FLOATINGPOINT
|
|
|
|
/* But we only perform the data conversion is we still have
|
|
|
|
* bytes remaining in the input data stream.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
if (*buf)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2013-03-18 01:41:42 +01:00
|
|
|
/* Skip over any white space before the real string */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
while (isspace(*buf))
|
|
|
|
{
|
|
|
|
buf++;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
/* Was a fieldwidth specified? */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
if (!width)
|
|
|
|
{
|
|
|
|
/* No... Guess a field width using some heuristics */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
width = findwidth(buf, fmt);
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
/* Copy the real string into a temporary working buffer. */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
strncpy(tmp, buf, width);
|
|
|
|
tmp[width] = '\0';
|
|
|
|
buf += width;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
linfo("vsscanf: tmp[]=\"%s\"\n", tmp);
|
2013-03-18 01:41:42 +01:00
|
|
|
|
|
|
|
/* Perform the floating point conversion */
|
|
|
|
|
|
|
|
if (!noassign)
|
|
|
|
{
|
|
|
|
/* strtod always returns a double */
|
scanf() fixes from kfrolov: 1) sscanf() function hangs in the following example: sscanf(2, %u,%u,%u,%u, ...), 2) sscanf() returns incorrect number of parsed numbers if some arguments can't be parsed: sscanf(=2, %u,%u,%u,%u,...)==1 instead of 0, and 3) using of char* instead of const char* in vsscanf function leads to warnings from GCC.
2013-10-21 18:41:15 +02:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
FAR char *endptr;
|
scanf() fixes from kfrolov: 1) sscanf() function hangs in the following example: sscanf(2, %u,%u,%u,%u, ...), 2) sscanf() returns incorrect number of parsed numbers if some arguments can't be parsed: sscanf(=2, %u,%u,%u,%u,...)==1 instead of 0, and 3) using of char* instead of const char* in vsscanf function leads to warnings from GCC.
2013-10-21 18:41:15 +02:00
|
|
|
int errsave;
|
|
|
|
double_t dvalue;
|
|
|
|
|
2014-03-30 19:33:47 +02:00
|
|
|
/* Preserve the errno value */
|
|
|
|
|
|
|
|
errsave = get_errno();
|
2013-12-11 21:54:22 +01:00
|
|
|
set_errno(0);
|
scanf() fixes from kfrolov: 1) sscanf() function hangs in the following example: sscanf(2, %u,%u,%u,%u, ...), 2) sscanf() returns incorrect number of parsed numbers if some arguments can't be parsed: sscanf(=2, %u,%u,%u,%u,...)==1 instead of 0, and 3) using of char* instead of const char* in vsscanf function leads to warnings from GCC.
2013-10-21 18:41:15 +02:00
|
|
|
dvalue = strtod(tmp, &endptr);
|
|
|
|
|
2014-04-02 17:15:40 +02:00
|
|
|
/* Check if the number was successfully converted */
|
scanf() fixes from kfrolov: 1) sscanf() function hangs in the following example: sscanf(2, %u,%u,%u,%u, ...), 2) sscanf() returns incorrect number of parsed numbers if some arguments can't be parsed: sscanf(=2, %u,%u,%u,%u,...)==1 instead of 0, and 3) using of char* instead of const char* in vsscanf function leads to warnings from GCC.
2013-10-21 18:41:15 +02:00
|
|
|
|
2014-03-30 19:33:47 +02:00
|
|
|
if (tmp == endptr || get_errno() == ERANGE)
|
scanf() fixes from kfrolov: 1) sscanf() function hangs in the following example: sscanf(2, %u,%u,%u,%u, ...), 2) sscanf() returns incorrect number of parsed numbers if some arguments can't be parsed: sscanf(=2, %u,%u,%u,%u,...)==1 instead of 0, and 3) using of char* instead of const char* in vsscanf function leads to warnings from GCC.
2013-10-21 18:41:15 +02:00
|
|
|
{
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2013-12-11 21:54:22 +01:00
|
|
|
set_errno(errsave);
|
scanf() fixes from kfrolov: 1) sscanf() function hangs in the following example: sscanf(2, %u,%u,%u,%u, ...), 2) sscanf() returns incorrect number of parsed numbers if some arguments can't be parsed: sscanf(=2, %u,%u,%u,%u,...)==1 instead of 0, and 3) using of char* instead of const char* in vsscanf function leads to warnings from GCC.
2013-10-21 18:41:15 +02:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
/* We have to check whether we need to return a float
|
|
|
|
* or a double.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_HAVE_DOUBLE
|
2013-03-18 01:41:42 +01:00
|
|
|
if (lflag)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
linfo("vsscanf: Return %f to %p\n", dvalue, pd);
|
2013-03-18 01:41:42 +01:00
|
|
|
*pd = dvalue;
|
|
|
|
}
|
|
|
|
else
|
2012-11-10 17:06:01 +01:00
|
|
|
#endif
|
2013-03-18 01:41:42 +01:00
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
linfo("vsscanf: Return %f to %p\n", dvalue, pf);
|
2013-03-18 01:41:42 +01:00
|
|
|
*pf = (float)dvalue;
|
|
|
|
}
|
2014-03-30 22:24:38 +02:00
|
|
|
|
|
|
|
count++;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process %n: Character count */
|
|
|
|
|
|
|
|
else if (*fmt == 'n')
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
linfo("vsscanf: Performing character count\n");
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
if (!noassign)
|
|
|
|
{
|
|
|
|
size_t nchars = (size_t)(buf - bufstart);
|
|
|
|
|
2014-03-30 22:24:38 +02:00
|
|
|
/* Note %n does not count as a conversion */
|
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
if (lflag)
|
|
|
|
{
|
2015-10-12 15:45:02 +02:00
|
|
|
FAR long *plong = va_arg(ap, FAR long *);
|
2012-11-10 17:06:01 +01:00
|
|
|
*plong = (long)nchars;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-12 15:45:02 +02:00
|
|
|
FAR int *pint = va_arg(ap, FAR int *);
|
2012-11-10 17:06:01 +01:00
|
|
|
*pint = (int)nchars;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
width = 0;
|
|
|
|
noassign = false;
|
|
|
|
lflag = false;
|
|
|
|
|
|
|
|
fmt++;
|
|
|
|
}
|
|
|
|
|
scanf() fixes from kfrolov: 1) sscanf() function hangs in the following example: sscanf(2, %u,%u,%u,%u, ...), 2) sscanf() returns incorrect number of parsed numbers if some arguments can't be parsed: sscanf(=2, %u,%u,%u,%u,...)==1 instead of 0, and 3) using of char* instead of const char* in vsscanf function leads to warnings from GCC.
2013-10-21 18:41:15 +02:00
|
|
|
/* It is not a conversion specifier */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
else if (*buf)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2013-03-18 01:41:42 +01:00
|
|
|
/* Skip over any leading spaces in the input buffer */
|
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
while (isspace(*buf))
|
|
|
|
{
|
|
|
|
buf++;
|
|
|
|
}
|
|
|
|
|
2013-03-18 01:41:42 +01:00
|
|
|
/* Skip over matching characters in the buffer and format */
|
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
if (*fmt != *buf)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fmt++;
|
|
|
|
buf++;
|
|
|
|
}
|
|
|
|
}
|
scanf() fixes from kfrolov: 1) sscanf() function hangs in the following example: sscanf(2, %u,%u,%u,%u, ...), 2) sscanf() returns incorrect number of parsed numbers if some arguments can't be parsed: sscanf(=2, %u,%u,%u,%u,...)==1 instead of 0, and 3) using of char* instead of const char* in vsscanf function leads to warnings from GCC.
2013-10-21 18:41:15 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* NULL terminator encountered */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
|
2014-03-30 22:24:38 +02:00
|
|
|
/* sscanf is required to return EOF if the input ends before the first
|
|
|
|
* matching failure or conversion.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return count ? count : EOF;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|