find_etc.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <grass/gis.h>
  5. static char *G__find_etc(const char *name)
  6. {
  7. char path[GPATH_MAX];
  8. const char *pathlist = getenv("GRASS_ADDON_ETC");
  9. /*
  10. * reject illegal names
  11. */
  12. if (*name == 0 || *name == '.')
  13. return NULL;
  14. /*
  15. * search paths
  16. */
  17. if (pathlist) {
  18. char **dirs = G_tokenize(pathlist, ":");
  19. char *result = NULL;
  20. int i;
  21. for (i = 0; dirs[i]; i++) {
  22. sprintf(path, "%s/%s", dirs[i], name);
  23. if (access(path, 0) == 0) {
  24. result = G_store(path);
  25. break;
  26. }
  27. }
  28. G_free_tokens(dirs);
  29. if (result)
  30. return result;
  31. }
  32. /*
  33. * check application etc dir
  34. */
  35. sprintf(path, "%s/etc/%s", G_gisbase(), name);
  36. if (access(path, 0) == 0)
  37. return G_store(path);
  38. return NULL;
  39. }
  40. /*!
  41. * \brief searches for a file from the etc search list in GRASS_ADDON_ETC
  42. * returns the full path to where the file was found.
  43. *
  44. * note:
  45. * rejects all names that begin with "."
  46. *
  47. * \param name file name to look for
  48. *
  49. * \return pointer to a string with full path to
  50. * where file was found, or NULL if not found
  51. */
  52. char *G_find_etc(const char *name)
  53. {
  54. return G__find_etc(name);
  55. }