Back to index

glibc  2.9
dl-environ.c
Go to the documentation of this file.
00001 /* Environment handling for dynamic loader.
00002    Copyright (C) 1995-1998, 2000, 2001, 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <string.h>
00021 #include <stdlib.h>
00022 #include <unistd.h>
00023 #include <ldsodefs.h>
00024 
00025 /* Walk through the environment of the process and return all entries
00026    starting with `LD_'.  */
00027 char *
00028 internal_function
00029 _dl_next_ld_env_entry (char ***position)
00030 {
00031   char **current = *position;
00032   char *result = NULL;
00033 
00034   while (*current != NULL)
00035     {
00036       if (__builtin_expect ((*current)[0] == 'L', 0)
00037          && (*current)[1] == 'D' && (*current)[2] == '_')
00038        {
00039          result = &(*current)[3];
00040 
00041          /* Save current position for next visit.  */
00042          *position = ++current;
00043 
00044          break;
00045        }
00046 
00047       ++current;
00048     }
00049 
00050   return result;
00051 }
00052 
00053 
00054 /* In ld.so __environ is not exported.  */
00055 extern char **__environ attribute_hidden;
00056 
00057 int
00058 unsetenv (const char *name)
00059 {
00060   char **ep;
00061 
00062   ep = __environ;
00063   while (*ep != NULL)
00064     {
00065       size_t cnt = 0;
00066 
00067       while ((*ep)[cnt] == name[cnt] && name[cnt] != '\0')
00068        ++cnt;
00069 
00070       if (name[cnt] == '\0' && (*ep)[cnt] == '=')
00071        {
00072          /* Found it.  Remove this pointer by moving later ones to
00073             the front.  */
00074          char **dp = ep;
00075 
00076          do
00077            dp[0] = dp[1];
00078          while (*dp++);
00079          /* Continue the loop in case NAME appears again.  */
00080        }
00081       else
00082        ++ep;
00083     }
00084 
00085   return 0;
00086 }