putenv.c

Go to the documentation of this file.
00001 #include <string.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include "config.h"
00005 #include "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 extern char **environ;
00015 
00016 void G_putenv(const char *name, const char *value)
00017 {
00018     char buf[1024];
00019 #if defined(HAVE_PUTENV)
00020     sprintf(buf, "%s=%s", name, value);
00021     putenv(G_store(buf));
00022 #elif defined(HAVE_SETENV)
00023     setenv(name, value, 1);
00024 #else
00025     static int first = 1;
00026     int i;
00027     char **newenv;
00028     char *env;
00029 
00030     if (first)
00031     {
00032         for (i = 0; environ[i]; i++)
00033                 ;
00034         newenv = (char **) G_malloc ((i + 1) * sizeof (char *));
00035         for (i = 0; env = environ[i], env; i++)
00036             newenv[i] = G_store(env);
00037         newenv[i] = NULL;
00038         environ = newenv;
00039         first = 0;
00040     }
00041 
00042     for (i = 0; env = environ[i], env; i++)
00043     {
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 }
00064 

Generated on Mon Jan 1 19:49:25 2007 for GRASS by  doxygen 1.5.1