is.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*!
  2. * \file lib/gis/is.c
  3. *
  4. * \brief GIS Library - Tests for file existence.
  5. *
  6. * (C) 2001-2014 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author GRASS GIS Development Team
  12. *
  13. * \date 2001-2014
  14. */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include <grass/gis.h>
  19. static int test_path_file(const char *, const char *);
  20. static int test_path_file(const char *path, const char *file)
  21. {
  22. int ret;
  23. char *buf;
  24. buf = (char *)G_malloc(strlen(path) + strlen(file) + 2);
  25. sprintf(buf, "%s/%s", path, file);
  26. ret = access(buf, F_OK);
  27. G_free(buf);
  28. if (ret == 0)
  29. return 1;
  30. return 0;
  31. }
  32. /**
  33. * \brief Test if specified directory is GISBASE.
  34. *
  35. * \param[in] path Path to directory
  36. * \return 1 The directory is GISBASE
  37. * \return 0 The directory is not GISBASE
  38. */
  39. int G_is_gisbase(const char *path)
  40. {
  41. return test_path_file(path, "etc/element_list");
  42. }
  43. /**
  44. * \brief Test if specified directory is location.
  45. *
  46. * \param[in] path Path to directory
  47. * \return 1 The directory is location
  48. * \return 0 The directory is not location
  49. */
  50. int G_is_location(const char *path)
  51. {
  52. return test_path_file(path, "PERMANENT/DEFAULT_WIND");
  53. }
  54. /**
  55. * \brief Test if specified directory is mapset.
  56. *
  57. * \param[in] path Path to directory
  58. * \return 1 The directory is mapset
  59. * \return 0 The directory is not mapset
  60. */
  61. int G_is_mapset(const char *path)
  62. {
  63. return test_path_file(path, "WIND");
  64. }