tempfile.c 2.6 KB

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