blob: a4db94c867522c3e0d299ff065a79bc750d4e021 [file] [log] [blame]
Paul Bakker01cc3942012-05-08 08:36:15 +00001/*
2 * Translate error code to error string
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2012, ARM Limited, All Rights Reserved
Paul Bakker01cc3942012-05-08 08:36:15 +00005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakker01cc3942012-05-08 08:36:15 +00007 *
Paul Bakker01cc3942012-05-08 08:36:15 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker01cc3942012-05-08 08:36:15 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
34#define polarssl_malloc malloc
35#define polarssl_free free
36#endif
37
Paul Bakker01cc3942012-05-08 08:36:15 +000038#include <stdlib.h>
39#include <string.h>
40#include <stdio.h>
41
Paul Bakker01cc3942012-05-08 08:36:15 +000042#include "polarssl/error.h"
43
44#define USAGE \
Paul Bakker62534dd2013-06-30 12:45:07 +020045 "\n usage: strerror <errorcode>\n" \
46 "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
Paul Bakker01cc3942012-05-08 08:36:15 +000047
Paul Bakker8fe40dc2013-02-02 12:43:08 +010048#if !defined(POLARSSL_ERROR_C) && !defined(POLARSSL_ERROR_STRERROR_DUMMY)
Paul Bakker01cc3942012-05-08 08:36:15 +000049int main( int argc, char *argv[] )
50{
51 ((void) argc);
52 ((void) argv);
53
Rich Evansf90016a2015-01-19 14:26:37 +000054 polarssl_printf("POLARSSL_ERROR_C and/or POLARSSL_ERROR_STRERROR_DUMMY not defined.\n");
Paul Bakker01cc3942012-05-08 08:36:15 +000055 return( 0 );
56}
57#else
58int main( int argc, char *argv[] )
59{
Paul Bakker62534dd2013-06-30 12:45:07 +020060 long int val;
61 char *end = argv[1];
Paul Bakker01cc3942012-05-08 08:36:15 +000062
63 if( argc != 2 )
64 {
Rich Evansf90016a2015-01-19 14:26:37 +000065 polarssl_printf( USAGE );
Paul Bakker01cc3942012-05-08 08:36:15 +000066 return( 0 );
67 }
68
Paul Bakker62534dd2013-06-30 12:45:07 +020069 val = strtol( argv[1], &end, 10 );
70 if( *end != '\0' )
71 {
72 val = strtol( argv[1], &end, 16 );
73 if( *end != '\0' )
74 {
Rich Evansf90016a2015-01-19 14:26:37 +000075 polarssl_printf( USAGE );
Paul Bakker62534dd2013-06-30 12:45:07 +020076 return( 0 );
77 }
78 }
79 if( val > 0 )
80 val = -val;
Paul Bakker01cc3942012-05-08 08:36:15 +000081
Paul Bakker62534dd2013-06-30 12:45:07 +020082 if( val != 0 )
Paul Bakker01cc3942012-05-08 08:36:15 +000083 {
84 char error_buf[200];
Paul Bakker62534dd2013-06-30 12:45:07 +020085 polarssl_strerror( val, error_buf, 200 );
Rich Evansf90016a2015-01-19 14:26:37 +000086 polarssl_printf("Last error was: -0x%04x - %s\n\n", (int) -val, error_buf );
Paul Bakker01cc3942012-05-08 08:36:15 +000087 }
88
89#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +000090 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker01cc3942012-05-08 08:36:15 +000091 fflush( stdout ); getchar();
92#endif
93
Paul Bakker62534dd2013-06-30 12:45:07 +020094 return( val );
Paul Bakker01cc3942012-05-08 08:36:15 +000095}
96#endif /* POLARSSL_ERROR_C */