GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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  newenv = (char **)G_malloc((i + 1) * sizeof(char *));
49  for (i = 0; env = environ[i], env; i++)
50  newenv[i] = G_store(env);
51  newenv[i] = NULL;
52  environ = newenv;
53  first = 0;
54  }
55 
56  for (i = 0; env = environ[i], env; i++) {
57  char temp[4];
58 
59  if (sscanf(env, "%[^=]=%1s", buf, temp) < 1)
60  continue;
61 
62  if (strcmp(buf, name) != 0)
63  continue;
64 
65  G_free(env);
66  sprintf(buf, "%s=%s", name, value);
67  environ[i] = G_store(buf);
68 
69  return;
70  }
71  environ = (char **)G_realloc(environ, (i + 2) * sizeof(char *));
72  sprintf(buf, "%s=%s", name, value);
73  environ[i++] = G_store(buf);
74  environ[i] = NULL;
75 #endif
76 }
#define G_malloc(n)
Definition: defs/gis.h:112
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
#define NULL
Definition: ccmath.h:32
void G_putenv(const char *name, const char *value)
Sets the UNIX environment variable name to value.
Definition: putenv.c:31
#define G_realloc(p, n)
Definition: defs/gis.h:114
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
const char * name
Definition: named_colr.c:7