tempfile.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * \file tempfile.c
  3. *
  4. * \brief GIS Library - Temporary file functions.
  5. *
  6. * (C) 2001-2008 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 1999-2008
  14. */
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <sys/stat.h>
  18. #include <grass/gis.h>
  19. static struct Counter unique;
  20. static int initialized;
  21. void G_init_tempfile(void)
  22. {
  23. if (initialized)
  24. return;
  25. G_init_counter(&unique, 0);
  26. initialized = 1;
  27. }
  28. /**
  29. * \brief Returns a temporary file name.
  30. *
  31. * This routine returns a pointer to a string containing a unique
  32. * temporary file name that can be used as a temporary file within the
  33. * module. Successive calls to <i>G_tempfile()</i> will generate new
  34. * names. Only the file name is generated. The file itself is not
  35. * created. To create the file, the module must use standard UNIX
  36. * functions which create and open files, e.g., <i>creat()</i> or
  37. * <i>fopen()</i>.<br>
  38. *
  39. * Successive calls will generate different names the names are of the
  40. * form pid.n where pid is the programs process id number and n is a
  41. * unique identifier.<br>
  42. *
  43. * <b>Note:</b> It is recommended to <i>unlink()</i> (remove) the
  44. * temp file on exit/error. Only if GRASS is left with 'exit', the GIS
  45. * mapset manangement will clean up the temp directory (ETC/clean_temp).
  46. *
  47. * \return pointer to a character string containing the name. The name
  48. * is copied to allocated memory and may be released by the unix free()
  49. * routine.
  50. */
  51. char *G_tempfile(void)
  52. {
  53. return G__tempfile(getpid());
  54. }
  55. /**
  56. * \brief Create tempfile from process id.
  57. *
  58. * See <i>G_tempfile()</i>.
  59. *
  60. * \param[in] pid
  61. * \return Pointer to string path
  62. */
  63. char *G__tempfile(int pid)
  64. {
  65. char path[GPATH_MAX];
  66. char name[GNAME_MAX];
  67. char element[100];
  68. struct stat st;
  69. if (pid <= 0)
  70. pid = getpid();
  71. G__temp_element(element);
  72. G_init_tempfile();
  73. do {
  74. int uniq = G_counter_next(&unique);
  75. sprintf(name, "%d.%d", pid, uniq);
  76. G__file_name(path, element, name, G_mapset());
  77. }
  78. while (stat(path, &st) == 0);
  79. return G_store(path);
  80. }
  81. /**
  82. * \brief Populates <b>element</b> with a path string.
  83. *
  84. * \param[in,out] element
  85. * \return
  86. */
  87. void G__temp_element(char *element)
  88. {
  89. const char *machine;
  90. strcpy(element, ".tmp");
  91. machine = G__machine_name();
  92. if (machine != NULL && *machine != 0) {
  93. strcat(element, "/");
  94. strcat(element, machine);
  95. }
  96. G__make_mapset_element(element);
  97. }