strerror.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #include "mbedtls/platform.h"
  21. #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
  22. #include "mbedtls/error.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #endif
  27. #define USAGE \
  28. "\n usage: strerror <errorcode>\n" \
  29. "\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
  30. #if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY)
  31. int main(void)
  32. {
  33. mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n");
  34. mbedtls_exit(0);
  35. }
  36. #else
  37. int main(int argc, char *argv[])
  38. {
  39. long int val;
  40. char *end = argv[1];
  41. if (argc != 2) {
  42. mbedtls_printf(USAGE);
  43. mbedtls_exit(0);
  44. }
  45. val = strtol(argv[1], &end, 10);
  46. if (*end != '\0') {
  47. val = strtol(argv[1], &end, 16);
  48. if (*end != '\0') {
  49. mbedtls_printf(USAGE);
  50. return 0;
  51. }
  52. }
  53. if (val > 0) {
  54. val = -val;
  55. }
  56. if (val != 0) {
  57. char error_buf[200];
  58. mbedtls_strerror(val, error_buf, 200);
  59. mbedtls_printf("Last error was: -0x%04x - %s\n\n", (unsigned int) -val, error_buf);
  60. }
  61. mbedtls_exit(val);
  62. }
  63. #endif /* MBEDTLS_ERROR_C */