GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
tempfile.c
Go to the documentation of this file.
1 
17 #include <string.h>
18 #include <unistd.h>
19 #include <sys/stat.h>
20 #include <grass/gis.h>
21 
22 
47 char *G_tempfile(void)
48 {
49  return G__tempfile(getpid());
50 }
51 
52 
62 char *G__tempfile(int pid)
63 {
64  char path[GPATH_MAX];
65  char name[GNAME_MAX];
66  char element[100];
67  static int uniq = 0;
68  struct stat st;
69 
70  if (pid <= 0)
71  pid = getpid();
72  G__temp_element(element);
73  do {
74  sprintf(name, "%d.%d", pid, uniq++);
75  G__file_name(path, element, name, G_mapset());
76  }
77  while (stat(path, &st) == 0);
78 
79  return G_store(path);
80 }
81 
82 
90 int G__temp_element(char *element)
91 {
92  const char *machine;
93 
94  strcpy(element, ".tmp");
95  machine = G__machine_name();
96  if (machine != NULL && *machine != 0) {
97  strcat(element, "/");
98  strcat(element, machine);
99  }
100  G__make_mapset_element(element);
101 
102  return 0;
103 }