2012-11-11 00:06:01 +08:00
|
|
|
/****************************************************************************
|
2018-05-30 03:21:26 +08:00
|
|
|
* /libs/libc/stdlib/lib_strtoul.c
|
2012-11-11 00:06:01 +08:00
|
|
|
*
|
Squashed commit of the following:
Author: Gregory Nutt <gnutt@nuttx.org>
TODO: Remove 'Missing fscanf()' bug
Clean up remaining complaints for tools/nxstyle
Apply tools/detab, rmcr, convert-comments, lowhex, and indent.sh to the new and highly modified files.
Author: Johannes <nivus.entwicklung@gmail.com>
- Move vscanf logic to lib_sscanf.c Switched to stream interface (tricky, because the old implementation used massive read ahead, which isn't suitable for streams, chars already read are gone).
- Added scanf and fscanf
- Added hh, h, and ll modifiers
- Fixes for standard compliance in scanf
- Fixes for standard compliance in strto... function family (don't consume single '-' or '+', allow sign in strotul(l))
2019-02-14 21:03:02 +08:00
|
|
|
* Copyright (C) 2007, 2009, 2011, 2016-2017, 2019 Gregory Nutt.
|
|
|
|
* All rights reserved.
|
2012-11-11 00:06:01 +08: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/config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2016-06-13 22:49:46 +08:00
|
|
|
#include <errno.h>
|
2012-11-11 00:06:01 +08:00
|
|
|
|
2015-12-30 07:31:17 +08:00
|
|
|
#include "libc.h"
|
2012-11-11 00:06:01 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: strtoul
|
|
|
|
*
|
|
|
|
* Description:
|
2016-11-10 20:09:57 +08:00
|
|
|
* The strtoul() function converts the initial part of the string in
|
2012-11-11 00:06:01 +08:00
|
|
|
* nptr to a long unsigned integer value according to the given base, which
|
|
|
|
* must be between 2 and 36 inclusive, or be the special value 0.
|
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2016-06-13 21:29:05 +08:00
|
|
|
* - The converted value, if the base and number are valid
|
2016-11-10 20:09:57 +08:00
|
|
|
* - 0 if an error occurs, and set errno to:
|
2016-06-13 21:29:05 +08:00
|
|
|
* * EINVAL if base < 2 or base > 36
|
2016-11-10 20:09:57 +08:00
|
|
|
* - ULONG_MAX if an overflow occurs, and set errno to:
|
2016-06-13 21:29:05 +08:00
|
|
|
* * ERANGE if the number cannot be represented using unsigned long
|
2012-11-11 00:06:01 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
2014-04-14 04:32:20 +08:00
|
|
|
|
2015-11-19 03:22:43 +08:00
|
|
|
unsigned long strtoul(FAR const char *nptr, FAR char **endptr, int base)
|
2012-11-11 00:06:01 +08:00
|
|
|
{
|
2016-06-13 22:21:06 +08:00
|
|
|
unsigned long accum = 0;
|
|
|
|
unsigned long prev;
|
2012-11-11 00:06:01 +08:00
|
|
|
int value;
|
Squashed commit of the following:
Author: Gregory Nutt <gnutt@nuttx.org>
TODO: Remove 'Missing fscanf()' bug
Clean up remaining complaints for tools/nxstyle
Apply tools/detab, rmcr, convert-comments, lowhex, and indent.sh to the new and highly modified files.
Author: Johannes <nivus.entwicklung@gmail.com>
- Move vscanf logic to lib_sscanf.c Switched to stream interface (tricky, because the old implementation used massive read ahead, which isn't suitable for streams, chars already read are gone).
- Added scanf and fscanf
- Added hh, h, and ll modifiers
- Fixes for standard compliance in scanf
- Fixes for standard compliance in strto... function family (don't consume single '-' or '+', allow sign in strotul(l))
2019-02-14 21:03:02 +08:00
|
|
|
char sign = 0;
|
2012-11-11 00:06:01 +08:00
|
|
|
|
|
|
|
if (nptr)
|
|
|
|
{
|
|
|
|
/* Skip leading spaces */
|
|
|
|
|
|
|
|
lib_skipspace(&nptr);
|
|
|
|
|
Squashed commit of the following:
Author: Gregory Nutt <gnutt@nuttx.org>
TODO: Remove 'Missing fscanf()' bug
Clean up remaining complaints for tools/nxstyle
Apply tools/detab, rmcr, convert-comments, lowhex, and indent.sh to the new and highly modified files.
Author: Johannes <nivus.entwicklung@gmail.com>
- Move vscanf logic to lib_sscanf.c Switched to stream interface (tricky, because the old implementation used massive read ahead, which isn't suitable for streams, chars already read are gone).
- Added scanf and fscanf
- Added hh, h, and ll modifiers
- Fixes for standard compliance in scanf
- Fixes for standard compliance in strto... function family (don't consume single '-' or '+', allow sign in strotul(l))
2019-02-14 21:03:02 +08:00
|
|
|
/* Check for leading + or - already done for strtol */
|
|
|
|
|
|
|
|
if (*nptr == '-' || *nptr == '+')
|
|
|
|
{
|
|
|
|
sign = *nptr;
|
|
|
|
nptr++;
|
|
|
|
}
|
|
|
|
|
2016-06-13 21:29:05 +08:00
|
|
|
/* Check for unspecified or incorrect base */
|
2012-11-11 00:06:01 +08:00
|
|
|
|
|
|
|
base = lib_checkbase(base, &nptr);
|
|
|
|
|
2016-06-13 21:29:05 +08:00
|
|
|
if (base < 0)
|
|
|
|
{
|
|
|
|
set_errno(EINVAL);
|
Squashed commit of the following:
Author: Gregory Nutt <gnutt@nuttx.org>
TODO: Remove 'Missing fscanf()' bug
Clean up remaining complaints for tools/nxstyle
Apply tools/detab, rmcr, convert-comments, lowhex, and indent.sh to the new and highly modified files.
Author: Johannes <nivus.entwicklung@gmail.com>
- Move vscanf logic to lib_sscanf.c Switched to stream interface (tricky, because the old implementation used massive read ahead, which isn't suitable for streams, chars already read are gone).
- Added scanf and fscanf
- Added hh, h, and ll modifiers
- Fixes for standard compliance in scanf
- Fixes for standard compliance in strto... function family (don't consume single '-' or '+', allow sign in strotul(l))
2019-02-14 21:03:02 +08:00
|
|
|
accum = 0;
|
2016-06-13 21:29:05 +08:00
|
|
|
}
|
Squashed commit of the following:
Author: Gregory Nutt <gnutt@nuttx.org>
TODO: Remove 'Missing fscanf()' bug
Clean up remaining complaints for tools/nxstyle
Apply tools/detab, rmcr, convert-comments, lowhex, and indent.sh to the new and highly modified files.
Author: Johannes <nivus.entwicklung@gmail.com>
- Move vscanf logic to lib_sscanf.c Switched to stream interface (tricky, because the old implementation used massive read ahead, which isn't suitable for streams, chars already read are gone).
- Added scanf and fscanf
- Added hh, h, and ll modifiers
- Fixes for standard compliance in scanf
- Fixes for standard compliance in strto... function family (don't consume single '-' or '+', allow sign in strotul(l))
2019-02-14 21:03:02 +08:00
|
|
|
else
|
2012-11-11 00:06:01 +08:00
|
|
|
{
|
Squashed commit of the following:
Author: Gregory Nutt <gnutt@nuttx.org>
TODO: Remove 'Missing fscanf()' bug
Clean up remaining complaints for tools/nxstyle
Apply tools/detab, rmcr, convert-comments, lowhex, and indent.sh to the new and highly modified files.
Author: Johannes <nivus.entwicklung@gmail.com>
- Move vscanf logic to lib_sscanf.c Switched to stream interface (tricky, because the old implementation used massive read ahead, which isn't suitable for streams, chars already read are gone).
- Added scanf and fscanf
- Added hh, h, and ll modifiers
- Fixes for standard compliance in scanf
- Fixes for standard compliance in strto... function family (don't consume single '-' or '+', allow sign in strotul(l))
2019-02-14 21:03:02 +08:00
|
|
|
/* Accumulate each "digit" */
|
2016-06-13 21:29:05 +08:00
|
|
|
|
Squashed commit of the following:
Author: Gregory Nutt <gnutt@nuttx.org>
TODO: Remove 'Missing fscanf()' bug
Clean up remaining complaints for tools/nxstyle
Apply tools/detab, rmcr, convert-comments, lowhex, and indent.sh to the new and highly modified files.
Author: Johannes <nivus.entwicklung@gmail.com>
- Move vscanf logic to lib_sscanf.c Switched to stream interface (tricky, because the old implementation used massive read ahead, which isn't suitable for streams, chars already read are gone).
- Added scanf and fscanf
- Added hh, h, and ll modifiers
- Fixes for standard compliance in scanf
- Fixes for standard compliance in strto... function family (don't consume single '-' or '+', allow sign in strotul(l))
2019-02-14 21:03:02 +08:00
|
|
|
while (lib_isbasedigit(*nptr, base, &value))
|
|
|
|
{
|
|
|
|
prev = accum;
|
|
|
|
accum = accum * base + value;
|
|
|
|
nptr++;
|
|
|
|
|
|
|
|
/* Check for overflow */
|
|
|
|
|
|
|
|
if (accum < prev)
|
|
|
|
{
|
|
|
|
set_errno(ERANGE);
|
|
|
|
accum = ULONG_MAX;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-06-13 21:29:05 +08:00
|
|
|
|
Squashed commit of the following:
Author: Gregory Nutt <gnutt@nuttx.org>
TODO: Remove 'Missing fscanf()' bug
Clean up remaining complaints for tools/nxstyle
Apply tools/detab, rmcr, convert-comments, lowhex, and indent.sh to the new and highly modified files.
Author: Johannes <nivus.entwicklung@gmail.com>
- Move vscanf logic to lib_sscanf.c Switched to stream interface (tricky, because the old implementation used massive read ahead, which isn't suitable for streams, chars already read are gone).
- Added scanf and fscanf
- Added hh, h, and ll modifiers
- Fixes for standard compliance in scanf
- Fixes for standard compliance in strto... function family (don't consume single '-' or '+', allow sign in strotul(l))
2019-02-14 21:03:02 +08:00
|
|
|
if (sign == '-')
|
2016-06-13 21:29:05 +08:00
|
|
|
{
|
Squashed commit of the following:
Author: Gregory Nutt <gnutt@nuttx.org>
TODO: Remove 'Missing fscanf()' bug
Clean up remaining complaints for tools/nxstyle
Apply tools/detab, rmcr, convert-comments, lowhex, and indent.sh to the new and highly modified files.
Author: Johannes <nivus.entwicklung@gmail.com>
- Move vscanf logic to lib_sscanf.c Switched to stream interface (tricky, because the old implementation used massive read ahead, which isn't suitable for streams, chars already read are gone).
- Added scanf and fscanf
- Added hh, h, and ll modifiers
- Fixes for standard compliance in scanf
- Fixes for standard compliance in strto... function family (don't consume single '-' or '+', allow sign in strotul(l))
2019-02-14 21:03:02 +08:00
|
|
|
accum = (~accum) + 1;
|
2016-06-13 21:29:05 +08:00
|
|
|
}
|
2012-11-11 00:06:01 +08:00
|
|
|
}
|
Squashed commit of the following:
Author: Gregory Nutt <gnutt@nuttx.org>
TODO: Remove 'Missing fscanf()' bug
Clean up remaining complaints for tools/nxstyle
Apply tools/detab, rmcr, convert-comments, lowhex, and indent.sh to the new and highly modified files.
Author: Johannes <nivus.entwicklung@gmail.com>
- Move vscanf logic to lib_sscanf.c Switched to stream interface (tricky, because the old implementation used massive read ahead, which isn't suitable for streams, chars already read are gone).
- Added scanf and fscanf
- Added hh, h, and ll modifiers
- Fixes for standard compliance in scanf
- Fixes for standard compliance in strto... function family (don't consume single '-' or '+', allow sign in strotul(l))
2019-02-14 21:03:02 +08:00
|
|
|
}
|
2012-11-11 00:06:01 +08:00
|
|
|
|
Squashed commit of the following:
Author: Gregory Nutt <gnutt@nuttx.org>
TODO: Remove 'Missing fscanf()' bug
Clean up remaining complaints for tools/nxstyle
Apply tools/detab, rmcr, convert-comments, lowhex, and indent.sh to the new and highly modified files.
Author: Johannes <nivus.entwicklung@gmail.com>
- Move vscanf logic to lib_sscanf.c Switched to stream interface (tricky, because the old implementation used massive read ahead, which isn't suitable for streams, chars already read are gone).
- Added scanf and fscanf
- Added hh, h, and ll modifiers
- Fixes for standard compliance in scanf
- Fixes for standard compliance in strto... function family (don't consume single '-' or '+', allow sign in strotul(l))
2019-02-14 21:03:02 +08:00
|
|
|
/* Return the final pointer to the unused value */
|
2012-11-11 00:06:01 +08:00
|
|
|
|
Squashed commit of the following:
Author: Gregory Nutt <gnutt@nuttx.org>
TODO: Remove 'Missing fscanf()' bug
Clean up remaining complaints for tools/nxstyle
Apply tools/detab, rmcr, convert-comments, lowhex, and indent.sh to the new and highly modified files.
Author: Johannes <nivus.entwicklung@gmail.com>
- Move vscanf logic to lib_sscanf.c Switched to stream interface (tricky, because the old implementation used massive read ahead, which isn't suitable for streams, chars already read are gone).
- Added scanf and fscanf
- Added hh, h, and ll modifiers
- Fixes for standard compliance in scanf
- Fixes for standard compliance in strto... function family (don't consume single '-' or '+', allow sign in strotul(l))
2019-02-14 21:03:02 +08:00
|
|
|
if (endptr)
|
|
|
|
{
|
|
|
|
if (sign)
|
2012-11-11 00:06:01 +08:00
|
|
|
{
|
Squashed commit of the following:
Author: Gregory Nutt <gnutt@nuttx.org>
TODO: Remove 'Missing fscanf()' bug
Clean up remaining complaints for tools/nxstyle
Apply tools/detab, rmcr, convert-comments, lowhex, and indent.sh to the new and highly modified files.
Author: Johannes <nivus.entwicklung@gmail.com>
- Move vscanf logic to lib_sscanf.c Switched to stream interface (tricky, because the old implementation used massive read ahead, which isn't suitable for streams, chars already read are gone).
- Added scanf and fscanf
- Added hh, h, and ll modifiers
- Fixes for standard compliance in scanf
- Fixes for standard compliance in strto... function family (don't consume single '-' or '+', allow sign in strotul(l))
2019-02-14 21:03:02 +08:00
|
|
|
if (*(nptr - 1) == sign)
|
|
|
|
{
|
|
|
|
nptr--;
|
|
|
|
}
|
2012-11-11 00:06:01 +08:00
|
|
|
}
|
2019-02-15 05:57:15 +08:00
|
|
|
|
Squashed commit of the following:
Author: Gregory Nutt <gnutt@nuttx.org>
TODO: Remove 'Missing fscanf()' bug
Clean up remaining complaints for tools/nxstyle
Apply tools/detab, rmcr, convert-comments, lowhex, and indent.sh to the new and highly modified files.
Author: Johannes <nivus.entwicklung@gmail.com>
- Move vscanf logic to lib_sscanf.c Switched to stream interface (tricky, because the old implementation used massive read ahead, which isn't suitable for streams, chars already read are gone).
- Added scanf and fscanf
- Added hh, h, and ll modifiers
- Fixes for standard compliance in scanf
- Fixes for standard compliance in strto... function family (don't consume single '-' or '+', allow sign in strotul(l))
2019-02-14 21:03:02 +08:00
|
|
|
*endptr = (FAR char *)nptr;
|
2012-11-11 00:06:01 +08:00
|
|
|
}
|
2013-03-28 00:24:45 +08:00
|
|
|
|
2014-01-16 22:03:26 +08:00
|
|
|
return accum;
|
2012-11-11 00:06:01 +08:00
|
|
|
}
|