pathsearch.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. @file
  3. @ingroup util
  4. @brief Search in PATH.
  5. @copyright@parblock
  6. Copyright (c) 1994-1998 The Regents of the Univ. of California.
  7. All rights reserved.
  8. Permission is hereby granted, without written agreement and without license
  9. or royalty fees, to use, copy, modify, and distribute this software and its
  10. documentation for any purpose, provided that the above copyright notice and
  11. the following two paragraphs appear in all copies of this software.
  12. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  13. DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  14. OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  15. CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  17. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  18. FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
  19. "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE
  20. MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  21. @endparblock
  22. @copyright@parblock
  23. Copyright (c) 1999-2015, Regents of the University of Colorado
  24. All rights reserved.
  25. Redistribution and use in source and binary forms, with or without
  26. modification, are permitted provided that the following conditions
  27. are met:
  28. Redistributions of source code must retain the above copyright
  29. notice, this list of conditions and the following disclaimer.
  30. Redistributions in binary form must reproduce the above copyright
  31. notice, this list of conditions and the following disclaimer in the
  32. documentation and/or other materials provided with the distribution.
  33. Neither the name of the University of Colorado nor the names of its
  34. contributors may be used to endorse or promote products derived from
  35. this software without specific prior written permission.
  36. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  37. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  38. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  39. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  40. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  41. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  42. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  44. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  45. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  46. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  47. POSSIBILITY OF SUCH DAMAGE.
  48. @endparblock
  49. */
  50. #include "util.h"
  51. /** \cond */
  52. static int check_file (char const *, char const *);
  53. /** \endcond */
  54. /**
  55. * @brief Looks for a program in the directories specified by PATH.
  56. */
  57. char *
  58. util_path_search(char const *prog)
  59. {
  60. #ifdef UNIX
  61. return util_file_search(prog, getenv("PATH"), (char *) "x");
  62. #else
  63. return util_file_search(prog, NIL(char), (char *) "x");
  64. #endif
  65. }
  66. /**
  67. * @brief Searches for a file given a set of paths.
  68. */
  69. char *
  70. util_file_search(
  71. char const *file, /**< file we're looking for */
  72. char *path, /**< search path, colon separated */
  73. char const *mode /**< "r", "w", or "x" */)
  74. {
  75. int quit;
  76. char *buffer, *filename, *save_path, *cp;
  77. if (path == 0 || strcmp(path, "") == 0) {
  78. path = (char *) "."; /* just look in the current directory */
  79. }
  80. save_path = path = util_strsav(path);
  81. quit = 0;
  82. do {
  83. cp = strchr(path, ':');
  84. if (cp != 0) {
  85. *cp = '\0';
  86. } else {
  87. quit = 1;
  88. }
  89. /* cons up the filename out of the path and file name */
  90. if (strcmp(path, ".") == 0) {
  91. buffer = util_strsav(file);
  92. } else {
  93. buffer = ALLOC(char, strlen(path) + strlen(file) + 4);
  94. (void) sprintf(buffer, "%s/%s", path, file);
  95. }
  96. filename = util_tilde_expand(buffer);
  97. FREE(buffer);
  98. /* see if we can access it */
  99. if (check_file(filename, mode)) {
  100. FREE(save_path);
  101. return filename;
  102. }
  103. FREE(filename);
  104. path = ++cp;
  105. } while (! quit);
  106. FREE(save_path);
  107. return 0;
  108. }
  109. /**
  110. * @brief Checks user permissions for a file.
  111. */
  112. static int
  113. check_file(char const *filename, char const *mode)
  114. {
  115. #ifdef UNIX
  116. int access_mode = /*F_OK*/ 0;
  117. if (strcmp(mode, "r") == 0) {
  118. access_mode = /*R_OK*/ 4;
  119. } else if (strcmp(mode, "w") == 0) {
  120. access_mode = /*W_OK*/ 2;
  121. } else if (strcmp(mode, "x") == 0) {
  122. access_mode = /*X_OK*/ 1;
  123. }
  124. return access(filename, access_mode) == 0;
  125. #else
  126. FILE *fp;
  127. int got_file;
  128. if (strcmp(mode, "x") == 0) {
  129. mode = "r";
  130. }
  131. fp = fopen(filename, mode);
  132. got_file = (fp != 0);
  133. if (fp != 0) {
  134. (void) fclose(fp);
  135. }
  136. return got_file;
  137. #endif
  138. }