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 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 LLONG_MAX instead of LONG_LONG_MAX */
0031 #ifndef LONG_LONG_MAX
0032 #define LONG_LONG_MAX   LLONG_MAX
0033 #endif
0034 /* c99 has LLONG_MIN instead of LONG_LONG_MIN */
0035 #ifndef LONG_LONG_MIN
0036 #define LONG_LONG_MIN   LLONG_MIN
0037 #endif
0038 
0039 /*
0040  *  Instantiate an error checking wrapper for strtoll (long long)
0041  */
0042 
0043 rtems_status_code rtems_string_to_long_long (
0044   const char *s,
0045   long long *n,
0046   char **endptr,
0047   int base
0048 )
0049 {
0050   long long result;
0051   char *end;
0052 
0053   if ( !n )
0054     return RTEMS_INVALID_ADDRESS;
0055 
0056   errno = 0;
0057   *n = 0;
0058 
0059   result = strtoll( s, &end, base );
0060 
0061   if ( endptr )
0062     *endptr = end;
0063 
0064   if ( end == s )
0065     return RTEMS_NOT_DEFINED;
0066 
0067   if ( ( errno == ERANGE ) &&
0068     (( result == 0 ) || ( result == LONG_LONG_MAX ) || ( result == LONG_LONG_MIN )))
0069       return RTEMS_INVALID_NUMBER;
0070 
0071   *n = result;
0072 
0073   return RTEMS_SUCCESSFUL;
0074 }