pathfind.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* -*- Mode: C -*- */
  2. /* pathfind.c --- find a FILE MODE along PATH */
  3. /*
  4. * Author: Gary V Vaughan <gvaughan@oranda.demon.co.uk>
  5. * Time-stamp: "2006-09-23 19:46:16 bkorb"
  6. * Last Modified: $Date: 2007/10/07 16:54:54 $
  7. * by: bkorb
  8. *
  9. * $Id: pathfind.c,v 4.12 2007/10/07 16:54:54 bkorb Exp $
  10. */
  11. /* Code: */
  12. #include "compat.h"
  13. #ifndef HAVE_PATHFIND
  14. #if defined(__windows__) && !defined(__CYGWIN__)
  15. char*
  16. pathfind( char const* path,
  17. char const* fileName,
  18. char const* mode )
  19. {
  20. return NULL;
  21. }
  22. #else
  23. static char* make_absolute( char const *string, char const *dot_path );
  24. static char* canonicalize_pathname( char *path );
  25. static char* extract_colon_unit( char* dir, char const *string, int *p_index );
  26. /*=export_func pathfind
  27. *
  28. * what: fild a file in a list of directories
  29. *
  30. * ifndef: HAVE_PATHFIND
  31. *
  32. * arg: + char const* + path + colon separated list of search directories +
  33. * arg: + char const* + file + the name of the file to look for +
  34. * arg: + char const* + mode + the mode bits that must be set to match +
  35. *
  36. * ret_type: char*
  37. * ret_desc: the path to the located file
  38. *
  39. * doc:
  40. *
  41. * pathfind looks for a a file with name "FILE" and "MODE" access
  42. * along colon delimited "PATH", and returns the full pathname as a
  43. * string, or NULL if not found. If "FILE" contains a slash, then
  44. * it is treated as a relative or absolute path and "PATH" is ignored.
  45. *
  46. * @strong{NOTE}: this function is compiled into @file{libopts} only if
  47. * it is not natively supplied.
  48. *
  49. * The "MODE" argument is a string of option letters chosen from the
  50. * list below:
  51. * @example
  52. * Letter Meaning
  53. * r readable
  54. * w writable
  55. * x executable
  56. * f normal file (NOT IMPLEMENTED)
  57. * b block special (NOT IMPLEMENTED)
  58. * c character special (NOT IMPLEMENTED)
  59. * d directory (NOT IMPLEMENTED)
  60. * p FIFO (pipe) (NOT IMPLEMENTED)
  61. * u set user ID bit (NOT IMPLEMENTED)
  62. * g set group ID bit (NOT IMPLEMENTED)
  63. * k sticky bit (NOT IMPLEMENTED)
  64. * s size nonzero (NOT IMPLEMENTED)
  65. * @end example
  66. *
  67. * example:
  68. * To find the "ls" command using the "PATH" environment variable:
  69. * @example
  70. * #include <stdlib.h>
  71. * char* pz_ls = pathfind( getenv("PATH"), "ls", "rx" );
  72. * <<do whatever with pz_ls>>
  73. * free( pz_ls );
  74. * @end example
  75. * The path is allocated with @code{malloc(3C)}, so you must @code{free(3C)}
  76. * the result. Also, do not use unimplemented file modes. :-)
  77. *
  78. * err: returns NULL if the file is not found.
  79. =*/
  80. char*
  81. pathfind( char const* path,
  82. char const* fileName,
  83. char const* mode )
  84. {
  85. int p_index = 0;
  86. int mode_bits = 0;
  87. char* pathName = NULL;
  88. char zPath[ AG_PATH_MAX + 1 ];
  89. if (strchr( mode, 'r' )) mode_bits |= R_OK;
  90. if (strchr( mode, 'w' )) mode_bits |= W_OK;
  91. if (strchr( mode, 'x' )) mode_bits |= X_OK;
  92. /*
  93. * FOR each non-null entry in the colon-separated path, DO ...
  94. */
  95. for (;;) {
  96. DIR* dirP;
  97. char* colon_unit = extract_colon_unit( zPath, path, &p_index );
  98. /*
  99. * IF no more entries, THEN quit
  100. */
  101. if (colon_unit == NULL)
  102. break;
  103. dirP = opendir( colon_unit );
  104. /*
  105. * IF the directory is inaccessable, THEN next directory
  106. */
  107. if (dirP == NULL)
  108. continue;
  109. /*
  110. * FOR every entry in the given directory, ...
  111. */
  112. for (;;) {
  113. struct dirent *entP = readdir( dirP );
  114. if (entP == (struct dirent*)NULL)
  115. break;
  116. /*
  117. * IF the file name matches the one we are looking for, ...
  118. */
  119. if (strcmp( entP->d_name, fileName ) == 0) {
  120. char* pzFullName = make_absolute( fileName, colon_unit);
  121. /*
  122. * Make sure we can access it in the way we want
  123. */
  124. if (access( pzFullName, mode_bits ) >= 0) {
  125. /*
  126. * We can, so normalize the name and return it below
  127. */
  128. pathName = canonicalize_pathname( pzFullName );
  129. }
  130. free( (void*)pzFullName );
  131. break;
  132. }
  133. }
  134. closedir( dirP );
  135. if (pathName != NULL)
  136. break;
  137. }
  138. return pathName;
  139. }
  140. /*
  141. * Turn STRING (a pathname) into an absolute pathname, assuming that
  142. * DOT_PATH contains the symbolic location of `.'. This always returns
  143. * a new string, even if STRING was an absolute pathname to begin with.
  144. */
  145. static char*
  146. make_absolute( char const *string, char const *dot_path )
  147. {
  148. char *result;
  149. int result_len;
  150. if (!dot_path || *string == '/') {
  151. result = strdup( string );
  152. } else {
  153. if (dot_path && dot_path[0]) {
  154. result = malloc( 2 + strlen( dot_path ) + strlen( string ) );
  155. strcpy( result, dot_path );
  156. result_len = strlen( result );
  157. if (result[result_len - 1] != '/') {
  158. result[result_len++] = '/';
  159. result[result_len] = '\0';
  160. }
  161. } else {
  162. result = malloc( 3 + strlen( string ) );
  163. result[0] = '.'; result[1] = '/'; result[2] = '\0';
  164. result_len = 2;
  165. }
  166. strcpy( result + result_len, string );
  167. }
  168. return result;
  169. }
  170. /*
  171. * Canonicalize PATH, and return a new path. The new path differs from
  172. * PATH in that:
  173. *
  174. * Multiple `/'s are collapsed to a single `/'.
  175. * Leading `./'s are removed.
  176. * Trailing `/.'s are removed.
  177. * Trailing `/'s are removed.
  178. * Non-leading `../'s and trailing `..'s are handled by removing
  179. * portions of the path.
  180. */
  181. static char*
  182. canonicalize_pathname( char *path )
  183. {
  184. int i, start;
  185. char stub_char, *result;
  186. /* The result cannot be larger than the input PATH. */
  187. result = strdup( path );
  188. stub_char = (*path == '/') ? '/' : '.';
  189. /* Walk along RESULT looking for things to compact. */
  190. i = 0;
  191. while (result[i]) {
  192. while (result[i] != '\0' && result[i] != '/')
  193. i++;
  194. start = i++;
  195. /* If we didn't find any slashes, then there is nothing left to
  196. * do.
  197. */
  198. if (!result[start])
  199. break;
  200. /* Handle multiple `/'s in a row. */
  201. while (result[i] == '/')
  202. i++;
  203. #if !defined (apollo)
  204. if ((start + 1) != i)
  205. #else
  206. if ((start + 1) != i && (start != 0 || i != 2))
  207. #endif /* apollo */
  208. {
  209. strcpy( result + start + 1, result + i );
  210. i = start + 1;
  211. }
  212. /* Handle backquoted `/'. */
  213. if (start > 0 && result[start - 1] == '\\')
  214. continue;
  215. /* Check for trailing `/', and `.' by itself. */
  216. if ((start && !result[i])
  217. || (result[i] == '.' && !result[i+1])) {
  218. result[--i] = '\0';
  219. break;
  220. }
  221. /* Check for `../', `./' or trailing `.' by itself. */
  222. if (result[i] == '.') {
  223. /* Handle `./'. */
  224. if (result[i + 1] == '/') {
  225. strcpy( result + i, result + i + 1 );
  226. i = (start < 0) ? 0 : start;
  227. continue;
  228. }
  229. /* Handle `../' or trailing `..' by itself. */
  230. if (result[i + 1] == '.' &&
  231. (result[i + 2] == '/' || !result[i + 2])) {
  232. while (--start > -1 && result[start] != '/')
  233. ;
  234. strcpy( result + start + 1, result + i + 2 );
  235. i = (start < 0) ? 0 : start;
  236. continue;
  237. }
  238. }
  239. }
  240. if (!*result) {
  241. *result = stub_char;
  242. result[1] = '\0';
  243. }
  244. return result;
  245. }
  246. /*
  247. * Given a string containing units of information separated by colons,
  248. * return the next one pointed to by (P_INDEX), or NULL if there are no
  249. * more. Advance (P_INDEX) to the character after the colon.
  250. */
  251. static char*
  252. extract_colon_unit( char* pzDir, char const *string, int *p_index )
  253. {
  254. char* pzDest = pzDir;
  255. int ix = *p_index;
  256. if (string == NULL)
  257. return NULL;
  258. if ((unsigned)ix >= strlen( string ))
  259. return NULL;
  260. {
  261. char const* pzSrc = string + ix;
  262. while (*pzSrc == ':') pzSrc++;
  263. for (;;) {
  264. char ch = (*(pzDest++) = *(pzSrc++));
  265. switch (ch) {
  266. case ':':
  267. pzDest[-1] = NUL;
  268. case NUL:
  269. goto copy_done;
  270. }
  271. if ((pzDest - pzDir) >= AG_PATH_MAX)
  272. break;
  273. } copy_done:;
  274. ix = pzSrc - string;
  275. }
  276. if (*pzDir == NUL)
  277. return NULL;
  278. *p_index = ix;
  279. return pzDir;
  280. }
  281. #endif /* __windows__ / __CYGWIN__ */
  282. #endif /* HAVE_PATHFIND */
  283. /*
  284. * Local Variables:
  285. * mode: C
  286. * c-file-style: "stroustrup"
  287. * indent-tabs-mode: nil
  288. * End:
  289. * end of compat/pathfind.c */