main.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /****************************************************************************
  2. *
  3. * MODULE: i.find
  4. * AUTHOR(S): Markus Neteler <neteler itc.it>
  5. * Bernhard Reiter <bernhard intevation.de>,
  6. * Brad Douglas <rez touchofmadness.com>,
  7. * Glynn Clements <glynn gclements.plus.com>,
  8. * Paul Kelly <paul-grass stjohnspoint.co.uk>
  9. * PURPOSE: produces a file containing the names of files of type
  10. * element (cell, dig, etc) in the search path for the mapset
  11. * in location
  12. * COPYRIGHT: (C) 1999-2006 by the GRASS Development Team
  13. *
  14. * This program is free software under the GNU General Public
  15. * License (>=v2). Read the file COPYING that comes with GRASS
  16. * for details.
  17. *
  18. *****************************************************************************/
  19. /************************************************************************
  20. * usage: i.find location mapset element file [element2 file2]...
  21. *
  22. * produces a file containing the names of files of type
  23. * element (cell, dig, etc) in the search path for the mapset in location
  24. *
  25. * the output file is in the format used by i.ask, which does a popup menu
  26. * of the files, and lets the user select one using the mouse
  27. *
  28. * at present this routine requires that both the current location/mapset
  29. * and the specified location/mapset be valid for the user.
  30. * I hope to remove this requirement some time.
  31. *
  32. * note: the list is created in other file, and when complete it is moved
  33. * to the one specified on the command line. This allows programs
  34. * to run this command in background and check for completion by
  35. * looking for the file.
  36. *
  37. * if there are no files, the list file is not created
  38. *
  39. ***********************************************************************/
  40. #include <dirent.h>
  41. #include <string.h>
  42. #include <stdio.h>
  43. #include <unistd.h>
  44. #include <stdlib.h>
  45. #include <sys/types.h>
  46. #include <grass/gis.h>
  47. #include <grass/glocale.h>
  48. /* function prototypes */
  49. static int find(FILE * fd, char *element);
  50. int main(int argc, char *argv[])
  51. {
  52. char *tempfile;
  53. int n;
  54. if (argc < 5 || argc % 2 == 0)
  55. G_fatal_error(_("usage: %s location mapset element file."), argv[0]);
  56. G_gisinit(argv[0]);
  57. /*
  58. * this code assumes that the SEARCH PATH is not read
  59. * until we call G__mapset_name() in find()
  60. */
  61. tempfile = G_tempfile();
  62. G__setenv("LOCATION_NAME", argv[1]);
  63. G__setenv("MAPSET", argv[2]);
  64. for (n = 3; n < argc; n += 2) {
  65. FILE *fd;
  66. int ok;
  67. /* get this list into a temp file first */
  68. fd = fopen(tempfile, "w");
  69. if (fd == NULL)
  70. G_fatal_error(_("Unable to open temp file."));
  71. remove(argv[n + 1]);
  72. ok = find(fd, argv[n]);
  73. fclose(fd);
  74. /* move the temp file to the real file
  75. * this allows programs to run i.find in the background
  76. * and check for completion by looking for the file
  77. */
  78. if (ok)
  79. G_rename_file(tempfile, argv[n + 1]);
  80. remove(tempfile);
  81. }
  82. G_free(tempfile);
  83. return 0;
  84. }
  85. static int find(FILE * fd, char *element)
  86. {
  87. int len1 = 0, len2 = 0;
  88. const char *mapset;
  89. int n;
  90. G_fseek(fd, 0L, SEEK_SET);
  91. fwrite(&len1, sizeof(len1), 1L, fd);
  92. fwrite(&len2, sizeof(len2), 1L, fd);
  93. for (n = 0; ((mapset = G__mapset_name(n)) != NULL); n++) {
  94. int len;
  95. char dir[1024];
  96. struct dirent *dp;
  97. DIR *dfd;
  98. G_file_name(dir, element, "", mapset);
  99. if ((dfd = opendir(dir)) == NULL)
  100. continue;
  101. len = strlen(mapset);
  102. if (len > len2)
  103. len2 = len;
  104. while ((dp = readdir(dfd)) != NULL) {
  105. if (dp->d_name[0] != '.') {
  106. fprintf(fd, "%s %s\n", dp->d_name, mapset);
  107. len = strlen(dp->d_name);
  108. if (len > len1)
  109. len1 = len;
  110. }
  111. }
  112. closedir(dfd);
  113. }
  114. if (len1 == 0 || len2 == 0)
  115. return 0;
  116. fflush(fd);
  117. G_fseek(fd, 0L, SEEK_SET);
  118. fwrite(&len1, sizeof(len1), 1L, fd);
  119. fwrite(&len2, sizeof(len2), 1L, fd);
  120. return 1;
  121. }