target.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <unistd.h>
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include "globals.h"
  5. /* read the target for the group and cast it into the alternate GRASS env */
  6. static int which_env;
  7. int get_target(void)
  8. {
  9. char location[GNAME_MAX];
  10. char mapset[GMAPSET_MAX];
  11. char buf[1024];
  12. int stat;
  13. if (!I_get_target(group.name, location, mapset)) {
  14. sprintf(buf, "Target information for group [%s] missing\n",
  15. group.name);
  16. goto error;
  17. }
  18. sprintf(buf, "%s/%s", G_gisdbase(), location);
  19. if (access(buf, 0) != 0) {
  20. sprintf(buf, "Target location [%s] not found\n", location);
  21. goto error;
  22. }
  23. G__create_alt_env();
  24. G__setenv("LOCATION_NAME", location);
  25. stat = G__mapset_permissions(mapset);
  26. if (stat > 0) {
  27. G__setenv("MAPSET", mapset);
  28. G__create_alt_search_path();
  29. G__switch_env();
  30. G__switch_search_path();
  31. which_env = 0;
  32. return 1;
  33. }
  34. sprintf(buf, "Mapset [%s] in target location [%s] - ", mapset, location);
  35. strcat(buf, stat == 0 ? "permission denied\n" : "not found\n");
  36. error:
  37. strcat(buf, "Please run i.target for group ");
  38. strcat(buf, group.name);
  39. G_fatal_error(buf);
  40. }
  41. int select_current_env(void)
  42. {
  43. if (which_env != 0) {
  44. G__switch_env();
  45. G__switch_search_path();
  46. which_env = 0;
  47. }
  48. return 0;
  49. }
  50. int select_target_env(void)
  51. {
  52. if (which_env != 1) {
  53. G__switch_env();
  54. G__switch_search_path();
  55. which_env = 1;
  56. }
  57. return 0;
  58. }