setenv.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2016,2017,2018,2019,2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. *
  5. * This file is part of GNU Mes.
  6. *
  7. * GNU Mes is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * GNU Mes is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <mes/lib.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #if defined(__M2__)
  24. #define M2_PTR_SIZE sizeof(char *)
  25. #else
  26. #define M2_PTR_SIZE 1
  27. #endif
  28. int
  29. setenv (char const *s, char const *v, int overwrite_p)
  30. {
  31. char **p = environ;
  32. int length = strlen (s);
  33. char* q;
  34. while (p[0] != 0)
  35. {
  36. if (strncmp (s, p[0], length) == 0)
  37. {
  38. q = p[0] + length;
  39. if (q[0] == '=')
  40. break;
  41. }
  42. p = p + M2_PTR_SIZE;
  43. }
  44. char *entry = malloc (length + strlen (v) + 2);
  45. int end_p = p[0] == 0;
  46. p[0] = entry;
  47. strcpy (entry, s);
  48. strcpy (entry + length, "=");
  49. strcpy (entry + length + 1, v);
  50. entry[length + strlen (v) + 2] = 0;
  51. if (end_p != 0)
  52. p[1] = 0;
  53. return 0;
  54. }