strerror.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Translate error code to error string
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_PLATFORM_C)
  27. #include "mbedtls/platform.h"
  28. #else
  29. #include <stdio.h>
  30. #define mbedtls_printf printf
  31. #endif
  32. #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
  33. #include "mbedtls/error.h"
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #endif
  38. #define USAGE \
  39. "\n usage: strerror <errorcode>\n" \
  40. "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
  41. #if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY)
  42. int main( void )
  43. {
  44. mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n");
  45. return( 0 );
  46. }
  47. #else
  48. int main( int argc, char *argv[] )
  49. {
  50. long int val;
  51. char *end = argv[1];
  52. if( argc != 2 )
  53. {
  54. mbedtls_printf( USAGE );
  55. return( 0 );
  56. }
  57. val = strtol( argv[1], &end, 10 );
  58. if( *end != '\0' )
  59. {
  60. val = strtol( argv[1], &end, 16 );
  61. if( *end != '\0' )
  62. {
  63. mbedtls_printf( USAGE );
  64. return( 0 );
  65. }
  66. }
  67. if( val > 0 )
  68. val = -val;
  69. if( val != 0 )
  70. {
  71. char error_buf[200];
  72. mbedtls_strerror( val, error_buf, 200 );
  73. mbedtls_printf("Last error was: -0x%04x - %s\n\n", (int) -val, error_buf );
  74. }
  75. #if defined(_WIN32)
  76. mbedtls_printf( " + Press Enter to exit this program.\n" );
  77. fflush( stdout ); getchar();
  78. #endif
  79. return( val );
  80. }
  81. #endif /* MBEDTLS_ERROR_C */