2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* libs/libc/stdlib/lib_strtoull.c
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <nuttx/compiler.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2016-06-13 16:49:46 +02:00
|
|
|
#include <errno.h>
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2015-12-30 00:31:17 +01:00
|
|
|
#include "libc.h"
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_HAVE_LONG_LONG
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: strtoull
|
|
|
|
*
|
|
|
|
* Description:
|
2016-11-10 13:09:57 +01:00
|
|
|
* The strtoull() function converts the initial part of the string in
|
2012-11-10 17:06:01 +01: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-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2016-06-13 15:29:05 +02:00
|
|
|
* - The converted value, if the base and number are valid
|
2016-11-10 13:09:57 +01:00
|
|
|
* - 0 if an error occurs, and set errno to:
|
2016-06-13 15:29:05 +02:00
|
|
|
* * EINVAL if base < 2 or base > 36
|
2016-11-10 13:09:57 +01:00
|
|
|
* - ULLONG_MAX if an overflow occurs, and set errno to:
|
2016-06-13 15:29:05 +02:00
|
|
|
* * ERANGE if the number cannot be represented using unsigned long long
|
|
|
|
*
|
2012-11-10 17:06:01 +01:00
|
|
|
****************************************************************************/
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2020-11-11 08:49:04 +01:00
|
|
|
unsigned long long strtoull(FAR const char *nptr,
|
|
|
|
FAR char **endptr, int base)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2016-06-13 16:21:06 +02:00
|
|
|
unsigned long long accum = 0;
|
2020-11-11 08:49:04 +01:00
|
|
|
unsigned long long limit;
|
2012-11-10 17:06:01 +01:00
|
|
|
int value;
|
2020-11-11 08:49:04 +01:00
|
|
|
int last_digit;
|
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 14:03:02 +01:00
|
|
|
char sign = 0;
|
2012-11-10 17:06:01 +01: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 14:03:02 +01:00
|
|
|
/* Check for leading + or - already done for strtol */
|
|
|
|
|
|
|
|
if (*nptr == '-' || *nptr == '+')
|
|
|
|
{
|
|
|
|
sign = *nptr;
|
|
|
|
nptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for unspecified or incorrect base */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
base = lib_checkbase(base, &nptr);
|
|
|
|
|
2016-06-13 15:29:05 +02: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 14:03:02 +01:00
|
|
|
accum = 0;
|
2016-06-13 15:29:05 +02: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 14:03:02 +01:00
|
|
|
else
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2020-11-11 08:49:04 +01:00
|
|
|
limit = ULLONG_MAX / base;
|
|
|
|
last_digit = ULLONG_MAX % base;
|
|
|
|
|
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 14:03:02 +01:00
|
|
|
/* Accumulate each "digit" */
|
2016-06-13 15:29:05 +02: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 14:03:02 +01:00
|
|
|
while (lib_isbasedigit(*nptr, base, &value))
|
|
|
|
{
|
|
|
|
/* Check for overflow */
|
|
|
|
|
2020-11-11 08:49:04 +01:00
|
|
|
if (accum > limit || (accum == limit && value > last_digit))
|
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 14:03:02 +01:00
|
|
|
{
|
|
|
|
set_errno(ERANGE);
|
|
|
|
accum = ULLONG_MAX;
|
|
|
|
break;
|
|
|
|
}
|
2020-11-11 08:49:04 +01:00
|
|
|
|
|
|
|
accum = accum * base + value;
|
|
|
|
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 14:03:02 +01:00
|
|
|
}
|
2016-06-13 15:29:05 +02: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 14:03:02 +01:00
|
|
|
if (sign == '-')
|
2016-06-13 15:29:05 +02: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 14:03:02 +01:00
|
|
|
accum = (~accum) + 1;
|
2016-06-13 15:29:05 +02:00
|
|
|
}
|
2012-11-10 17:06:01 +01: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 14:03:02 +01:00
|
|
|
}
|
2012-11-10 17:06:01 +01: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 14:03:02 +01:00
|
|
|
/* Return the final pointer to the unused value */
|
2012-11-10 17:06:01 +01: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 14:03:02 +01:00
|
|
|
if (endptr)
|
|
|
|
{
|
|
|
|
if (sign)
|
2012-11-10 17:06:01 +01: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 14:03:02 +01:00
|
|
|
if (*(nptr - 1) == sign)
|
|
|
|
{
|
|
|
|
nptr--;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
2019-02-14 22:57:15 +01: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 14:03:02 +01:00
|
|
|
*endptr = (FAR char *)nptr;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
2016-06-13 16:21:06 +02:00
|
|
|
|
|
|
|
return accum;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
|
2015-11-18 20:22:43 +01:00
|
|
|
#endif
|