strerror.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Translate error code to error string
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_PLATFORM_C)
  52. #include "mbedtls/platform.h"
  53. #else
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #define mbedtls_printf printf
  57. #define mbedtls_exit exit
  58. #endif
  59. #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
  60. #include "mbedtls/error.h"
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63. #include <string.h>
  64. #endif
  65. #define USAGE \
  66. "\n usage: strerror <errorcode>\n" \
  67. "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
  68. #if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY)
  69. int main( void )
  70. {
  71. mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n");
  72. mbedtls_exit( 0 );
  73. }
  74. #else
  75. int main( int argc, char *argv[] )
  76. {
  77. long int val;
  78. char *end = argv[1];
  79. if( argc != 2 )
  80. {
  81. mbedtls_printf( USAGE );
  82. mbedtls_exit( 0 );
  83. }
  84. val = strtol( argv[1], &end, 10 );
  85. if( *end != '\0' )
  86. {
  87. val = strtol( argv[1], &end, 16 );
  88. if( *end != '\0' )
  89. {
  90. mbedtls_printf( USAGE );
  91. return( 0 );
  92. }
  93. }
  94. if( val > 0 )
  95. val = -val;
  96. if( val != 0 )
  97. {
  98. char error_buf[200];
  99. mbedtls_strerror( val, error_buf, 200 );
  100. mbedtls_printf("Last error was: -0x%04x - %s\n\n", (int) -val, error_buf );
  101. }
  102. #if defined(_WIN32)
  103. mbedtls_printf( " + Press Enter to exit this program.\n" );
  104. fflush( stdout ); getchar();
  105. #endif
  106. mbedtls_exit( val );
  107. }
  108. #endif /* MBEDTLS_ERROR_C */