GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
putenv.c
Go to the documentation of this file.
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <grass/config.h>
5 #include <grass/gis.h>
6 
7 /*******************************************************************
8  * G_putenv (name, value)
9  * const char *name, *value
10  *
11  * this routine sets the UNIX environment variable name to value
12  ******************************************************************/
13 
14 #if !defined(HAVE_PUTENV) && !defined(HAVE_SETENV)
15 extern char **environ;
16 #endif
17 
18 void G_putenv(const char *name, const char *value)
19 {
20  char buf[1024];
21 
22 #if defined(HAVE_PUTENV)
23  sprintf(buf, "%s=%s", name, value);
24  putenv(G_store(buf));
25 #elif defined(HAVE_SETENV)
26  setenv(name, value, 1);
27 #else
28  static int first = 1;
29  int i;
30  char **newenv;
31  char *env;
32 
33  if (first) {
34  for (i = 0; environ[i]; i++) ;
35  newenv = (char **)G_malloc((i + 1) * sizeof(char *));
36  for (i = 0; env = environ[i], env; i++)
37  newenv[i] = G_store(env);
38  newenv[i] = NULL;
39  environ = newenv;
40  first = 0;
41  }
42 
43  for (i = 0; env = environ[i], env; i++) {
44  char temp[4];
45 
46  if (sscanf(env, "%[^=]=%1s", buf, temp) < 1)
47  continue;
48 
49  if (strcmp(buf, name) != 0)
50  continue;
51 
52  G_free(env);
53  sprintf(buf, "%s=%s", name, value);
54  environ[i] = G_store(buf);
55 
56  return;
57  }
58  environ = (char **)G_realloc(environ, (i + 2) * sizeof(char *));
59  sprintf(buf, "%s=%s", name, value);
60  environ[i++] = G_store(buf);
61  environ[i] = NULL;
62 #endif
63 }
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
string name
Definition: render.py:1314
tuple env
void G_putenv(const char *name, const char *value)
Definition: putenv.c:18
char * value
Definition: env.c:30
int first
Definition: form/open.c:25
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
return NULL
Definition: dbfopen.c:1394
char ** environ