envz.c 4.8 KB

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