blob: b751ca0661b64567fbd28a6d8aceac089b60d88b [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
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Paul Bakker01cc3942012-05-08 08:36:15 +000035#include <stdlib.h>
36#include <string.h>
37#include <stdio.h>
38
Paul Bakker01cc3942012-05-08 08:36:15 +000039#include "polarssl/error.h"
40
41#define USAGE \
Paul Bakker62534dd2013-06-30 12:45:07 +020042 "\n usage: strerror <errorcode>\n" \
43 "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
Paul Bakker01cc3942012-05-08 08:36:15 +000044
Paul Bakker8fe40dc2013-02-02 12:43:08 +010045#if !defined(POLARSSL_ERROR_C) && !defined(POLARSSL_ERROR_STRERROR_DUMMY)
Paul Bakker01cc3942012-05-08 08:36:15 +000046int main( int argc, char *argv[] )
47{
48 ((void) argc);
49 ((void) argv);
50
Rich Evansf90016a2015-01-19 14:26:37 +000051 polarssl_printf("POLARSSL_ERROR_C and/or POLARSSL_ERROR_STRERROR_DUMMY not defined.\n");
Paul Bakker01cc3942012-05-08 08:36:15 +000052 return( 0 );
53}
54#else
55int main( int argc, char *argv[] )
56{
Paul Bakker62534dd2013-06-30 12:45:07 +020057 long int val;
58 char *end = argv[1];
Paul Bakker01cc3942012-05-08 08:36:15 +000059
60 if( argc != 2 )
61 {
Rich Evansf90016a2015-01-19 14:26:37 +000062 polarssl_printf( USAGE );
Paul Bakker01cc3942012-05-08 08:36:15 +000063 return( 0 );
64 }
65
Paul Bakker62534dd2013-06-30 12:45:07 +020066 val = strtol( argv[1], &end, 10 );
67 if( *end != '\0' )
68 {
69 val = strtol( argv[1], &end, 16 );
70 if( *end != '\0' )
71 {
Rich Evansf90016a2015-01-19 14:26:37 +000072 polarssl_printf( USAGE );
Paul Bakker62534dd2013-06-30 12:45:07 +020073 return( 0 );
74 }
75 }
76 if( val > 0 )
77 val = -val;
Paul Bakker01cc3942012-05-08 08:36:15 +000078
Paul Bakker62534dd2013-06-30 12:45:07 +020079 if( val != 0 )
Paul Bakker01cc3942012-05-08 08:36:15 +000080 {
81 char error_buf[200];
Paul Bakker62534dd2013-06-30 12:45:07 +020082 polarssl_strerror( val, error_buf, 200 );
Rich Evansf90016a2015-01-19 14:26:37 +000083 polarssl_printf("Last error was: -0x%04x - %s\n\n", (int) -val, error_buf );
Paul Bakker01cc3942012-05-08 08:36:15 +000084 }
85
86#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +000087 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker01cc3942012-05-08 08:36:15 +000088 fflush( stdout ); getchar();
89#endif
90
Paul Bakker62534dd2013-06-30 12:45:07 +020091 return( val );
Paul Bakker01cc3942012-05-08 08:36:15 +000092}
93#endif /* POLARSSL_ERROR_C */