programs/util/strerror now handles decimal and hexidecimal input

This commit is contained in:
Paul Bakker 2013-06-30 12:45:07 +02:00
parent 63899feca8
commit 62534dd1d8

View File

@ -36,7 +36,8 @@
#include "polarssl/error.h"
#define USAGE \
"\n usage: strerror <errorcode>\n"
"\n usage: strerror <errorcode>\n" \
"\n where <errorcode> 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 */