diff options
Diffstat (limited to 'cpukit/libmisc/stringto')
-rw-r--r-- | cpukit/libmisc/stringto/stringto.h | 246 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtodouble.c | 58 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtofloat.c | 58 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtoint.c | 73 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtolong.c | 59 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtolongdouble.c | 58 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtolonglong.c | 68 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtopointer.c | 47 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtounsignedchar.c | 66 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtounsignedint.c | 66 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtounsignedlong.c | 59 | ||||
-rw-r--r-- | cpukit/libmisc/stringto/stringtounsignedlonglong.c | 64 |
12 files changed, 922 insertions, 0 deletions
diff --git a/cpukit/libmisc/stringto/stringto.h b/cpukit/libmisc/stringto/stringto.h new file mode 100644 index 0000000000..dfb1617bc9 --- /dev/null +++ b/cpukit/libmisc/stringto/stringto.h @@ -0,0 +1,246 @@ +/* + * COPYRIGHT (c) 2009. + * On-Line Applications Research Corporation (OAR). + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + * + * $Id$ + */ + +#ifndef _RTEMS_STRINGTO_H +#define _RTEMS_STRINGTO_H + +#include <rtems.h> + +/** + * @brief Convert String to Pointer (with validation) + * + * This method converts a string to a pointer (void *) with + * basic numeric validation. + * + * @param[in] s is the string to convert + * @param[in] n points to the variable to place the converted output in + * @param[in] endptr is used to keep track of the position in the string + * + * @return This method returns RTEMS_SUCCESSFUL on successful conversion + * and *n is filled in. Otherwise, the status indicates the + * source of the error. + */ +rtems_status_code rtems_string_to_pointer( + const char *s, + void **n, + char **endptr +); + +/** + * @brief Convert String to Unsigned Character (with validation) + * + * This method converts a string to an unsigned character with + * range validation. + * + * @param[in] s is the string to convert + * @param[in] n points to the variable to place the converted output in + * @param[in] endptr is used to keep track of the position in the string + * @param[in] base is the expected base of the number + * + * @return This method returns RTEMS_SUCCESSFUL on successful conversion + * and *n is filled in. Otherwise, the status indicates the + * source of the error. + */ +rtems_status_code rtems_string_to_unsigned_char( + const char *s, + unsigned char *n, + char **endptr, + int base +); + +/** + * @brief Convert String to Int (with validation) + * + * This method converts a string to an int with range validation. + * + * @param[in] s is the string to convert + * @param[in] n points to the variable to place the converted output in + * @param[in] endptr is used to keep track of the position in the string + * @param[in] base is the expected base of the number + * + * @return This method returns RTEMS_SUCCESSFUL on successful conversion + * and *n is filled in. Otherwise, the status indicates the + * source of the error. + */ +rtems_status_code rtems_string_to_int( + const char *s, + int *n, + char **endptr, + int base +); + +/** + * @brief Convert String to Unsigned Int (with validation) + * + * This method converts a string to an unsigned int with range validation. + * + * @param[in] s is the string to convert + * @param[in] n points to the variable to place the converted output in + * @param[in] endptr is used to keep track of the position in the string + * @param[in] base is the expected base of the number + * + * @return This method returns RTEMS_SUCCESSFUL on successful conversion + * and *n is filled in. Otherwise, the status indicates the + * source of the error. + */ +rtems_status_code rtems_string_to_unsigned_int( + const char *s, + unsigned int *n, + char **endptr, + int base +); + +/** + * @brief Convert String to Long (with validation) + * + * This method converts a string to a long with + * range validation. + * + * @param[in] s is the string to convert + * @param[in] n points to the variable to place the converted output in + * @param[in] endptr is used to keep track of the position in the string + * @param[in] base is the expected base of the number + * + * @return This method returns RTEMS_SUCCESSFUL on successful conversion + * and *n is filled in. Otherwise, the status indicates the + * source of the error. + */ +rtems_status_code rtems_string_to_long( + const char *s, + long *n, + char **endptr, + int base +); + +/** + * @brief Convert String to Unsigned Long (with validation) + * + * This method converts a string to an unsigned long with + * range validation. + * + * @param[in] s is the string to convert + * @param[in] n points to the variable to place the converted output in + * @param[in] endptr is used to keep track of the position in the string + * @param[in] base is the expected base of the number + * + * @return This method returns RTEMS_SUCCESSFUL on successful conversion + * and *n is filled in. Otherwise, the status indicates the + * source of the error. + */ +rtems_status_code rtems_string_to_unsigned_long( + const char *s, + unsigned long *n, + char **endptr, + int base +); + +/** + * @brief Convert String to Long Long (with validation) + * + * This method converts a string to a long long with + * range validation. + * + * @param[in] s is the string to convert + * @param[in] n points to the variable to place the converted output in + * @param[in] endptr is used to keep track of the position in the string + * @param[in] base is the expected base of the number + * + * @return This method returns RTEMS_SUCCESSFUL on successful conversion + * and *n is filled in. Otherwise, the status indicates the + * source of the error. + */ +rtems_status_code rtems_string_to_long_long( + const char *s, + long long *n, + char **endptr, + int base +); + +/** + * @brief Convert String to Unsigned Long Long (with validation) + * + * This method converts a string to an unsigned character with + * range validation. + * + * @param[in] s is the string to convert + * @param[in] n points to the variable to place the converted output in + * @param[in] endptr is used to keep track of the position in the string + * @param[in] base is the expected base of the number + * + * @return This method returns RTEMS_SUCCESSFUL on successful conversion + * and *n is filled in. Otherwise, the status indicates the + * source of the error. + */ +rtems_status_code rtems_string_to_unsigned_long_long( + const char *s, + unsigned long long *n, + char **endptr, + int base +); + +/** + * @brief Convert String to Float (with validation) + * + * This method converts a string to a float with range validation. + * + * @param[in] s is the string to convert + * @param[in] n points to the variable to place the converted output in + * @param[in] endptr is used to keep track of the position in the string + * + * @return This method returns RTEMS_SUCCESSFUL on successful conversion + * and *n is filled in. Otherwise, the status indicates the + * source of the error. + */ +rtems_status_code rtems_string_to_float( + const char *s, + float *n, + char **endptr +); + +/** + * @brief Convert String to Double (with validation) + * + * This method converts a string to a double with range validation. + * + * @param[in] s is the string to convert + * @param[in] n points to the variable to place the converted output in + * @param[in] endptr is used to keep track of the position in the string + * + * @return This method returns RTEMS_SUCCESSFUL on successful conversion + * and *n is filled in. Otherwise, the status indicates the + * source of the error. + */ +rtems_status_code rtems_string_to_double( + const char *s, + double *n, + char **endptr +); + +/** + * @brief Convert String to long double (with validation) + * + * This method converts a string to a long double with range validation. + * + * @param[in] s is the string to convert + * @param[in] n points to the variable to place the converted output in + * @param[in] endptr is used to keep track of the position in the string + * + * @return This method returns RTEMS_SUCCESSFUL on successful conversion + * and *n is filled in. Otherwise, the status indicates the + * source of the error. + */ +rtems_status_code rtems_string_to_long_double( + const char *s, + long double *n, + char **endptr +); + +#endif diff --git a/cpukit/libmisc/stringto/stringtodouble.c b/cpukit/libmisc/stringto/stringtodouble.c new file mode 100644 index 0000000000..ec50a1332b --- /dev/null +++ b/cpukit/libmisc/stringto/stringtodouble.c @@ -0,0 +1,58 @@ +/* + * COPYRIGHT (c) 2009. + * On-Line Applications Research Corporation (OAR). + * + * Copyright (c) 2011 Ralf Corsépius, Ulm, Germany. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + * + * $Id$ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <errno.h> +#include <stdlib.h> +#include <math.h> + +#include <rtems/stringto.h> + +/* + * Instantiate an error checking wrapper for strtod (double) + */ + +rtems_status_code rtems_string_to_double ( + const char *s, + double *n, + char **endptr +) +{ + double result; + char *end; + + if ( !n ) + return RTEMS_INVALID_ADDRESS; + + errno = 0; + *n = 0; + + result = strtod( s, &end ); + + if ( endptr ) + *endptr = end; + + if ( end == s ) + return RTEMS_NOT_DEFINED; + + if ( ( errno == ERANGE ) && + (( result == 0 ) || ( result == HUGE_VAL ) || ( result == -HUGE_VAL ))) + return RTEMS_INVALID_NUMBER; + + *n = result; + + return RTEMS_SUCCESSFUL; +} diff --git a/cpukit/libmisc/stringto/stringtofloat.c b/cpukit/libmisc/stringto/stringtofloat.c new file mode 100644 index 0000000000..05ebe33566 --- /dev/null +++ b/cpukit/libmisc/stringto/stringtofloat.c @@ -0,0 +1,58 @@ +/* + * COPYRIGHT (c) 2009. + * On-Line Applications Research Corporation (OAR). + * + * Copyright (c) 2011 Ralf Corsépius, Ulm, Germany. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + * + * $Id$ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <errno.h> +#include <stdlib.h> +#include <math.h> + +#include <rtems/stringto.h> + +/* + * Instantiate an error checking wrapper for strtof (float) + */ + +rtems_status_code rtems_string_to_float ( + const char *s, + float *n, + char **endptr +) +{ + float result; + char *end; + + if ( !n ) + return RTEMS_INVALID_ADDRESS; + + errno = 0; + *n = 0; + + result = strtof( s, &end ); + + if ( endptr ) + *endptr = end; + + if ( end == s ) + return RTEMS_NOT_DEFINED; + + if ( ( errno == ERANGE ) && + (( result == 0 ) || ( result == HUGE_VALF ) || ( result == -HUGE_VALF ))) + return RTEMS_INVALID_NUMBER; + + *n = result; + + return RTEMS_SUCCESSFUL; +} diff --git a/cpukit/libmisc/stringto/stringtoint.c b/cpukit/libmisc/stringto/stringtoint.c new file mode 100644 index 0000000000..8e3663b496 --- /dev/null +++ b/cpukit/libmisc/stringto/stringtoint.c @@ -0,0 +1,73 @@ +/* + * COPYRIGHT (c) 2009. + * On-Line Applications Research Corporation (OAR). + * + * Copyright (c) 2011 Ralf Corsépius, Ulm, Germany. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + * + * $Id$ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <errno.h> +#include <stdlib.h> +#include <limits.h> + +#include <rtems/stringto.h> + +/* + * Instantiate an error checking wrapper for strtol (int) + */ + +rtems_status_code rtems_string_to_int ( + const char *s, + int *n, + char **endptr, + int base +) +{ + long result; + char *end; + + if ( !n ) + return RTEMS_INVALID_ADDRESS; + + errno = 0; + *n = 0; + + result = strtol( s, &end, base ); + + if ( endptr ) + *endptr = end; + + if ( end == s ) + return RTEMS_NOT_DEFINED; + + if ( ( errno == ERANGE ) && + (( result == 0 ) || ( result == LONG_MAX ) || ( result == LONG_MIN ))) + return RTEMS_INVALID_NUMBER; + +#if (INT_MAX < LONG_MAX) + if ( result > INT_MAX ) { + errno = ERANGE; + return RTEMS_INVALID_NUMBER; + } +#endif + +#if (INT_MIN < LONG_MIN) + if ( result < INT_MIN ) { + errno = ERANGE; + return RTEMS_INVALID_NUMBER; + } +#endif + + *n = result; + + return RTEMS_SUCCESSFUL; +} diff --git a/cpukit/libmisc/stringto/stringtolong.c b/cpukit/libmisc/stringto/stringtolong.c new file mode 100644 index 0000000000..e77b8822b8 --- /dev/null +++ b/cpukit/libmisc/stringto/stringtolong.c @@ -0,0 +1,59 @@ +/* + * COPYRIGHT (c) 2009. + * On-Line Applications Research Corporation (OAR). + * + * Copyright (c) 2011 Ralf Corsépius, Ulm, Germany. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + * + * $Id$ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <errno.h> +#include <stdlib.h> +#include <limits.h> + +#include <rtems/stringto.h> + +/* + * Instantiate an error checking wrapper for strtol (long) + */ + +rtems_status_code rtems_string_to_long ( + const char *s, + long *n, + char **endptr, + int base +) +{ + long result; + char *end; + + if ( !n ) + return RTEMS_INVALID_ADDRESS; + + errno = 0; + *n = 0; + + result = strtol( s, &end, base ); + + if ( endptr ) + *endptr = end; + + if ( end == s ) + return RTEMS_NOT_DEFINED; + + if ( ( errno == ERANGE ) && + (( result == 0 ) || ( result == LONG_MAX ) || ( result == LONG_MIN ))) + return RTEMS_INVALID_NUMBER; + + *n = result; + + return RTEMS_SUCCESSFUL; +} diff --git a/cpukit/libmisc/stringto/stringtolongdouble.c b/cpukit/libmisc/stringto/stringtolongdouble.c new file mode 100644 index 0000000000..776f5e183b --- /dev/null +++ b/cpukit/libmisc/stringto/stringtolongdouble.c @@ -0,0 +1,58 @@ +/* + * COPYRIGHT (c) 2009. + * On-Line Applications Research Corporation (OAR). + * + * Copyright (c) 2011 Ralf Corsépius, Ulm, Germany. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + * + * $Id$ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <errno.h> +#include <stdlib.h> +#include <math.h> + +#include <rtems/stringto.h> + +/* + * Instantiate an error checking wrapper for strtod (double) + */ + +rtems_status_code rtems_string_to_long_double ( + const char *s, + long double *n, + char **endptr +) +{ + long double result; + char *end; + + if ( !n ) + return RTEMS_INVALID_ADDRESS; + + errno = 0; + *n = 0; + + result = strtold( s, &end ); + + if ( endptr ) + *endptr = end; + + if ( end == s ) + return RTEMS_NOT_DEFINED; + + if ( ( errno == ERANGE ) && + (( result == 0 ) || ( result == HUGE_VALL ) || ( result == -HUGE_VALL ))) + return RTEMS_INVALID_NUMBER; + + *n = result; + + return RTEMS_SUCCESSFUL; +} diff --git a/cpukit/libmisc/stringto/stringtolonglong.c b/cpukit/libmisc/stringto/stringtolonglong.c new file mode 100644 index 0000000000..d291262477 --- /dev/null +++ b/cpukit/libmisc/stringto/stringtolonglong.c @@ -0,0 +1,68 @@ +/* + * COPYRIGHT (c) 2009. + * On-Line Applications Research Corporation (OAR). + * + * Copyright (c) 2011 Ralf Corsépius, Ulm, Germany. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + * + * $Id$ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <errno.h> +#include <stdlib.h> +#include <limits.h> + +#include <rtems/stringto.h> + +/* c99 has LLONG_MAX instead of LONG_LONG_MAX */ +#ifndef LONG_LONG_MAX +#define LONG_LONG_MAX LLONG_MAX +#endif +/* c99 has LLONG_MIN instead of LONG_LONG_MIN */ +#ifndef LONG_LONG_MIN +#define LONG_LONG_MIN LLONG_MIN +#endif + +/* + * Instantiate an error checking wrapper for strtoll (long long) + */ + +rtems_status_code rtems_string_to_long_long ( + const char *s, + long long *n, + char **endptr, + int base +) +{ + long long result; + char *end; + + if ( !n ) + return RTEMS_INVALID_ADDRESS; + + errno = 0; + *n = 0; + + result = strtoll( s, &end, base ); + + if ( endptr ) + *endptr = end; + + if ( end == s ) + return RTEMS_NOT_DEFINED; + + if ( ( errno == ERANGE ) && + (( result == 0 ) || ( result == LONG_LONG_MAX ) || ( result == LONG_LONG_MIN ))) + return RTEMS_INVALID_NUMBER; + + *n = result; + + return RTEMS_SUCCESSFUL; +} diff --git a/cpukit/libmisc/stringto/stringtopointer.c b/cpukit/libmisc/stringto/stringtopointer.c new file mode 100644 index 0000000000..3a99baa3b5 --- /dev/null +++ b/cpukit/libmisc/stringto/stringtopointer.c @@ -0,0 +1,47 @@ +/* + * COPYRIGHT (c) 2009. + * On-Line Applications Research Corporation (OAR). + * + * Copyright (c) 2011 Ralf Corsépius, Ulm, Germany. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + * + * $Id$ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <errno.h> +#include <stdlib.h> +#include <limits.h> +#include <stdint.h> + +#include <rtems/stringto.h> + +/* + * Instantiate an error checking wrapper for strtoul/strtoull (void *) + */ + +#if (UINTPTR_MAX == ULONG_MAX) +#define STRTOFUNC(a,b,c) rtems_string_to_unsigned_long(a, (unsigned long*) b, c, 0) +#elif (UINTPTR_MAX == ULONG_LONG_MAX) +#define STRTOFUNC(a,b,c) rtems_string_to_unsigned_long_long(a, (unsigned long long*) b, c, 0) +#elif (UINTPTR_MAX == UINT_MAX) +#define STRTOFUNC(a,b,c) rtems_string_to_unsigned_int(a, (unsigned int*) b, c, 0) +#else +/* Fallback to unsigned long */ +#define STRTOFUNC(a,b,c) rtems_string_to_unsigned_long(a, (unsigned long*) b, c, 0) +#endif + +rtems_status_code rtems_string_to_pointer ( + const char *s, + void **n, + char **endptr +) +{ + return STRTOFUNC( s, n, endptr ); +} diff --git a/cpukit/libmisc/stringto/stringtounsignedchar.c b/cpukit/libmisc/stringto/stringtounsignedchar.c new file mode 100644 index 0000000000..5223b03fb7 --- /dev/null +++ b/cpukit/libmisc/stringto/stringtounsignedchar.c @@ -0,0 +1,66 @@ +/* + * COPYRIGHT (c) 2009. + * On-Line Applications Research Corporation (OAR). + * + * Copyright (c) 2011 Ralf Corsépius, Ulm, Germany. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + * + * $Id$ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <errno.h> +#include <stdlib.h> +#include <limits.h> + +#include <rtems/stringto.h> + +/* + * Instantiate an error checking wrapper for strtoul (unsigned char) + */ + +rtems_status_code rtems_string_to_unsigned_char ( + const char *s, + unsigned char *n, + char **endptr, + int base +) +{ + unsigned long result; + char *end; + + if ( !n ) + return RTEMS_INVALID_ADDRESS; + + errno = 0; + *n = 0; + + result = strtoul( s, &end, base ); + + if ( endptr ) + *endptr = end; + + if ( end == s ) + return RTEMS_NOT_DEFINED; + + if ( ( errno == ERANGE ) && + (( result == 0 ) || ( result == ULONG_MAX ))) + return RTEMS_INVALID_NUMBER; + +#if (UCHAR_MAX < ULONG_MAX) + if ( result > UCHAR_MAX ) { + errno = ERANGE; + return RTEMS_INVALID_NUMBER; + } +#endif + + *n = result; + + return RTEMS_SUCCESSFUL; +} diff --git a/cpukit/libmisc/stringto/stringtounsignedint.c b/cpukit/libmisc/stringto/stringtounsignedint.c new file mode 100644 index 0000000000..9821beff87 --- /dev/null +++ b/cpukit/libmisc/stringto/stringtounsignedint.c @@ -0,0 +1,66 @@ +/* + * COPYRIGHT (c) 2009. + * On-Line Applications Research Corporation (OAR). + * + * Copyright (c) 2011 Ralf Corsépius, Ulm, Germany. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + * + * $Id$ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <errno.h> +#include <stdlib.h> +#include <limits.h> + +#include <rtems/stringto.h> + +/* + * Instantiate an error checking wrapper for strtoul (unsigned int) + */ + +rtems_status_code rtems_string_to_unsigned_int ( + const char *s, + unsigned int *n, + char **endptr, + int base +) +{ + unsigned long result; + char *end; + + if ( !n ) + return RTEMS_INVALID_ADDRESS; + + errno = 0; + *n = 0; + + result = strtoul( s, &end, base ); + + if ( endptr ) + *endptr = end; + + if ( end == s ) + return RTEMS_NOT_DEFINED; + + if ( ( errno == ERANGE ) && + (( result == 0 ) || ( result == ULONG_MAX ))) + return RTEMS_INVALID_NUMBER; + +#if (UINT_MAX < ULONG_MAX) + if ( result > UINT_MAX ) { + errno = ERANGE; + return RTEMS_INVALID_NUMBER; + } +#endif + + *n = result; + + return RTEMS_SUCCESSFUL; +} diff --git a/cpukit/libmisc/stringto/stringtounsignedlong.c b/cpukit/libmisc/stringto/stringtounsignedlong.c new file mode 100644 index 0000000000..11c374c486 --- /dev/null +++ b/cpukit/libmisc/stringto/stringtounsignedlong.c @@ -0,0 +1,59 @@ +/* + * COPYRIGHT (c) 2009. + * On-Line Applications Research Corporation (OAR). + * + * Copyright (c) 2011 Ralf Corsépius, Ulm, Germany. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + * + * $Id$ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <errno.h> +#include <stdlib.h> +#include <limits.h> + +#include <rtems/stringto.h> + +/* + * Instantiate an error checking wrapper for strtoul (unsigned long) + */ + +rtems_status_code rtems_string_to_unsigned_long ( + const char *s, + unsigned long *n, + char **endptr, + int base +) +{ + unsigned long result; + char *end; + + if ( !n ) + return RTEMS_INVALID_ADDRESS; + + errno = 0; + *n = 0; + + result = strtoul( s, &end, base ); + + if ( endptr ) + *endptr = end; + + if ( end == s ) + return RTEMS_NOT_DEFINED; + + if ( ( errno == ERANGE ) && + (( result == 0 ) || ( result == ULONG_MAX ))) + return RTEMS_INVALID_NUMBER; + + *n = result; + + return RTEMS_SUCCESSFUL; +} diff --git a/cpukit/libmisc/stringto/stringtounsignedlonglong.c b/cpukit/libmisc/stringto/stringtounsignedlonglong.c new file mode 100644 index 0000000000..61c6e2e063 --- /dev/null +++ b/cpukit/libmisc/stringto/stringtounsignedlonglong.c @@ -0,0 +1,64 @@ +/* + * COPYRIGHT (c) 2009. + * On-Line Applications Research Corporation (OAR). + * + * Copyright (c) 2011 Ralf Corsépius, Ulm, Germany. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + * + * $Id$ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <errno.h> +#include <stdlib.h> +#include <limits.h> + +#include <rtems/stringto.h> + +/* c99 has ULLONG_MAX instead of ULONG_LONG_MAX */ +#ifndef ULONG_LONG_MAX +#define ULONG_LONG_MAX ULLONG_MAX +#endif + +/* + * Instantiate an error checking wrapper for strtoull (unsigned long long) + */ + +rtems_status_code rtems_string_to_unsigned_long_long ( + const char *s, + unsigned long long *n, + char **endptr, + int base +) +{ + unsigned long long result; + char *end; + + if ( !n ) + return RTEMS_INVALID_ADDRESS; + + errno = 0; + *n = 0; + + result = strtoull( s, &end, base ); + + if ( endptr ) + *endptr = end; + + if ( end == s ) + return RTEMS_NOT_DEFINED; + + if ( ( errno == ERANGE ) && + (( result == 0 ) || ( result == ULONG_LONG_MAX ))) + return RTEMS_INVALID_NUMBER; + + *n = result; + + return RTEMS_SUCCESSFUL; +} |