GRASS GIS 7 Programmer's Manual  7.0.5(2016)-r00000
tempfile.c
Go to the documentation of this file.
1 
14 #include <string.h>
15 #include <unistd.h>
16 #include <sys/stat.h>
17 #include <grass/gis.h>
18 
19 #include "local_proto.h"
20 
21 static struct Counter unique;
22 static int initialized;
23 
27 void G_init_tempfile(void)
28 {
29  if (G_is_initialized(&initialized))
30  return;
31 
32  G_init_counter(&unique, 0);
33 
34  G_initialize_done(&initialized);
35 }
36 
60 char *G_tempfile(void)
61 {
62  return G_tempfile_pid(getpid());
63 }
64 
73 char *G_tempfile_pid(int pid)
74 {
75  char path[GPATH_MAX];
76  char name[GNAME_MAX];
77  char element[100];
78 
79  if (pid <= 0)
80  pid = getpid();
81  G_temp_element(element);
83  do {
84  int uniq = G_counter_next(&unique);
85  sprintf(name, "%d.%d", pid, uniq);
86  G_file_name(path, element, name, G_mapset());
87  }
88  while (access(path, F_OK) == 0);
89 
90  return G_store(path);
91 }
92 
99 {
100  const char *machine;
101 
102  strcpy(element, ".tmp");
103  machine = G__machine_name();
104  if (machine != NULL && *machine != 0) {
105  strcat(element, "/");
106  strcat(element, machine);
107  }
108  G_make_mapset_element(element);
109 }
int G_make_mapset_element(const char *p_element)
Create element in the current mapset.
Definition: mapset_msc.c:35
void G_init_counter(struct Counter *c, int v)
Definition: counter.c:38
const char * G_mapset(void)
Get current mapset name.
Definition: mapset.c:33
char * G_tempfile_pid(int pid)
Create tempfile from process id.
Definition: tempfile.c:73
void G_temp_element(char *element)
Populates element with a path string.
Definition: tempfile.c:98
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:86
char * G_tempfile(void)
Returns a temporary file name.
Definition: tempfile.c:60
int G_is_initialized(int *p)
Definition: counter.c:59
#define NULL
Definition: ccmath.h:32
void G_initialize_done(int *p)
Definition: counter.c:76
Definition: lidar.h:89
const char * G__machine_name(void)
Definition: mach_name.c:17
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:33
void G_init_tempfile(void)
Initialize environment for creating tempfiles.
Definition: tempfile.c:27
Definition: path.h:16
int G_counter_next(struct Counter *c)
Definition: counter.c:46
const char * name
Definition: named_colr.c:7