GRASS GIS 8 Programmer's Manual 8.2.1(2023)-exported
tempfile.c
Go to the documentation of this file.
1/*!
2 * \file lib/gis/tempfile.c
3 *
4 * \brief GIS Library - Temporary file functions.
5 *
6 * (C) 2001-2015 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
14#include <string.h>
15#include <unistd.h>
16#include <sys/stat.h>
17#include <stdlib.h>
18
19#include <grass/gis.h>
20
21#include "gis_local_proto.h"
22
23static struct Counter unique;
24static int initialized;
25
26/*!
27 \brief Initialize environment for creating tempfiles.
28*/
30{
31 if (G_is_initialized(&initialized))
32 return;
33
34 G_init_counter(&unique, 0);
35
36 G_initialize_done(&initialized);
37}
38
39/*!
40 * \brief Returns a temporary file name.
41 *
42 * This routine returns a pointer to a string containing a unique
43 * temporary file name that can be used as a temporary file within the
44 * module. Successive calls to G_tempfile() will generate new
45 * names. Only the file name is generated. The file itself is not
46 * created. To create the file, the module must use standard UNIX
47 * functions which create and open files, e.g., <i>creat()</i> or
48 * <i>fopen()</i>.
49 *
50 * Successive calls will generate different names the names are of the
51 * form pid.n where pid is the programs process id number and n is a
52 * unique identifier.
53 *
54 * <b>Note:</b> It is recommended to <i>unlink()</i> (remove) the
55 * temp file on exit/error. Only if GRASS is left with 'exit', the GIS
56 * mapset management will clean up the temp directory (ETC/clean_temp).
57 *
58 * \return pointer to a character string containing the name. The name
59 * is copied to allocated memory and may be released by the unix free()
60 * routine.
61 */
62char *G_tempfile(void)
63{
64 return G_tempfile_pid(getpid());
65}
66
67/*!
68 * \brief Create tempfile from process id.
69 *
70 * See G_tempfile().
71 *
72 * \param pid
73 * \return pointer to string path
74 */
75char *G_tempfile_pid(int pid)
76{
77 char path[GPATH_MAX];
78 char name[GNAME_MAX];
79 char element[100];
80
81 if (pid <= 0)
82 pid = getpid();
85 do {
86 int uniq = G_counter_next(&unique);
87 sprintf(name, "%d.%d", pid, uniq);
89 }
90 while (access(path, F_OK) == 0);
91
92 G_debug(2, "G_tempfile_pid(): %s", path);
93
94 return G_store(path);
95}
96
97/*!
98 * \brief Populates element with a path string.
99 *
100 * \param[out] element element name
101 */
103{
105}
106
107/*!
108 * \brief Populates element with a path string (internal use only!)
109 *
110 * \param[out] element element name
111 * \param tmp TRUE to use G_make_mapset_element_tmp() instead of G_make_mapset_element()
112 */
113void G__temp_element(char *element, int tmp)
114{
115 const char *machine;
116
117 strcpy(element, ".tmp");
118 machine = G__machine_name();
119 if (machine != NULL && *machine != 0) {
120 strcat(element, "/");
121 strcat(element, machine);
122 }
123
124 if (!tmp)
126 else
128
129 G_debug(2, "G__temp_element(): %s (tmp=%d)", element, tmp);
130}
#define NULL
Definition: ccmath.h:32
void G_initialize_done(int *p)
Definition: counter.c:76
void G_init_counter(struct Counter *c, int v)
Definition: counter.c:38
int G_is_initialized(int *p)
Definition: counter.c:59
int G_counter_next(struct Counter *c)
Definition: counter.c:46
#define FALSE
Definition: dbfopen.c:182
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
char * G_file_name(char *path, const char *element, const char *name, const char *mapset)
Builds full path names to GIS data files.
Definition: file_name.c:61
const char * G__machine_name(void)
Definition: mach_name.c:17
const char * G_mapset(void)
Get current mapset name.
Definition: mapset.c:33
int G_make_mapset_object_group_tmp(const char *type)
Create directory for type of objects in the temporary directory.
Definition: mapset_msc.c:153
int G_make_mapset_object_group(const char *type)
Create directory for group of elements of a given type.
Definition: mapset_msc.c:74
const char * name
Definition: named_colr.c:7
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:87
Definition: lidar.h:87
Definition: path.h:16
void G_temp_element(char *element)
Populates element with a path string.
Definition: tempfile.c:102
char * G_tempfile_pid(int pid)
Create tempfile from process id.
Definition: tempfile.c:75
void G__temp_element(char *element, int tmp)
Populates element with a path string (internal use only!)
Definition: tempfile.c:113
char * G_tempfile(void)
Returns a temporary file name.
Definition: tempfile.c:62
void G_init_tempfile(void)
Initialize environment for creating tempfiles.
Definition: tempfile.c:29