GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-b4e4cb0fe9
putenv.c
Go to the documentation of this file.
1 /*!
2  \file lib/gis/putenv.c
3 
4  \brief GIS library - environment routines
5 
6  (C) 2001-2009, 2011 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  \author Updated for GRASS7 by Glynn Clements
13  */
14 
15 #include <string.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <grass/config.h>
19 #include <grass/gis.h>
20 
21 #if !defined(HAVE_PUTENV) && !defined(HAVE_SETENV)
22 extern char **environ;
23 #endif
24 
25 /*!
26  \brief Sets the UNIX environment variable name to value
27 
28  \param name env name
29  \param value env value
30  */
31 void G_putenv(const char *name, const char *value)
32 {
33  char buf[1024];
34 
35 #if defined(HAVE_PUTENV)
36  sprintf(buf, "%s=%s", name, value);
37  putenv(G_store(buf));
38 #elif defined(HAVE_SETENV)
39  setenv(name, value, 1);
40 #else
41  static int first = 1;
42  int i;
43  char **newenv;
44  char *env;
45 
46  if (first) {
47  for (i = 0; environ[i]; i++)
48  ;
49  newenv = (char **)G_malloc((i + 1) * sizeof(char *));
50  for (i = 0; env = environ[i], env; i++)
51  newenv[i] = G_store(env);
52  newenv[i] = NULL;
53  environ = newenv;
54  first = 0;
55  }
56 
57  for (i = 0; env = environ[i], env; i++) {
58  char temp[4];
59 
60  if (sscanf(env, "%[^=]=%1s", buf, temp) < 1)
61  continue;
62 
63  if (strcmp(buf, name) != 0)
64  continue;
65 
66  G_free(env);
67  sprintf(buf, "%s=%s", name, value);
68  environ[i] = G_store(buf);
69 
70  return;
71  }
72  environ = (char **)G_realloc(environ, (i + 2) * sizeof(char *));
73  sprintf(buf, "%s=%s", name, value);
74  environ[i++] = G_store(buf);
75  environ[i] = NULL;
76 #endif
77 }
#define NULL
Definition: ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
#define G_realloc(p, n)
Definition: defs/gis.h:96
#define G_malloc(n)
Definition: defs/gis.h:94
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
const char * name
Definition: named_colr.c:6
void G_putenv(const char *name, const char *value)
Sets the UNIX environment variable name to value.
Definition: putenv.c:31