tempfile.c 2.4 KB

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