putenv.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
  2. * This file is part of the Linux-8086 C library and is distributed
  3. * under the GNU Library General Public License.
  4. */
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <malloc.h>
  9. #define ADD_NUM 4
  10. int putenv (char *var)
  11. {
  12. static char **mall_env = 0;
  13. static int extras = 0;
  14. char **p, **d;
  15. char *r;
  16. int len;
  17. r = strchr(var, '=');
  18. if (r == 0)
  19. len = strlen(var);
  20. else
  21. len = r - var;
  22. if (!__environ) {
  23. __environ = (char **) malloc(ADD_NUM * sizeof(char *));
  24. memset(__environ, 0, sizeof(char *) * ADD_NUM);
  25. extras = ADD_NUM;
  26. }
  27. for (p = __environ; *p; p++) {
  28. if (memcmp(var, *p, len) == 0 && (*p)[len] == '=') {
  29. while ((p[0] = p[1]))
  30. p++;
  31. extras++;
  32. break;
  33. }
  34. }
  35. if (r == 0)
  36. return 0;
  37. if (extras <= 0) { /* Need more space */
  38. d = malloc((p - __environ + 1 + ADD_NUM) * sizeof(char *));
  39. if (d == 0)
  40. return -1;
  41. memcpy((void *) d, (void *) __environ,
  42. (p - __environ + 1) * sizeof(char *));
  43. p = d + (p - __environ);
  44. extras = ADD_NUM;
  45. if (mall_env)
  46. free(mall_env);
  47. __environ = d;
  48. mall_env = d;
  49. }
  50. *p++ = strdup((char *) var);
  51. *p = '\0';
  52. extras--;
  53. return 0;
  54. }