From 62534dd1d8d26f66c5142a7397489acef818db1b Mon Sep 17 00:00:00 2001
From: Paul Bakker
Date: Sun, 30 Jun 2013 12:45:07 +0200
Subject: [PATCH] programs/util/strerror now handles decimal and hexidecimal
input
---
programs/util/strerror.c | 31 +++++++++++++++++++++----------
1 file changed, 21 insertions(+), 10 deletions(-)
diff --git a/programs/util/strerror.c b/programs/util/strerror.c
index b8e69dbc8..8b76228f2 100644
--- a/programs/util/strerror.c
+++ b/programs/util/strerror.c
@@ -36,7 +36,8 @@
#include "polarssl/error.h"
#define USAGE \
- "\n usage: strerror \n"
+ "\n usage: strerror \n" \
+ "\n where can be a decimal or hexadecimal (starts with 0x or -0x)\n"
#if !defined(POLARSSL_ERROR_C) && !defined(POLARSSL_ERROR_STRERROR_DUMMY)
int main( int argc, char *argv[] )
@@ -44,13 +45,14 @@ int main( int argc, char *argv[] )
((void) argc);
((void) argv);
- printf("POLARSSL_ERROR_C and/or POLARSSL_ERROR_STRERRO_DUMMY not defined.\n");
+ printf("POLARSSL_ERROR_C and/or POLARSSL_ERROR_STRERROR_DUMMY not defined.\n");
return( 0 );
}
#else
int main( int argc, char *argv[] )
{
- int ret;
+ long int val;
+ char *end = argv[1];
if( argc != 2 )
{
@@ -58,15 +60,24 @@ int main( int argc, char *argv[] )
return( 0 );
}
- ret = atoi( argv[1] );
- if( ret > 0 )
- ret = - ret;
+ val = strtol( argv[1], &end, 10 );
+ if( *end != '\0' )
+ {
+ val = strtol( argv[1], &end, 16 );
+ if( *end != '\0' )
+ {
+ printf( USAGE );
+ return( 0 );
+ }
+ }
+ if( val > 0 )
+ val = -val;
- if( ret != 0 )
+ if( val != 0 )
{
char error_buf[200];
- polarssl_strerror( ret, error_buf, 200 );
- printf("Last error was: %d - %s\n\n", ret, error_buf );
+ polarssl_strerror( val, error_buf, 200 );
+ printf("Last error was: -0x%04x - %s\n\n", (int) -val, error_buf );
}
#if defined(_WIN32)
@@ -74,6 +85,6 @@ int main( int argc, char *argv[] )
fflush( stdout ); getchar();
#endif
- return( ret );
+ return( val );
}
#endif /* POLARSSL_ERROR_C */