ls_groups.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*************************************************************
  2. * I_list_groups (full)
  3. * I_list_subgroups (group, full)
  4. *************************************************************/
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <grass/imagery.h>
  10. #include <grass/spawn.h>
  11. static char *tempfile = NULL;
  12. int I_list_groups(int full)
  13. {
  14. char *element;
  15. int i;
  16. char buf[GPATH_MAX];
  17. char title[50];
  18. FILE *ls, *temp;
  19. struct Ref ref;
  20. int any;
  21. if (tempfile == NULL)
  22. tempfile = G_tempfile();
  23. element = "group";
  24. G__make_mapset_element(element);
  25. temp = fopen(tempfile, "w");
  26. if (temp == NULL)
  27. G_fatal_error("can't open any temp files");
  28. fprintf(temp, "Available groups\n");
  29. fprintf(temp, "---------------------------------\n");
  30. any = 0;
  31. strcpy(buf, "cd ");
  32. G__file_name(buf + strlen(buf), element, "", G_mapset());
  33. strcat(buf, ";ls");
  34. if (!full)
  35. strcat(buf, " -C");
  36. /* FIXME: use G__ls() */
  37. if ((ls = popen(buf, "r"))) {
  38. while (G_getl(buf, sizeof(buf), ls)) {
  39. any = 1;
  40. fprintf(temp, "%s", buf);
  41. if (full) {
  42. I_get_group_title(buf, title, sizeof(title));
  43. if (*title)
  44. fprintf(temp, " (%s)", title);
  45. fprintf(temp, "\n");
  46. I_get_group_ref(buf, &ref);
  47. for (i = 0; i < ref.nfiles; i++)
  48. fprintf(temp, "\t%s in %s\n", ref.file[i].name,
  49. ref.file[i].mapset);
  50. if (ref.nfiles <= 0)
  51. fprintf(temp, "\t** empty **\n");
  52. I_free_group_ref(&ref);
  53. }
  54. else
  55. fprintf(temp, "\n");
  56. }
  57. pclose(ls);
  58. }
  59. if (!any)
  60. fprintf(temp, "no group files available\n");
  61. fprintf(temp, "---------------------------------\n");
  62. fclose(temp);
  63. G_spawn(getenv("GRASS_PAGER"), getenv("GRASS_PAGER"), tempfile, NULL);
  64. remove(tempfile);
  65. return 0;
  66. }
  67. int I_list_subgroups(const char *group, int full)
  68. {
  69. char element[GNAME_MAX + 15];
  70. int i;
  71. char buf[GPATH_MAX];
  72. FILE *ls, *temp;
  73. struct Ref ref;
  74. int any;
  75. if (tempfile == NULL)
  76. tempfile = G_tempfile();
  77. sprintf(element, "group/%s/subgroup", group);
  78. G__make_mapset_element(element);
  79. temp = fopen(tempfile, "w");
  80. if (temp == NULL)
  81. G_fatal_error("Unable to open any temporary file");
  82. fprintf(temp, "Available subgroups in group %s\n", group);
  83. fprintf(temp, "---------------------------------\n");
  84. any = 0;
  85. strcpy(buf, "cd ");
  86. G__file_name(buf + strlen(buf), element, "", G_mapset());
  87. strcat(buf, ";ls");
  88. if (!full)
  89. strcat(buf, " -C");
  90. /* FIXME: use G__ls() */
  91. if ((ls = popen(buf, "r"))) {
  92. while (G_getl(buf, sizeof(buf), ls)) {
  93. any = 1;
  94. fprintf(temp, "%s\n", buf);
  95. if (full) {
  96. I_get_subgroup_ref(group, buf, &ref);
  97. for (i = 0; i < ref.nfiles; i++)
  98. fprintf(temp, "\t%s in %s\n", ref.file[i].name,
  99. ref.file[i].mapset);
  100. if (ref.nfiles <= 0)
  101. fprintf(temp, "\t** empty **\n");
  102. I_free_group_ref(&ref);
  103. }
  104. }
  105. pclose(ls);
  106. }
  107. if (!any)
  108. fprintf(temp, "no subgroup files available\n");
  109. fprintf(temp, "---------------------------------\n");
  110. fclose(temp);
  111. G_spawn(getenv("GRASS_PAGER"), getenv("GRASS_PAGER"), tempfile, NULL);
  112. remove(tempfile);
  113. return 0;
  114. }