2
0

strerror.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Translate error code to error string
  3. *
  4. * Copyright The Mbed TLS Contributors
  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. #include "mbedtls/build_info.h"
  20. #if defined(MBEDTLS_PLATFORM_C)
  21. #include "mbedtls/platform.h"
  22. #else
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #define mbedtls_printf printf
  26. #define mbedtls_exit exit
  27. #endif
  28. #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
  29. #include "mbedtls/error.h"
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #endif
  34. #define USAGE \
  35. "\n usage: strerror <errorcode>\n" \
  36. "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
  37. #if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY)
  38. int main( void )
  39. {
  40. mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n");
  41. mbedtls_exit( 0 );
  42. }
  43. #else
  44. int main( int argc, char *argv[] )
  45. {
  46. long int val;
  47. char *end = argv[1];
  48. if( argc != 2 )
  49. {
  50. mbedtls_printf( USAGE );
  51. mbedtls_exit( 0 );
  52. }
  53. val = strtol( argv[1], &end, 10 );
  54. if( *end != '\0' )
  55. {
  56. val = strtol( argv[1], &end, 16 );
  57. if( *end != '\0' )
  58. {
  59. mbedtls_printf( USAGE );
  60. return( 0 );
  61. }
  62. }
  63. if( val > 0 )
  64. val = -val;
  65. if( val != 0 )
  66. {
  67. char error_buf[200];
  68. mbedtls_strerror( val, error_buf, 200 );
  69. mbedtls_printf("Last error was: -0x%04x - %s\n\n", (unsigned int) -val, error_buf );
  70. }
  71. #if defined(_WIN32)
  72. mbedtls_printf( " + Press Enter to exit this program.\n" );
  73. fflush( stdout ); getchar();
  74. #endif
  75. mbedtls_exit( val );
  76. }
  77. #endif /* MBEDTLS_ERROR_C */