strerror.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #if !defined(MBEDTLS_CONFIG_FILE)
  20. #include "mbedtls/config.h"
  21. #else
  22. #include MBEDTLS_CONFIG_FILE
  23. #endif
  24. #if defined(MBEDTLS_PLATFORM_C)
  25. #include "mbedtls/platform.h"
  26. #else
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #define mbedtls_printf printf
  30. #define mbedtls_exit exit
  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. mbedtls_exit( 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. mbedtls_exit( 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", (unsigned 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. mbedtls_exit( val );
  80. }
  81. #endif /* MBEDTLS_ERROR_C */