From 48751ab095bbbb23489c2e3243f57571cbfa9153 Mon Sep 17 00:00:00 2001 From: Joel Sherrill Date: Thu, 23 Jul 2009 14:32:34 +0000 Subject: 2009-07-23 Joel Sherrill * libmisc/Makefile.am, libmisc/shell/main_chmod.c, libmisc/shell/main_mdump.c, libmisc/shell/main_medit.c, libmisc/shell/main_mfill.c, libmisc/shell/main_mmove.c, libmisc/shell/main_msdosfmt.c, libmisc/shell/main_mwdump.c, libmisc/shell/main_sleep.c, libmisc/shell/main_umask.c, libmisc/shell/shell_script.c, libmisc/stringto/stringto.h, libmisc/stringto/stringto_template.h: Convert return type from bool to rtems_status_code and add rtems_string_to_pointer. Perform associated clean up and changes for return type change. * libmisc/stringto/stringtopointer.c: New file. --- cpukit/ChangeLog | 13 +++++ cpukit/libmisc/Makefile.am | 1 + cpukit/libmisc/shell/main_chmod.c | 2 +- cpukit/libmisc/shell/main_mdump.c | 21 +++----- cpukit/libmisc/shell/main_medit.c | 8 +-- cpukit/libmisc/shell/main_mfill.c | 17 +++---- cpukit/libmisc/shell/main_mmove.c | 18 +++---- cpukit/libmisc/shell/main_msdosfmt.c | 4 +- cpukit/libmisc/shell/main_mwdump.c | 12 ++--- cpukit/libmisc/shell/main_sleep.c | 4 +- cpukit/libmisc/shell/main_umask.c | 2 +- cpukit/libmisc/shell/shell_script.c | 4 +- cpukit/libmisc/stringto/stringto.h | 78 ++++++++++++++++++++--------- cpukit/libmisc/stringto/stringto_template.h | 41 +++++++++++---- cpukit/libmisc/stringto/stringtopointer.c | 25 +++++++++ 15 files changed, 164 insertions(+), 86 deletions(-) create mode 100644 cpukit/libmisc/stringto/stringtopointer.c diff --git a/cpukit/ChangeLog b/cpukit/ChangeLog index 04bc1f1de4..a74e5f2358 100644 --- a/cpukit/ChangeLog +++ b/cpukit/ChangeLog @@ -1,3 +1,16 @@ +2009-07-23 Joel Sherrill + + * libmisc/Makefile.am, libmisc/shell/main_chmod.c, + libmisc/shell/main_mdump.c, libmisc/shell/main_medit.c, + libmisc/shell/main_mfill.c, libmisc/shell/main_mmove.c, + libmisc/shell/main_msdosfmt.c, libmisc/shell/main_mwdump.c, + libmisc/shell/main_sleep.c, libmisc/shell/main_umask.c, + libmisc/shell/shell_script.c, libmisc/stringto/stringto.h, + libmisc/stringto/stringto_template.h: Convert return type from bool + to rtems_status_code and add rtems_string_to_pointer. Perform + associated clean up and changes for return type change. + * libmisc/stringto/stringtopointer.c: New file. + 2009-07-22 Joel Sherrill * posix/src/killinfo.c: Clean up. Avoid NULL pointer case. diff --git a/cpukit/libmisc/Makefile.am b/cpukit/libmisc/Makefile.am index 871d9abcb3..8b9f373e41 100644 --- a/cpukit/libmisc/Makefile.am +++ b/cpukit/libmisc/Makefile.am @@ -131,6 +131,7 @@ noinst_LIBRARIES += libstringto.a libstringto_a_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/stringto libstringto_a_SOURCES = stringto/stringtodouble.c stringto/stringtofloat.c \ stringto/stringtoint.c stringto/stringtolong.c stringto/stringtolonglong.c \ + stringto/stringtopointer.c stringto/stringtounsignedint.c \ stringto/stringtounsignedchar.c stringto/stringtounsignedint.c \ stringto/stringtounsignedlong.c stringto/stringtounsignedlonglong.c diff --git a/cpukit/libmisc/shell/main_chmod.c b/cpukit/libmisc/shell/main_chmod.c index 14e744255c..3c6ae68310 100644 --- a/cpukit/libmisc/shell/main_chmod.c +++ b/cpukit/libmisc/shell/main_chmod.c @@ -45,7 +45,7 @@ int rtems_shell_main_chmod( /* * Convert arguments into numbers */ - if ( !rtems_string_to_unsigned_long(argv[1], &tmp, NULL, 0) ) { + if ( rtems_string_to_unsigned_long(argv[1], &tmp, NULL, 0) ) { printf( "Mode argument (%s) is not a number\n", argv[1] ); return -1; } diff --git a/cpukit/libmisc/shell/main_mdump.c b/cpukit/libmisc/shell/main_mdump.c index 9d597a9436..297ab6db0e 100644 --- a/cpukit/libmisc/shell/main_mdump.c +++ b/cpukit/libmisc/shell/main_mdump.c @@ -31,34 +31,30 @@ int rtems_shell_main_mdump( char *argv[] ) { - unsigned long tmp; unsigned char n; unsigned char m; int max; int res; - uintptr_t addr = 0; + void *addr = NULL; unsigned char *pb; if (argc > 1) { - if ( !rtems_string_to_unsigned_long(argv[1], &tmp, NULL, 0) ) { + if ( rtems_string_to_pointer(argv[1], &addr, NULL) ) { printf( "Address argument (%s) is not a number\n", argv[1] ); return -1; } - addr = (uintptr_t) tmp; } - if (argc>2) { - if ( !rtems_string_to_int(argv[1], &max, NULL, 0) ) { + if (argc > 2) { + if ( rtems_string_to_int(argv[1], &max, NULL, 0) ) { printf( "Length argument (%s) is not a number\n", argv[1] ); return -1; } - addr = (uintptr_t) tmp; if (max <= 0) { max = 1; /* print 1 item if 0 or neg. */ res = 0; - } - else { + } else { max--; res = max & 0xf;/* num bytes in last row */ max >>= 4; /* div by 16 */ @@ -68,14 +64,13 @@ int rtems_shell_main_mdump( res = 0xf; /* 16 bytes print in last row */ } } - } - else { + } else { max = 20; res = 0xf; } + pb = addr; for (m=0; m 1 ) { - if ( !rtems_string_to_unsigned_long(argv[1], &tmp, NULL, 0) ) { + if ( rtems_string_to_pointer(argv[1], &addr, NULL) ) { printf( "Address argument (%s) is not a number\n", argv[1] ); return -1; } - addr = (uintptr_t) tmp; } if ( argc > 2 ) { - if ( !rtems_string_to_int(argv[2], &max, NULL, 0) ) { + if ( rtems_string_to_int(argv[2], &max, NULL, 0) ) { printf( "Address argument (%s) is not a number\n", argv[1] ); return -1; } @@ -71,8 +69,8 @@ int rtems_shell_main_mwdump( res = 0xf; } + pb = addr; for (m=0;m + +/** + * @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) * @@ -23,10 +45,11 @@ * @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 true on successful conversion and *n is - * filled in. + * @return This method returns RTEMS_SUCCESSFUL on successful conversion + * and *n is filled in. Otherwise, the status indicates the + * source of the error. */ -bool rtems_string_to_unsigned_char( +rtems_status_code rtems_string_to_unsigned_char( const char *s, unsigned char *n, char **endptr, @@ -43,10 +66,11 @@ bool rtems_string_to_unsigned_char( * @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 true on successful conversion and *n is - * filled in. + * @return This method returns RTEMS_SUCCESSFUL on successful conversion + * and *n is filled in. Otherwise, the status indicates the + * source of the error. */ -bool rtems_string_to_int( +rtems_status_code rtems_string_to_int( const char *s, int *n, char **endptr, @@ -64,10 +88,11 @@ bool rtems_string_to_int( * @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 true on successful conversion and *n is - * filled in. + * @return This method returns RTEMS_SUCCESSFUL on successful conversion + * and *n is filled in. Otherwise, the status indicates the + * source of the error. */ -bool rtems_string_to_long( +rtems_status_code rtems_string_to_long( const char *s, long *n, char **endptr, @@ -85,10 +110,11 @@ bool rtems_string_to_long( * @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 true on successful conversion and *n is - * filled in. + * @return This method returns RTEMS_SUCCESSFUL on successful conversion + * and *n is filled in. Otherwise, the status indicates the + * source of the error. */ -bool rtems_string_to_unsigned_long( +rtems_status_code rtems_string_to_unsigned_long( const char *s, unsigned long *n, char **endptr, @@ -106,10 +132,11 @@ bool rtems_string_to_unsigned_long( * @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 true on successful conversion and *n is - * filled in. + * @return This method returns RTEMS_SUCCESSFUL on successful conversion + * and *n is filled in. Otherwise, the status indicates the + * source of the error. */ -bool rtems_string_to_long_long( +rtems_status_code rtems_string_to_long_long( const char *s, long long *n, char **endptr, @@ -127,10 +154,11 @@ bool rtems_string_to_long_long( * @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 true on successful conversion and *n is - * filled in. + * @return This method returns RTEMS_SUCCESSFUL on successful conversion + * and *n is filled in. Otherwise, the status indicates the + * source of the error. */ -bool rtems_string_to_unsigned_long_long( +rtems_status_code rtems_string_to_unsigned_long_long( const char *s, unsigned long long *n, char **endptr, @@ -146,10 +174,11 @@ bool rtems_string_to_unsigned_long_long( * @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 true on successful conversion and *n is - * filled in. + * @return This method returns RTEMS_SUCCESSFUL on successful conversion + * and *n is filled in. Otherwise, the status indicates the + * source of the error. */ -bool rtems_string_to_float( +rtems_status_code rtems_string_to_float( const char *s, float *n, char **endptr @@ -164,10 +193,11 @@ bool rtems_string_to_float( * @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 true on successful conversion and *n is - * filled in. + * @return This method returns RTEMS_SUCCESSFUL on successful conversion + * and *n is filled in. Otherwise, the status indicates the + * source of the error. */ -bool rtems_string_to_double( +rtems_status_code rtems_string_to_double( const char *s, double *n, char **endptr diff --git a/cpukit/libmisc/stringto/stringto_template.h b/cpukit/libmisc/stringto/stringto_template.h index 501f4393f8..863c98ff81 100644 --- a/cpukit/libmisc/stringto/stringto_template.h +++ b/cpukit/libmisc/stringto/stringto_template.h @@ -9,18 +9,31 @@ * $Id$ */ +#include + #include #include #include #include -#include +/* + * If we are doing floating point conversion, then we need math.h + */ +#if defined(STRING_TO_FLOAT) + #include +#endif + +#include /* * This file is designed to be included multiple times to instantiate * it and should NOT be protected against multiple inclusions. */ +#if defined(STRING_TO_POINTER) + #define STRING_TO_INTEGER +#endif + #if !defined(STRING_TO_FLOAT) && !defined(STRING_TO_INTEGER) #error "Neither STRING_TO_FLOAT nor STRING_TO_INTEGER defined" #endif @@ -52,27 +65,33 @@ #define ZERO 0 #endif -bool STRING_TO_NAME ( +#if !defined(STRING_TO_INPUT_TYPE) + #define STRING_TO_INPUT_TYPE STRING_TO_TYPE +#endif + +rtems_status_code STRING_TO_NAME ( const char *s, STRING_TO_TYPE *n, char **endptr - #if defined(STRING_TO_INTEGER) + #if defined(STRING_TO_INTEGER) && !defined(STRING_TO_POINTER) , int base #endif ) { - STRING_TO_TYPE result; - char *end; + STRING_TO_INPUT_TYPE result; + char *end; if ( !n ) - return false; + return RTEMS_INVALID_ADDRESS; errno = 0; *n = 0; #ifdef STRING_TO_FLOAT result = STRING_TO_METHOD( s, &end ); + #elif defined(STRING_TO_POINTER) + result = STRING_TO_METHOD( s, &end, 16 ); #elif defined(STRING_TO_INTEGER) result = STRING_TO_METHOD( s, &end, base ); #endif @@ -83,25 +102,25 @@ bool STRING_TO_NAME ( /* nothing was converted */ if ( end == s ) - return false; + return RTEMS_NOT_DEFINED; /* there was a conversion error */ if ( (result == ZERO) && errno ) - return false; + return RTEMS_INVALID_NUMBER; #ifdef STRING_TO_MAX /* there was an overflow */ if ( (result == STRING_TO_MAX) && (errno == ERANGE)) - return false; + return RTEMS_INVALID_NUMBER; #endif #ifdef STRING_TO_MIN /* there was an underflow */ if ( (result == STRING_TO_MIN) && (errno == ERANGE)) - return false; + return RTEMS_INVALID_NUMBER; #endif *n = (STRING_TO_TYPE) result; - return true; + return RTEMS_SUCCESSFUL; } diff --git a/cpukit/libmisc/stringto/stringtopointer.c b/cpukit/libmisc/stringto/stringtopointer.c new file mode 100644 index 0000000000..ca1e480d2d --- /dev/null +++ b/cpukit/libmisc/stringto/stringtopointer.c @@ -0,0 +1,25 @@ +/* + * 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$ + */ + +/* + * Instantiate an error checking wrapper for strtoul which is + * used to input a (void *) + * + * NOTE: This is only an appropriate implementation when unsigned long + * can represent a void * + */ +#define STRING_TO_POINTER +#define STRING_TO_TYPE void * +#define STRING_TO_INPUT_TYPE unsigned long +#define STRING_TO_NAME rtems_string_to_pointer +#define STRING_TO_METHOD strtoul +#define STRING_TO_MAX ULONG_MAX +#include "stringto_template.h" -- cgit v1.2.3