putenv.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <grass/config.h>
  5. #include <grass/gis.h>
  6. /*******************************************************************
  7. * G_putenv (name, value)
  8. * const char *name, *value
  9. *
  10. * this routine sets the UNIX environment variable name to value
  11. ******************************************************************/
  12. #if !defined(HAVE_PUTENV) && !defined(HAVE_SETENV)
  13. extern char **environ;
  14. #endif
  15. void G_putenv(const char *name, const char *value)
  16. {
  17. char buf[1024];
  18. #if defined(HAVE_PUTENV)
  19. sprintf(buf, "%s=%s", name, value);
  20. putenv(G_store(buf));
  21. #elif defined(HAVE_SETENV)
  22. setenv(name, value, 1);
  23. #else
  24. static int first = 1;
  25. int i;
  26. char **newenv;
  27. char *env;
  28. if (first) {
  29. for (i = 0; environ[i]; i++) ;
  30. newenv = (char **)G_malloc((i + 1) * sizeof(char *));
  31. for (i = 0; env = environ[i], env; i++)
  32. newenv[i] = G_store(env);
  33. newenv[i] = NULL;
  34. environ = newenv;
  35. first = 0;
  36. }
  37. for (i = 0; env = environ[i], env; i++) {
  38. char temp[4];
  39. if (sscanf(env, "%[^=]=%1s", buf, temp) < 1)
  40. continue;
  41. if (strcmp(buf, name) != 0)
  42. continue;
  43. G_free(env);
  44. sprintf(buf, "%s=%s", name, value);
  45. environ[i] = G_store(buf);
  46. return;
  47. }
  48. environ = (char **)G_realloc(environ, (i + 2) * sizeof(char *));
  49. sprintf(buf, "%s=%s", name, value);
  50. environ[i++] = G_store(buf);
  51. environ[i] = NULL;
  52. #endif
  53. }