gdb/environ.c - gdb

Functions defined

Macros defined

Source code

  1. /* environ.c -- library for manipulating environments for GNU.

  2.    Copyright (C) 1986-2015 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. #define min(a, b) ((a) < (b) ? (a) : (b))
  14. #define max(a, b) ((a) > (b) ? (a) : (b))

  15. #include "defs.h"
  16. #include "environ.h"


  17. /* Return a new environment object.  */

  18. struct gdb_environ *
  19. make_environ (void)
  20. {
  21.   struct gdb_environ *e;

  22.   e = (struct gdb_environ *) xmalloc (sizeof (struct gdb_environ));

  23.   e->allocated = 10;
  24.   e->vector = (char **) xmalloc ((e->allocated + 1) * sizeof (char *));
  25.   e->vector[0] = 0;
  26.   return e;
  27. }

  28. /* Free an environment and all the strings in it.  */

  29. void
  30. free_environ (struct gdb_environ *e)
  31. {
  32.   char **vector = e->vector;

  33.   while (*vector)
  34.     xfree (*vector++);

  35.   xfree (e->vector);
  36.   xfree (e);
  37. }

  38. /* Copy the environment given to this process into E.
  39.    Also copies all the strings in it, so we can be sure
  40.    that all strings in these environments are safe to free.  */

  41. void
  42. init_environ (struct gdb_environ *e)
  43. {
  44.   extern char **environ;
  45.   int i;

  46.   if (environ == NULL)
  47.     return;

  48.   for (i = 0; environ[i]; i++) /*EMPTY */ ;

  49.   if (e->allocated < i)
  50.     {
  51.       e->allocated = max (i, e->allocated + 10);
  52.       e->vector = (char **) xrealloc ((char *) e->vector,
  53.                                       (e->allocated + 1) * sizeof (char *));
  54.     }

  55.   memcpy (e->vector, environ, (i + 1) * sizeof (char *));

  56.   while (--i >= 0)
  57.     {
  58.       int len = strlen (e->vector[i]);
  59.       char *new = (char *) xmalloc (len + 1);

  60.       memcpy (new, e->vector[i], len + 1);
  61.       e->vector[i] = new;
  62.     }
  63. }

  64. /* Return the vector of environment E.
  65.    This is used to get something to pass to execve.  */

  66. char **
  67. environ_vector (struct gdb_environ *e)
  68. {
  69.   return e->vector;
  70. }

  71. /* Return the value in environment E of variable VAR.  */

  72. char *
  73. get_in_environ (const struct gdb_environ *e, const char *var)
  74. {
  75.   int len = strlen (var);
  76.   char **vector = e->vector;
  77.   char *s;

  78.   for (; (s = *vector) != NULL; vector++)
  79.     if (strncmp (s, var, len) == 0 && s[len] == '=')
  80.       return &s[len + 1];

  81.   return 0;
  82. }

  83. /* Store the value in E of VAR as VALUE.  */

  84. void
  85. set_in_environ (struct gdb_environ *e, const char *var, const char *value)
  86. {
  87.   int i;
  88.   int len = strlen (var);
  89.   char **vector = e->vector;
  90.   char *s;

  91.   for (i = 0; (s = vector[i]) != NULL; i++)
  92.     if (strncmp (s, var, len) == 0 && s[len] == '=')
  93.       break;

  94.   if (s == 0)
  95.     {
  96.       if (i == e->allocated)
  97.         {
  98.           e->allocated += 10;
  99.           vector = (char **) xrealloc ((char *) vector,
  100.                                        (e->allocated + 1) * sizeof (char *));
  101.           e->vector = vector;
  102.         }
  103.       vector[i + 1] = 0;
  104.     }
  105.   else
  106.     xfree (s);

  107.   s = (char *) xmalloc (len + strlen (value) + 2);
  108.   strcpy (s, var);
  109.   strcat (s, "=");
  110.   strcat (s, value);
  111.   vector[i] = s;

  112.   /* This used to handle setting the PATH and GNUTARGET variables
  113.      specially.  The latter has been replaced by "set gnutarget"
  114.      (which has worked since GDB 4.11).  The former affects searching
  115.      the PATH to find SHELL, and searching the PATH to find the
  116.      argument of "symbol-file" or "exec-file".  Maybe we should have
  117.      some kind of "set exec-path" for that.  But in any event, having
  118.      "set env" affect anything besides the inferior is a bad idea.
  119.      What if we want to change the environment we pass to the program
  120.      without afecting GDB's behavior?  */

  121.   return;
  122. }

  123. /* Remove the setting for variable VAR from environment E.  */

  124. void
  125. unset_in_environ (struct gdb_environ *e, const char *var)
  126. {
  127.   int len = strlen (var);
  128.   char **vector = e->vector;
  129.   char *s;

  130.   for (; (s = *vector) != NULL; vector++)
  131.     {
  132.       if (strncmp (s, var, len) == 0 && s[len] == '=')
  133.         {
  134.           xfree (s);
  135.           /* Walk through the vector, shuffling args down by one, including
  136.              the NULL terminator.  Can't use memcpy() here since the regions
  137.              overlap, and memmove() might not be available.  */
  138.           while ((vector[0] = vector[1]) != NULL)
  139.             {
  140.               vector++;
  141.             }
  142.           break;
  143.         }
  144.     }
  145. }