2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/stdlib/lib_strtol.c
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-04-02 09:22:52 +02: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-04-02 09:22:52 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-04-02 09:22:52 +02: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 <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
2016-11-10 13:09:57 +01: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
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: strtol
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The strtol() function converts the initial part of the string in
|
|
|
|
* nptr to a long 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-11-10 13:09:57 +01:00
|
|
|
* - The converted value, if the base and number are valid
|
|
|
|
* - 0 if an error occurs, and set errno to:
|
|
|
|
* * EINVAL if base < 2 or base > 36
|
|
|
|
* - LONG_MIN or LONG_MAX, of correct sign, if an overflow occurs,
|
|
|
|
* and set errno to:
|
|
|
|
* * ERANGE if the number cannot be represented using long
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2015-11-18 20:22:43 +01:00
|
|
|
long strtol(FAR const char *nptr, FAR char **endptr, int base)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
|
|
|
unsigned long accum = 0;
|
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
|
|
|
long retval = 0;
|
|
|
|
char sign = 0;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
if (nptr)
|
|
|
|
{
|
|
|
|
/* Skip leading spaces */
|
|
|
|
|
|
|
|
lib_skipspace(&nptr);
|
|
|
|
|
|
|
|
/* Check for leading + or - */
|
|
|
|
|
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 == '-' || *nptr == '+')
|
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
|
|
|
sign = *nptr;
|
2012-11-10 17:06:01 +01:00
|
|
|
nptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the unsigned value */
|
|
|
|
|
|
|
|
accum = strtoul(nptr, endptr, base);
|
|
|
|
|
2016-11-10 13:09:57 +01:00
|
|
|
/* Correct the sign of the result and check for overflow */
|
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 (sign == '-')
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2016-11-10 13:09:57 +01:00
|
|
|
const unsigned long limit = ((unsigned long)-(LONG_MIN + 1)) + 1;
|
|
|
|
|
|
|
|
if (accum > limit)
|
|
|
|
{
|
|
|
|
set_errno(ERANGE);
|
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
|
|
|
retval = LONG_MIN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
retval = (accum == limit) ? LONG_MIN : -(long)accum;
|
2016-11-10 13:09:57 +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
|
|
|
else
|
|
|
|
{
|
|
|
|
if (accum > LONG_MAX)
|
|
|
|
{
|
|
|
|
set_errno(ERANGE);
|
|
|
|
retval = LONG_MAX;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
retval = accum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the final pointer to the unused value */
|
2016-11-10 13:09:57 +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)
|
2016-11-10 13:09:57 +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) - 1) == sign)
|
|
|
|
{
|
|
|
|
(*endptr)--;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
2014-01-16 15:03:26 +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 retval;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|