envz.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* Routines for dealing with '\0' separated environment vectors
  2. Copyright (C) 1995-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <malloc.h>
  16. #include <string.h>
  17. #include <envz.h>
  18. /* The character separating names from values in an envz. */
  19. #define SEP '='
  20. /* Returns a pointer to the entry in ENVZ for NAME, or 0 if there is none.
  21. If NAME contains the separator character, only the portion before it is
  22. used in the comparison. */
  23. char *
  24. envz_entry (const char *envz, size_t envz_len, const char *name)
  25. {
  26. while (envz_len)
  27. {
  28. const char *p = name;
  29. const char *entry = envz; /* Start of this entry. */
  30. /* See how far NAME and ENTRY match. */
  31. while (envz_len && *p == *envz && *p && *p != SEP)
  32. p++, envz++, envz_len--;
  33. if ((*envz == '\0' || *envz == SEP) && (*p == '\0' || *p == SEP))
  34. /* Bingo! */
  35. return (char *) entry;
  36. /* No match, skip to the next entry. */
  37. while (envz_len && *envz)
  38. envz++, envz_len--;
  39. if (envz_len)
  40. envz++, envz_len--; /* skip '\0' */
  41. }
  42. return NULL;
  43. }
  44. libc_hidden_def (envz_entry)
  45. /* Returns a pointer to the value portion of the entry in ENVZ for NAME, or 0
  46. if there is none. */
  47. char *
  48. envz_get (const char *envz, size_t envz_len, const char *name)
  49. {
  50. char *entry = envz_entry (envz, envz_len, name);
  51. if (entry)
  52. {
  53. while (*entry && *entry != SEP)
  54. entry++;
  55. if (*entry)
  56. entry++;
  57. else
  58. entry = NULL; /* A null entry. */
  59. }
  60. return entry;
  61. }
  62. /* Remove the entry for NAME from ENVZ & ENVZ_LEN, if any. */
  63. void
  64. envz_remove (char **envz, size_t *envz_len, const char *name)
  65. {
  66. char *entry = envz_entry (*envz, *envz_len, name);
  67. if (entry)
  68. argz_delete (envz, envz_len, entry);
  69. }
  70. libc_hidden_def (envz_remove)
  71. /* Adds an entry for NAME with value VALUE to ENVZ & ENVZ_LEN. If an entry
  72. with the same name already exists in ENVZ, it is removed. If VALUE is
  73. NULL, then the new entry will a special null one, for which envz_get will
  74. return NULL, although envz_entry will still return an entry; this is handy
  75. because when merging with another envz, the null entry can override an
  76. entry in the other one. Null entries can be removed with envz_strip (). */
  77. error_t
  78. envz_add (char **envz, size_t *envz_len, const char *name, const char *value)
  79. {
  80. envz_remove (envz, envz_len, name);
  81. if (value)
  82. /* Add the new value, if there is one. */
  83. {
  84. size_t name_len = strlen (name);
  85. size_t value_len = strlen (value);
  86. size_t old_envz_len = *envz_len;
  87. size_t new_envz_len = old_envz_len + name_len + 1 + value_len + 1;
  88. char *new_envz = realloc (*envz, new_envz_len);
  89. if (new_envz)
  90. {
  91. memcpy (new_envz + old_envz_len, name, name_len);
  92. new_envz[old_envz_len + name_len] = SEP;
  93. memcpy (new_envz + old_envz_len + name_len + 1, value, value_len);
  94. new_envz[new_envz_len - 1] = 0;
  95. *envz = new_envz;
  96. *envz_len = new_envz_len;
  97. return 0;
  98. }
  99. else
  100. return ENOMEM;
  101. }
  102. else
  103. /* Add a null entry. */
  104. return __argz_add (envz, envz_len, name);
  105. }
  106. /* Adds each entry in ENVZ2 to ENVZ & ENVZ_LEN, as if with envz_add(). If
  107. OVERRIDE is true, then values in ENVZ2 will supersede those with the same
  108. name in ENV, otherwise not. */
  109. error_t
  110. envz_merge (char **envz, size_t *envz_len, const char *envz2,
  111. size_t envz2_len, int override)
  112. {
  113. error_t err = 0;
  114. while (envz2_len && ! err)
  115. {
  116. char *old = envz_entry (*envz, *envz_len, envz2);
  117. size_t new_len = strlen (envz2) + 1;
  118. if (! old)
  119. err = __argz_append (envz, envz_len, envz2, new_len);
  120. else if (override)
  121. {
  122. argz_delete (envz, envz_len, old);
  123. err = __argz_append (envz, envz_len, envz2, new_len);
  124. }
  125. envz2 += new_len;
  126. envz2_len -= new_len;
  127. }
  128. return err;
  129. }
  130. /* Remove null entries. */
  131. void
  132. envz_strip (char **envz, size_t *envz_len)
  133. {
  134. char *entry = *envz;
  135. size_t left = *envz_len;
  136. while (left)
  137. {
  138. size_t entry_len = strlen (entry) + 1;
  139. left -= entry_len;
  140. if (! strchr (entry, SEP))
  141. /* Null entry. */
  142. memmove (entry, entry + entry_len, left);
  143. else
  144. entry += entry_len;
  145. }
  146. *envz_len = entry - *envz;
  147. }