GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
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 SPDX-FileCopyrightText: 2001-2009, 2011 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Original author CERL
10 \author Updated for GRASS7 by Glynn Clements
11 */
12
13#include <string.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <grass/config.h>
17#include <grass/gis.h>
18
19#if !defined(HAVE_PUTENV) && !defined(HAVE_SETENV)
20extern char **environ;
21#endif
22
23/*!
24 \brief Sets the UNIX environment variable name to value
25
26 \param name env name
27 \param value env value
28 */
29void G_putenv(const char *name, const char *value)
30{
31 char buf[1024];
32
33#if defined(HAVE_PUTENV)
34 snprintf(buf, sizeof(buf), "%s=%s", name, value);
35 putenv(G_store(buf));
36#elif defined(HAVE_SETENV)
37 setenv(name, value, 1);
38#else
39 static int first = 1;
40 int i;
41 char **newenv;
42 char *env;
43
44 if (first) {
45 for (i = 0; environ[i]; i++)
46 ;
47 newenv = (char **)G_malloc((i + 1) * sizeof(char *));
48 for (i = 0; env = environ[i], env; i++)
49 newenv[i] = G_store(env);
50 newenv[i] = NULL;
52 first = 0;
53 }
54
55 for (i = 0; env = environ[i], env; i++) {
56 char temp[4];
57
58 if (sscanf(env, "%[^=]=%1s", buf, temp) < 1)
59 continue;
60
61 if (strcmp(buf, name) != 0)
62 continue;
63
64 G_free(env);
65 snprintf(buf, sizeof(buf), "%s=%s", name, value);
66 environ[i] = G_store(buf);
67
68 return;
69 }
70 environ = (char **)G_realloc(environ, (i + 2) * sizeof(char *));
71 snprintf(buf, sizeof(buf), "%s=%s", name, value);
72 environ[i++] = G_store(buf);
73 environ[i] = NULL;
74#endif
75}
#define NULL
Definition ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
#define G_realloc(p, n)
Definition defs/gis.h:138
#define G_malloc(n)
Definition defs/gis.h:136
char * G_store(const char *)
Copy string to allocated memory.
Definition strings.c:85
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:29