tool_libinfo.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "tool_setup.h"
  25. #include "strcase.h"
  26. #define ENABLE_CURLX_PRINTF
  27. /* use our own printf() functions */
  28. #include "curlx.h"
  29. #include "tool_libinfo.h"
  30. #include "memdebug.h" /* keep this as LAST include */
  31. /* global variable definitions, for libcurl run-time info */
  32. static const char *no_protos = NULL;
  33. curl_version_info_data *curlinfo = NULL;
  34. const char * const *built_in_protos = &no_protos;
  35. size_t proto_count = 0;
  36. const char *proto_file = NULL;
  37. const char *proto_ftp = NULL;
  38. const char *proto_ftps = NULL;
  39. const char *proto_http = NULL;
  40. const char *proto_https = NULL;
  41. const char *proto_rtsp = NULL;
  42. const char *proto_scp = NULL;
  43. const char *proto_sftp = NULL;
  44. const char *proto_tftp = NULL;
  45. static struct proto_name_tokenp {
  46. const char *proto_name;
  47. const char **proto_tokenp;
  48. } const possibly_built_in[] = {
  49. { "file", &proto_file },
  50. { "ftp", &proto_ftp },
  51. { "ftps", &proto_ftps },
  52. { "http", &proto_http },
  53. { "https", &proto_https },
  54. { "rtsp", &proto_rtsp },
  55. { "scp", &proto_scp },
  56. { "sftp", &proto_sftp },
  57. { "tftp", &proto_tftp },
  58. { NULL, NULL }
  59. };
  60. /*
  61. * libcurl_info_init: retrieves run-time information about libcurl,
  62. * setting a global pointer 'curlinfo' to libcurl's run-time info
  63. * struct, count protocols and flag those we are interested in.
  64. */
  65. CURLcode get_libcurl_info(void)
  66. {
  67. CURLcode result = CURLE_OK;
  68. /* Pointer to libcurl's run-time version information */
  69. curlinfo = curl_version_info(CURLVERSION_NOW);
  70. if(!curlinfo)
  71. return CURLE_FAILED_INIT;
  72. if(curlinfo->protocols) {
  73. const char *const *builtin;
  74. const struct proto_name_tokenp *p;
  75. built_in_protos = curlinfo->protocols;
  76. for(builtin = built_in_protos; !result && *builtin; builtin++) {
  77. /* Identify protocols we are interested in. */
  78. for(p = possibly_built_in; p->proto_name; p++)
  79. if(curl_strequal(p->proto_name, *builtin)) {
  80. *p->proto_tokenp = *builtin;
  81. break;
  82. }
  83. }
  84. proto_count = builtin - built_in_protos;
  85. }
  86. return CURLE_OK;
  87. }
  88. /* Tokenize a protocol name.
  89. * Return the address of the protocol name listed by the library, or NULL if
  90. * not found.
  91. * Although this may seem useless, this always returns the same address for
  92. * a given protocol and thus allows comparing pointers rather than strings.
  93. * In addition, the returned pointer is not deallocated until the program ends.
  94. */
  95. const char *proto_token(const char *proto)
  96. {
  97. const char * const *builtin;
  98. if(!proto)
  99. return NULL;
  100. for(builtin = built_in_protos; *builtin; builtin++)
  101. if(curl_strequal(*builtin, proto))
  102. break;
  103. return *builtin;
  104. }