| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Translate error code to error string | 
|  | 3 | * | 
|  | 4 | *  Copyright (C) 2006-2012, Brainspark B.V. | 
|  | 5 | * | 
|  | 6 | *  This file is part of PolarSSL (http://www.polarssl.org) | 
|  | 7 | *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> | 
|  | 8 | * | 
|  | 9 | *  All rights reserved. | 
|  | 10 | * | 
|  | 11 | *  This program is free software; you can redistribute it and/or modify | 
|  | 12 | *  it under the terms of the GNU General Public License as published by | 
|  | 13 | *  the Free Software Foundation; either version 2 of the License, or | 
|  | 14 | *  (at your option) any later version. | 
|  | 15 | * | 
|  | 16 | *  This program is distributed in the hope that it will be useful, | 
|  | 17 | *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 18 | *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 19 | *  GNU General Public License for more details. | 
|  | 20 | * | 
|  | 21 | *  You should have received a copy of the GNU General Public License along | 
|  | 22 | *  with this program; if not, write to the Free Software Foundation, Inc., | 
|  | 23 | *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 
|  | 24 | */ | 
|  | 25 |  | 
|  | 26 | #ifndef _CRT_SECURE_NO_DEPRECATE | 
|  | 27 | #define _CRT_SECURE_NO_DEPRECATE 1 | 
|  | 28 | #endif | 
|  | 29 |  | 
|  | 30 | #include <stdlib.h> | 
|  | 31 | #include <string.h> | 
|  | 32 | #include <stdio.h> | 
|  | 33 |  | 
|  | 34 | #include "polarssl/config.h" | 
|  | 35 |  | 
|  | 36 | #include "polarssl/error.h" | 
|  | 37 |  | 
|  | 38 | #define USAGE \ | 
| Paul Bakker | 62534dd | 2013-06-30 12:45:07 +0200 | [diff] [blame] | 39 | "\n usage: strerror <errorcode>\n" \ | 
|  | 40 | "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n" | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 41 |  | 
| Paul Bakker | 8fe40dc | 2013-02-02 12:43:08 +0100 | [diff] [blame] | 42 | #if !defined(POLARSSL_ERROR_C) && !defined(POLARSSL_ERROR_STRERROR_DUMMY) | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 43 | int main( int argc, char *argv[] ) | 
|  | 44 | { | 
|  | 45 | ((void) argc); | 
|  | 46 | ((void) argv); | 
|  | 47 |  | 
| Paul Bakker | 62534dd | 2013-06-30 12:45:07 +0200 | [diff] [blame] | 48 | printf("POLARSSL_ERROR_C and/or POLARSSL_ERROR_STRERROR_DUMMY not defined.\n"); | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 49 | return( 0 ); | 
|  | 50 | } | 
|  | 51 | #else | 
|  | 52 | int main( int argc, char *argv[] ) | 
|  | 53 | { | 
| Paul Bakker | 62534dd | 2013-06-30 12:45:07 +0200 | [diff] [blame] | 54 | long int val; | 
|  | 55 | char *end = argv[1]; | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 56 |  | 
|  | 57 | if( argc != 2 ) | 
|  | 58 | { | 
|  | 59 | printf( USAGE ); | 
|  | 60 | return( 0 ); | 
|  | 61 | } | 
|  | 62 |  | 
| Paul Bakker | 62534dd | 2013-06-30 12:45:07 +0200 | [diff] [blame] | 63 | val = strtol( argv[1], &end, 10 ); | 
|  | 64 | if( *end != '\0' ) | 
|  | 65 | { | 
|  | 66 | val = strtol( argv[1], &end, 16 ); | 
|  | 67 | if( *end != '\0' ) | 
|  | 68 | { | 
|  | 69 | printf( USAGE ); | 
|  | 70 | return( 0 ); | 
|  | 71 | } | 
|  | 72 | } | 
|  | 73 | if( val > 0 ) | 
|  | 74 | val = -val; | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 75 |  | 
| Paul Bakker | 62534dd | 2013-06-30 12:45:07 +0200 | [diff] [blame] | 76 | if( val != 0 ) | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 77 | { | 
|  | 78 | char error_buf[200]; | 
| Paul Bakker | 62534dd | 2013-06-30 12:45:07 +0200 | [diff] [blame] | 79 | polarssl_strerror( val, error_buf, 200 ); | 
|  | 80 | printf("Last error was: -0x%04x - %s\n\n", (int) -val, error_buf ); | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
|  | 83 | #if defined(_WIN32) | 
|  | 84 | printf( "  + Press Enter to exit this program.\n" ); | 
|  | 85 | fflush( stdout ); getchar(); | 
|  | 86 | #endif | 
|  | 87 |  | 
| Paul Bakker | 62534dd | 2013-06-30 12:45:07 +0200 | [diff] [blame] | 88 | return( val ); | 
| Paul Bakker | 01cc394 | 2012-05-08 08:36:15 +0000 | [diff] [blame] | 89 | } | 
|  | 90 | #endif /* POLARSSL_ERROR_C */ |