Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:19

0001 /**
0002  * @file
0003  *
0004  * @ingroup libmisc_conv_help Conversion Helpers
0005  *
0006  * @brief Convert String to Unsigned Long Long (with validation)
0007  */
0008 
0009 /*
0010  *  COPYRIGHT (c) 2009.
0011  *  On-Line Applications Research Corporation (OAR).
0012  *
0013  *  Copyright (c) 2011  Ralf Corsépius, Ulm, Germany.
0014  *
0015  *  The license and distribution terms for this file may be
0016  *  found in the file LICENSE in this distribution or at
0017  *  http://www.rtems.org/license/LICENSE.
0018  */
0019 
0020 #ifdef HAVE_CONFIG_H
0021 #include "config.h"
0022 #endif
0023 
0024 #include <errno.h>
0025 #include <stdlib.h>
0026 #include <limits.h>
0027 
0028 #include <rtems/stringto.h>
0029 
0030 /* c99 has ULLONG_MAX instead of ULONG_LONG_MAX */
0031 #ifndef ULONG_LONG_MAX
0032 #define ULONG_LONG_MAX  ULLONG_MAX
0033 #endif
0034 
0035 /*
0036  *  Instantiate an error checking wrapper for strtoull (unsigned long long)
0037  */
0038 
0039 rtems_status_code rtems_string_to_unsigned_long_long (
0040   const char *s,
0041   unsigned long long *n,
0042   char **endptr,
0043   int base
0044 )
0045 {
0046   unsigned long long result;
0047   char *end;
0048 
0049   if ( !n )
0050     return RTEMS_INVALID_ADDRESS;
0051 
0052   errno = 0;
0053   *n = 0;
0054 
0055   result = strtoull( s, &end, base );
0056 
0057   if ( endptr )
0058     *endptr = end;
0059 
0060   if ( end == s )
0061     return RTEMS_NOT_DEFINED;
0062 
0063   if ( ( errno == ERANGE ) &&
0064     (( result == 0 ) || ( result == ULONG_LONG_MAX )))
0065       return RTEMS_INVALID_NUMBER;
0066 
0067   *n = result;
0068 
0069   return RTEMS_SUCCESSFUL;
0070 }