environ.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* environ.c -- library for manipulating environments for GNU.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include "common-defs.h"
  14. #include "environ.h"
  15. #include <algorithm>
  16. #include <utility>
  17. /* See gdbsupport/environ.h. */
  18. gdb_environ &
  19. gdb_environ::operator= (gdb_environ &&e)
  20. {
  21. /* Are we self-moving? */
  22. if (&e == this)
  23. return *this;
  24. m_environ_vector = std::move (e.m_environ_vector);
  25. m_user_set_env = std::move (e.m_user_set_env);
  26. m_user_unset_env = std::move (e.m_user_unset_env);
  27. e.m_environ_vector.clear ();
  28. e.m_environ_vector.push_back (NULL);
  29. e.m_user_set_env.clear ();
  30. e.m_user_unset_env.clear ();
  31. return *this;
  32. }
  33. /* See gdbsupport/environ.h. */
  34. gdb_environ gdb_environ::from_host_environ ()
  35. {
  36. extern char **environ;
  37. gdb_environ e;
  38. if (environ == NULL)
  39. return e;
  40. for (int i = 0; environ[i] != NULL; ++i)
  41. {
  42. /* Make sure we add the element before the last (NULL). */
  43. e.m_environ_vector.insert (e.m_environ_vector.end () - 1,
  44. xstrdup (environ[i]));
  45. }
  46. return e;
  47. }
  48. /* See gdbsupport/environ.h. */
  49. void
  50. gdb_environ::clear ()
  51. {
  52. for (char *v : m_environ_vector)
  53. xfree (v);
  54. m_environ_vector.clear ();
  55. /* Always add the NULL element. */
  56. m_environ_vector.push_back (NULL);
  57. m_user_set_env.clear ();
  58. m_user_unset_env.clear ();
  59. }
  60. /* Helper function to check if STRING contains an environment variable
  61. assignment of VAR, i.e., if STRING starts with 'VAR='. Return true
  62. if it contains, false otherwise. */
  63. static bool
  64. match_var_in_string (const char *string, const char *var, size_t var_len)
  65. {
  66. if (strncmp (string, var, var_len) == 0 && string[var_len] == '=')
  67. return true;
  68. return false;
  69. }
  70. /* See gdbsupport/environ.h. */
  71. const char *
  72. gdb_environ::get (const char *var) const
  73. {
  74. size_t len = strlen (var);
  75. for (char *el : m_environ_vector)
  76. if (el != NULL && match_var_in_string (el, var, len))
  77. return &el[len + 1];
  78. return NULL;
  79. }
  80. /* See gdbsupport/environ.h. */
  81. void
  82. gdb_environ::set (const char *var, const char *value)
  83. {
  84. char *fullvar = concat (var, "=", value, (char *) NULL);
  85. /* We have to unset the variable in the vector if it exists. */
  86. unset (var, false);
  87. /* Insert the element before the last one, which is always NULL. */
  88. m_environ_vector.insert (m_environ_vector.end () - 1, fullvar);
  89. /* Mark this environment variable as having been set by the user.
  90. This will be useful when we deal with setting environment
  91. variables on the remote target. */
  92. m_user_set_env.insert (std::string (fullvar));
  93. /* If this environment variable is marked as unset by the user, then
  94. remove it from the list, because now the user wants to set
  95. it. */
  96. m_user_unset_env.erase (std::string (var));
  97. }
  98. /* See gdbsupport/environ.h. */
  99. void
  100. gdb_environ::unset (const char *var, bool update_unset_list)
  101. {
  102. size_t len = strlen (var);
  103. std::vector<char *>::iterator it_env;
  104. /* We iterate until '.end () - 1' because the last element is
  105. always NULL. */
  106. for (it_env = m_environ_vector.begin ();
  107. it_env != m_environ_vector.end () - 1;
  108. ++it_env)
  109. if (match_var_in_string (*it_env, var, len))
  110. break;
  111. if (it_env != m_environ_vector.end () - 1)
  112. {
  113. m_user_set_env.erase (std::string (*it_env));
  114. xfree (*it_env);
  115. m_environ_vector.erase (it_env);
  116. }
  117. if (update_unset_list)
  118. m_user_unset_env.insert (std::string (var));
  119. }
  120. /* See gdbsupport/environ.h. */
  121. void
  122. gdb_environ::unset (const char *var)
  123. {
  124. unset (var, true);
  125. }
  126. /* See gdbsupport/environ.h. */
  127. char **
  128. gdb_environ::envp () const
  129. {
  130. return const_cast<char **> (&m_environ_vector[0]);
  131. }
  132. /* See gdbsupport/environ.h. */
  133. const std::set<std::string> &
  134. gdb_environ::user_set_env () const
  135. {
  136. return m_user_set_env;
  137. }
  138. const std::set<std::string> &
  139. gdb_environ::user_unset_env () const
  140. {
  141. return m_user_unset_env;
  142. }