tool_findfile.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. #ifdef HAVE_PWD_H
  26. # undef __NO_NET_API /* required for building for AmigaOS */
  27. # include <pwd.h>
  28. #endif
  29. #ifdef HAVE_SYS_STAT_H
  30. #include <sys/stat.h>
  31. #endif
  32. #ifdef HAVE_FCNTL_H
  33. #include <fcntl.h>
  34. #endif
  35. #include <curl/mprintf.h>
  36. #include "tool_findfile.h"
  37. #include "memdebug.h" /* keep this as LAST include */
  38. struct finder {
  39. const char *env;
  40. const char *append;
  41. bool withoutdot;
  42. };
  43. /* The order of the variables below is important, as the index number is used
  44. in the findfile() function */
  45. static const struct finder list[] = {
  46. { "CURL_HOME", NULL, FALSE },
  47. { "XDG_CONFIG_HOME", NULL, FALSE }, /* index == 1, used in the code */
  48. { "HOME", NULL, FALSE },
  49. #ifdef WIN32
  50. { "USERPROFILE", NULL, FALSE },
  51. { "APPDATA", NULL, FALSE },
  52. { "USERPROFILE", "\\Application Data", FALSE},
  53. #endif
  54. /* these are for .curlrc if XDG_CONFIG_HOME is not defined */
  55. { "CURL_HOME", "/.config", TRUE },
  56. { "HOME", "/.config", TRUE },
  57. { NULL, NULL, FALSE }
  58. };
  59. static char *checkhome(const char *home, const char *fname, bool dotscore)
  60. {
  61. const char pref[2] = { '.', '_' };
  62. int i;
  63. for(i = 0; i < (dotscore ? 2 : 1); i++) {
  64. char *c;
  65. if(dotscore)
  66. c = curl_maprintf("%s" DIR_CHAR "%c%s", home, pref[i], &fname[1]);
  67. else
  68. c = curl_maprintf("%s" DIR_CHAR "%s", home, fname);
  69. if(c) {
  70. int fd = open(c, O_RDONLY);
  71. if(fd >= 0) {
  72. char *path = strdup(c);
  73. close(fd);
  74. curl_free(c);
  75. return path;
  76. }
  77. curl_free(c);
  78. }
  79. }
  80. return NULL;
  81. }
  82. /*
  83. * findfile() - return the full path name of the file.
  84. *
  85. * If 'dotscore' is TRUE, then check for the file first with a leading dot
  86. * and then with a leading underscore.
  87. *
  88. * 1. Iterate over the environment variables in order, and if set, check for
  89. * the given file to be accessed there, then it is a match.
  90. * 2. Non-windows: try getpwuid
  91. */
  92. char *findfile(const char *fname, int dotscore)
  93. {
  94. int i;
  95. bool xdg = FALSE;
  96. DEBUGASSERT(fname && fname[0]);
  97. DEBUGASSERT((dotscore != 1) || (fname[0] == '.'));
  98. if(!fname[0])
  99. return NULL;
  100. for(i = 0; list[i].env; i++) {
  101. char *home = curl_getenv(list[i].env);
  102. if(home) {
  103. char *path;
  104. const char *filename = fname;
  105. if(i == 1 /* XDG_CONFIG_HOME */)
  106. xdg = TRUE;
  107. if(!home[0]) {
  108. curl_free(home);
  109. continue;
  110. }
  111. if(list[i].append) {
  112. char *c = curl_maprintf("%s%s", home, list[i].append);
  113. curl_free(home);
  114. if(!c)
  115. return NULL;
  116. home = c;
  117. }
  118. if(list[i].withoutdot) {
  119. if(!dotscore || xdg) {
  120. /* this is not looking for .curlrc, or the XDG_CONFIG_HOME was
  121. defined so we skip the extended check */
  122. curl_free(home);
  123. continue;
  124. }
  125. filename++; /* move past the leading dot */
  126. dotscore = 0; /* disable it for this check */
  127. }
  128. path = checkhome(home, filename, dotscore ? dotscore - 1 : 0);
  129. curl_free(home);
  130. if(path)
  131. return path;
  132. }
  133. }
  134. #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
  135. {
  136. struct passwd *pw = getpwuid(geteuid());
  137. if(pw) {
  138. char *home = pw->pw_dir;
  139. if(home && home[0])
  140. return checkhome(home, fname, FALSE);
  141. }
  142. }
  143. #endif /* PWD-stuff */
  144. return NULL;
  145. }