2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2014-01-16 15:03:26 +01:00
|
|
|
* /libc/stdlib/lib_strtoull.c
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2016-06-13 16:21:06 +02:00
|
|
|
* Copyright (C) 2009, 2010, 2016 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/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.
|
|
|
|
*
|
2016-06-13 15:29:05 +02:00
|
|
|
* Returns:
|
|
|
|
* - 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
|
|
|
|
2015-11-18 20:22:43 +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;
|
|
|
|
unsigned long long prev;
|
2012-11-10 17:06:01 +01:00
|
|
|
int value;
|
|
|
|
|
|
|
|
if (nptr)
|
|
|
|
{
|
|
|
|
/* Skip leading spaces */
|
|
|
|
|
|
|
|
lib_skipspace(&nptr);
|
|
|
|
|
|
|
|
/* Check for unspecified base */
|
|
|
|
|
|
|
|
base = lib_checkbase(base, &nptr);
|
|
|
|
|
2016-06-13 15:29:05 +02:00
|
|
|
if (base < 0)
|
|
|
|
{
|
|
|
|
set_errno(EINVAL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
/* Accumulate each "digit" */
|
|
|
|
|
|
|
|
while (lib_isbasedigit(*nptr, base, &value))
|
|
|
|
{
|
2016-06-13 15:29:05 +02:00
|
|
|
prev = accum;
|
|
|
|
accum = accum*base + value;
|
|
|
|
nptr++;
|
|
|
|
|
|
|
|
/* Check for overflow */
|
|
|
|
|
|
|
|
if (accum < prev)
|
|
|
|
{
|
2016-06-13 16:21:06 +02:00
|
|
|
set_errno(ERANGE);
|
2016-11-10 13:09:57 +01:00
|
|
|
accum = ULLONG_MAX;
|
2016-06-13 16:21:06 +02:00
|
|
|
break;
|
2016-06-13 15:29:05 +02:00
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the final pointer to the unused value */
|
|
|
|
|
|
|
|
if (endptr)
|
|
|
|
{
|
|
|
|
*endptr = (char *)nptr;
|
|
|
|
}
|
|
|
|
}
|
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
|