GRASS Programmer's Manual  6.4.2(2012)
putenv.c
Go to the documentation of this file.
00001 #include <string.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <grass/config.h>
00005 #include <grass/gis.h>
00006 
00007 /*******************************************************************
00008  * G_putenv (name, value)
00009  *   const char *name, *value
00010  *
00011  * this routine sets the UNIX environment variable name to value
00012  ******************************************************************/
00013 
00014 #if !defined(HAVE_PUTENV) && !defined(HAVE_SETENV)
00015 extern char **environ;
00016 #endif
00017 
00018 void G_putenv(const char *name, const char *value)
00019 {
00020     char buf[1024];
00021 
00022 #if defined(HAVE_PUTENV)
00023     sprintf(buf, "%s=%s", name, value);
00024     putenv(G_store(buf));
00025 #elif defined(HAVE_SETENV)
00026     setenv(name, value, 1);
00027 #else
00028     static int first = 1;
00029     int i;
00030     char **newenv;
00031     char *env;
00032 
00033     if (first) {
00034         for (i = 0; environ[i]; i++) ;
00035         newenv = (char **)G_malloc((i + 1) * sizeof(char *));
00036         for (i = 0; env = environ[i], env; i++)
00037             newenv[i] = G_store(env);
00038         newenv[i] = NULL;
00039         environ = newenv;
00040         first = 0;
00041     }
00042 
00043     for (i = 0; env = environ[i], env; i++) {
00044         char temp[4];
00045 
00046         if (sscanf(env, "%[^=]=%1s", buf, temp) < 1)
00047             continue;
00048 
00049         if (strcmp(buf, name) != 0)
00050             continue;
00051 
00052         G_free(env);
00053         sprintf(buf, "%s=%s", name, value);
00054         environ[i] = G_store(buf);
00055 
00056         return;
00057     }
00058     environ = (char **)G_realloc(environ, (i + 2) * sizeof(char *));
00059     sprintf(buf, "%s=%s", name, value);
00060     environ[i++] = G_store(buf);
00061     environ[i] = NULL;
00062 #endif
00063 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines