list.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <dirent.h>
  4. #include <sys/stat.h>
  5. #include <grass/gis.h>
  6. #include <grass/glocale.h>
  7. #include "proto.h"
  8. /* get monitor path */
  9. char *get_path(const char *name, int fpath)
  10. {
  11. char tmpdir[GPATH_MAX];
  12. G_temp_element(tmpdir);
  13. strcat(tmpdir, "/");
  14. strcat(tmpdir, "MONITORS");
  15. if (name) {
  16. strcat(tmpdir, "/");
  17. strcat(tmpdir, name);
  18. }
  19. if (fpath) {
  20. char ret[GPATH_MAX];
  21. G_file_name(ret, tmpdir, NULL, G_mapset());
  22. return G_store(ret);
  23. }
  24. return G_store(tmpdir);
  25. }
  26. /* get list of running monitors */
  27. void list_mon(char ***list, int *n)
  28. {
  29. char *mon_path;
  30. struct dirent *dp;
  31. DIR *dirp;
  32. struct stat s;
  33. *list = NULL;
  34. *n = 0;
  35. mon_path = get_path(NULL, TRUE);
  36. dirp = opendir(mon_path);
  37. G_free(mon_path);
  38. if (!dirp)
  39. return;
  40. while ((dp = readdir(dirp)) != NULL) {
  41. int ret;
  42. if (!dp->d_name || dp->d_name[0] == '.')
  43. continue;
  44. mon_path = get_path(dp->d_name, TRUE);
  45. ret = G_stat(mon_path, &s);
  46. G_free(mon_path);
  47. if (ret != 0 || !S_ISDIR(s.st_mode))
  48. continue;
  49. *list = G_realloc(*list, (*n + 1) * sizeof(char *));
  50. (*list)[*n] = dp->d_name;
  51. (*n)++;
  52. }
  53. closedir(dirp);
  54. }
  55. /* print list of running monitors */
  56. void print_list(FILE *fd)
  57. {
  58. char **list;
  59. int i, n;
  60. list_mon(&list, &n);
  61. if (n > 0)
  62. G_message(_("List of running monitors:"));
  63. else {
  64. G_important_message(_("No monitors running"));
  65. return;
  66. }
  67. for (i = 0; i < n; i++)
  68. fprintf(fd, "%s\n", list[i]);
  69. }
  70. /* check if monitor is running */
  71. int check_mon(const char *name)
  72. {
  73. char **list;
  74. int i, n;
  75. list_mon(&list, &n);
  76. for (i = 0; i < n; i++)
  77. if (G_strcasecmp(list[i], name) == 0)
  78. return TRUE;
  79. return FALSE;
  80. }
  81. /* list related commands for given monitor */
  82. void list_cmd(const char *name, FILE *fd_out)
  83. {
  84. char *mon_path;
  85. char cmd_file[GPATH_MAX], buf[4096];
  86. FILE *fd;
  87. mon_path = get_path(name, FALSE);
  88. G_file_name(cmd_file, mon_path, "cmd", G_mapset());
  89. fd = fopen(cmd_file, "r");
  90. if (!fd)
  91. G_fatal_error(_("Unable to open file '%s'"), cmd_file);
  92. while (G_getl2(buf, sizeof(buf) - 1, fd) != 0) {
  93. fprintf(fd_out, "%s\n", buf);
  94. }
  95. fclose(fd);
  96. G_free(mon_path);
  97. }
  98. void list_files(const char *name, FILE *fd_out)
  99. {
  100. char *p;
  101. char tmpdir[GPATH_MAX], mon_path[GPATH_MAX];
  102. struct dirent *dp;
  103. DIR *dirp;
  104. G_temp_element(tmpdir);
  105. strcat(tmpdir, "/");
  106. strcat(tmpdir, "MONITORS");
  107. strcat(tmpdir, "/");
  108. strcat(tmpdir, name);
  109. G_file_name(mon_path, tmpdir, NULL, G_mapset());
  110. fprintf(fd_out, "path=%s\n", mon_path);
  111. dirp = opendir(mon_path);
  112. if (!dirp)
  113. G_fatal_error(_("No support files found for monitor <%s>"), name);
  114. while ((dp = readdir(dirp)) != NULL) {
  115. if (!dp->d_name || dp->d_name[0] == '.')
  116. continue;
  117. p = strrchr(dp->d_name, '.');
  118. if (!p)
  119. p = dp->d_name;
  120. else
  121. p++; /* skip '.' */
  122. fprintf(fd_out, "%s=%s%c%s\n", p,
  123. mon_path, HOST_DIRSEP, dp->d_name);
  124. }
  125. }