tempfile.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #include "local_proto.h"
  18. static struct Counter unique;
  19. static int initialized;
  20. /*!
  21. \brief Initialize environment for creating tempfiles.
  22. */
  23. void G_init_tempfile(void)
  24. {
  25. if (G_is_initialized(&initialized))
  26. return;
  27. G_init_counter(&unique, 0);
  28. G_initialize_done(&initialized);
  29. }
  30. /*!
  31. * \brief Returns a temporary file name.
  32. *
  33. * This routine returns a pointer to a string containing a unique
  34. * temporary file name that can be used as a temporary file within the
  35. * module. Successive calls to G_tempfile() will generate new
  36. * names. Only the file name is generated. The file itself is not
  37. * created. To create the file, the module must use standard UNIX
  38. * functions which create and open files, e.g., <i>creat()</i> or
  39. * <i>fopen()</i>.
  40. *
  41. * Successive calls will generate different names the names are of the
  42. * form pid.n where pid is the programs process id number and n is a
  43. * unique identifier.
  44. *
  45. * <b>Note:</b> It is recommended to <i>unlink()</i> (remove) the
  46. * temp file on exit/error. Only if GRASS is left with 'exit', the GIS
  47. * mapset management will clean up the temp directory (ETC/clean_temp).
  48. *
  49. * \return pointer to a character string containing the name. The name
  50. * is copied to allocated memory and may be released by the unix free()
  51. * routine.
  52. */
  53. char *G_tempfile(void)
  54. {
  55. return G_tempfile_pid(getpid());
  56. }
  57. /*!
  58. * \brief Create tempfile from process id.
  59. *
  60. * See G_tempfile().
  61. *
  62. * \param pid
  63. * \return pointer to string path
  64. */
  65. char *G_tempfile_pid(int pid)
  66. {
  67. char path[GPATH_MAX];
  68. char name[GNAME_MAX];
  69. char element[100];
  70. if (pid <= 0)
  71. pid = getpid();
  72. G_temp_element(element);
  73. G_init_tempfile();
  74. do {
  75. int uniq = G_counter_next(&unique);
  76. sprintf(name, "%d.%d", pid, uniq);
  77. G_file_name(path, element, name, G_mapset());
  78. }
  79. while (access(path, F_OK) == 0);
  80. return G_store(path);
  81. }
  82. /*!
  83. * \brief Populates element with a path string.
  84. *
  85. * \param[out] element element name
  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. }