From: Bruno Haible Date: Fri, 14 Sep 2001 12:34:21 +0000 (+0000) Subject: Add the setenv() function to lib/. X-Git-Tag: v0.11~502 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2ecb11c53e32a57384bfe68e4e398c0936aad6d1;p=thirdparty%2Fgettext.git Add the setenv() function to lib/. --- diff --git a/ChangeLog b/ChangeLog index e8eff6f76..25c92803d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2001-09-06 Bruno Haible + + * configure.in: Call gt_FUNC_SETENV. + 2001-09-02 Bruno Haible * configure.in: Call gt_TMPDIR and gt_FUNC_MKDTEMP. diff --git a/configure.in b/configure.in index 56556bed2..fa50a4b14 100644 --- a/configure.in +++ b/configure.in @@ -61,6 +61,7 @@ AC_FUNC_VFORK gt_UNION_WAIT gt_TMPDIR gt_FUNC_MKDTEMP +gt_FUNC_SETENV AM_FUNC_ERROR_AT_LINE diff --git a/lib/ChangeLog b/lib/ChangeLog index c9b1f2f34..369c13771 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,10 @@ +2001-09-06 Bruno Haible + + * setenv.h: New file. + * setenv.c: New file, from glibc-2.2.4. + * Makefile.am (EXTRA_DIST): Add setenv.c. + (noinst_HEADERS): Add setenv.h. + 2001-09-02 Bruno Haible * mkdtemp.h: New file. diff --git a/lib/Makefile.am b/lib/Makefile.am index 98ebe19e6..4902fc9d4 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -22,8 +22,8 @@ AUTOMAKE_OPTIONS = 1.2 gnits noinst_LIBRARIES = libnlsut.a EXTRA_DIST = alloca.c config.charset error.c getline.c memset.c memmove.c \ -mkdtemp.c ref-add.sin ref-del.sin stpcpy.c stpncpy.c strcasecmp.c strcspn.c \ -strncasecmp.c strstr.c strtol.c strtoul.c vasprintf.c \ +mkdtemp.c ref-add.sin ref-del.sin setenv.c stpcpy.c stpncpy.c strcasecmp.c \ +strcspn.c strncasecmp.c strstr.c strtol.c strtoul.c vasprintf.c \ stdbool.h.in \ gen-lbrkprop.c 3level.h @@ -37,8 +37,8 @@ libnlsut_a_LIBADD = @ALLOCA@ @LIBOBJS@ noinst_HEADERS = c-ctype.h error.h execute.h findprog.h fstrcmp.h \ full-write.h gcd.h getline.h getopt.h hash.h lbrkprop.h linebreak.h mbswidth.h \ -mkdtemp.h obstack.h pathmax.h pipe.h progname.h safe-read.h system.h tmpdir.h \ -utf8-ucs4.h utf16-ucs4.h wait-process.h xerror.h +mkdtemp.h obstack.h pathmax.h pipe.h progname.h safe-read.h setenv.h system.h \ +tmpdir.h utf8-ucs4.h utf16-ucs4.h wait-process.h xerror.h DEFS = -DLIBDIR=\"$(libdir)\" @DEFS@ INCLUDES = -I. -I$(srcdir) -I.. -I../intl diff --git a/lib/setenv.c b/lib/setenv.c new file mode 100644 index 000000000..a7c1e167b --- /dev/null +++ b/lib/setenv.c @@ -0,0 +1,345 @@ +/* Copyright (C) 1992,95,96,97,98,99,2000,2001 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#if HAVE_CONFIG_H +# include +#endif + +#include +#if !_LIBC +# if !defined errno && !defined HAVE_ERRNO_DECL +extern int errno; +# endif +# define __set_errno(ev) ((errno) = (ev)) +#endif + +#if _LIBC || HAVE_STDLIB_H +# include +#endif +#if _LIBC || HAVE_STRING_H +# include +#endif +#if _LIBC || HAVE_UNISTD_H +# include +#endif + +#if !_LIBC +# define __environ environ +# ifndef HAVE_ENVIRON_DECL +extern char **environ; +# endif +#endif + +#if _LIBC +/* This lock protects against simultaneous modifications of `environ'. */ +# include +__libc_lock_define_initialized (static, envlock) +# define LOCK __libc_lock_lock (envlock) +# define UNLOCK __libc_lock_unlock (envlock) +#else +# define LOCK +# define UNLOCK +#endif + +/* In the GNU C library we must keep the namespace clean. */ +#ifdef _LIBC +# define setenv __setenv +# define unsetenv __unsetenv +# define clearenv __clearenv +# define tfind __tfind +# define tsearch __tsearch +#endif + +/* In the GNU C library implementation we try to be more clever and + allow arbitrarily many changes of the environment given that the used + values are from a small set. Outside glibc this will eat up all + memory after a while. */ +#if defined _LIBC || (defined HAVE_SEARCH_H && defined HAVE_TSEARCH \ + && defined __GNUC__) +# define USE_TSEARCH 1 +# include + +/* This is a pointer to the root of the search tree with the known + values. */ +static void *known_values; + +# define KNOWN_VALUE(Str) \ + ({ \ + void *value = tfind (Str, &known_values, (__compar_fn_t) strcmp); \ + value != NULL ? *(char **) value : NULL; \ + }) +# define STORE_VALUE(Str) \ + tsearch (Str, &known_values, (__compar_fn_t) strcmp) + +#else +# undef USE_TSEARCH + +# define KNOWN_VALUE(Str) NULL +# define STORE_VALUE(Str) do { } while (0) + +#endif + + +/* If this variable is not a null pointer we allocated the current + environment. */ +static char **last_environ; + + +/* This function is used by `setenv' and `putenv'. The difference between + the two functions is that for the former must create a new string which + is then placed in the environment, while the argument of `putenv' + must be used directly. This is all complicated by the fact that we try + to reuse values once generated for a `setenv' call since we can never + free the strings. */ +int +__add_to_environ (name, value, combined, replace) + const char *name; + const char *value; + const char *combined; + int replace; +{ + register char **ep; + register size_t size; + const size_t namelen = strlen (name); + const size_t vallen = value != NULL ? strlen (value) + 1 : 0; + + LOCK; + + /* We have to get the pointer now that we have the lock and not earlier + since another thread might have created a new environment. */ + ep = __environ; + + size = 0; + if (ep != NULL) + { + for (; *ep != NULL; ++ep) + if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=') + break; + else + ++size; + } + + if (ep == NULL || *ep == NULL) + { + char **new_environ; +#ifdef USE_TSEARCH + char *new_value; +#endif + + /* We allocated this space; we can extend it. */ + new_environ = (char **) realloc (last_environ, + (size + 2) * sizeof (char *)); + if (new_environ == NULL) + { + UNLOCK; + return -1; + } + + /* If the whole entry is given add it. */ + if (combined != NULL) + /* We must not add the string to the search tree since it belongs + to the user. */ + new_environ[size] = (char *) combined; + else + { + /* See whether the value is already known. */ +#ifdef USE_TSEARCH + new_value = (char *) alloca (namelen + 1 + vallen); +# ifdef _LIBC + __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1), + value, vallen); +# else + memcpy (new_value, name, namelen); + new_value[namelen] = '='; + memcpy (&new_value[namelen + 1], value, vallen); +# endif + + new_environ[size] = KNOWN_VALUE (new_value); + if (new_environ[size] == NULL) +#endif + { + new_environ[size] = (char *) malloc (namelen + 1 + vallen); + if (new_environ[size] == NULL) + { + __set_errno (ENOMEM); + UNLOCK; + return -1; + } + +#ifdef USE_TSEARCH + memcpy (new_environ[size], new_value, namelen + 1 + vallen); +#else + memcpy (new_environ[size], name, namelen); + new_environ[size][namelen] = '='; + memcpy (&new_environ[size][namelen + 1], value, vallen); +#endif + /* And save the value now. We cannot do this when we remove + the string since then we cannot decide whether it is a + user string or not. */ + STORE_VALUE (new_environ[size]); + } + } + + if (__environ != last_environ) + memcpy ((char *) new_environ, (char *) __environ, + size * sizeof (char *)); + + new_environ[size + 1] = NULL; + + last_environ = __environ = new_environ; + } + else if (replace) + { + char *np; + + /* Use the user string if given. */ + if (combined != NULL) + np = (char *) combined; + else + { +#ifdef USE_TSEARCH + char *new_value = alloca (namelen + 1 + vallen); +# ifdef _LIBC + __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1), + value, vallen); +# else + memcpy (new_value, name, namelen); + new_value[namelen] = '='; + memcpy (&new_value[namelen + 1], value, vallen); +# endif + + np = KNOWN_VALUE (new_value); + if (np == NULL) +#endif + { + np = malloc (namelen + 1 + vallen); + if (np == NULL) + { + UNLOCK; + return -1; + } + +#ifdef USE_TSEARCH + memcpy (np, new_value, namelen + 1 + vallen); +#else + memcpy (np, name, namelen); + np[namelen] = '='; + memcpy (&np[namelen + 1], value, vallen); +#endif + /* And remember the value. */ + STORE_VALUE (np); + } + } + + *ep = np; + } + + UNLOCK; + + return 0; +} + +int +setenv (name, value, replace) + const char *name; + const char *value; + int replace; +{ + return __add_to_environ (name, value, NULL, replace); +} + +int +unsetenv (name) + const char *name; +{ + size_t len; + char **ep; + + if (name == NULL || *name == '\0' || strchr (name, '=') != NULL) + { + __set_errno (EINVAL); + return -1; + } + + len = strlen (name); + + LOCK; + + ep = __environ; + while (*ep != NULL) + if (!strncmp (*ep, name, len) && (*ep)[len] == '=') + { + /* Found it. Remove this pointer by moving later ones back. */ + char **dp = ep; + + do + dp[0] = dp[1]; + while (*dp++); + /* Continue the loop in case NAME appears again. */ + } + else + ++ep; + + UNLOCK; + + return 0; +} + +/* The `clearenv' was planned to be added to POSIX.1 but probably + never made it. Nevertheless the POSIX.9 standard (POSIX bindings + for Fortran 77) requires this function. */ +int +clearenv () +{ + LOCK; + + if (__environ == last_environ && __environ != NULL) + { + /* We allocated this environment so we can free it. */ + free (__environ); + last_environ = NULL; + } + + /* Clear the environment pointer removes the whole environment. */ + __environ = NULL; + + UNLOCK; + + return 0; +} +#ifdef _LIBC +static void +free_mem (void) +{ + /* Remove all traces. */ + clearenv (); + + /* Now remove the search tree. */ + __tdestroy (known_values, free); + known_values = NULL; +} +text_set_element (__libc_subfreeres, free_mem); + + +# undef setenv +# undef unsetenv +# undef clearenv +weak_alias (__setenv, setenv) +weak_alias (__unsetenv, unsetenv) +weak_alias (__clearenv, clearenv) +#endif diff --git a/lib/setenv.h b/lib/setenv.h new file mode 100644 index 000000000..df8fa83fb --- /dev/null +++ b/lib/setenv.h @@ -0,0 +1,40 @@ +/* Setting environment variables. + Copyright (C) 2001 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#ifndef PARAMS +# if defined (__GNUC__) || __STDC__ +# define PARAMS(Args) Args +# else +# define PARAMS(Args) () +# endif +#endif + +#if HAVE_SETENV + +/* Get setenv(), unsetenv() declarations. */ +#include + +#else + +/* Set NAME to VALUE in the environment. + If REPLACE is nonzero, overwrite an existing value. */ +extern int setenv PARAMS ((const char *name, const char *value, int replace)); + +/* Remove the variable NAME from the environment. */ +extern int unsetenv PARAMS ((const char *name)); + +#endif diff --git a/m4/ChangeLog b/m4/ChangeLog index f89a47adf..22fa3aa19 100644 --- a/m4/ChangeLog +++ b/m4/ChangeLog @@ -1,3 +1,8 @@ +2001-09-06 Bruno Haible + + * setenv.m4: New file. + * Makefile.am (EXTRA_DIST): Add it. + 2001-09-02 Bruno Haible * mkdtemp.m4: New file. diff --git a/m4/Makefile.am b/m4/Makefile.am index ea1fdb312..ce5c3cc7d 100644 --- a/m4/Makefile.am +++ b/m4/Makefile.am @@ -9,5 +9,5 @@ aclocal_DATA = codeset.m4 gettext.m4 glibc21.m4 iconv.m4 isc-posix.m4 lcmessage. EXTRA_DIST = README \ c-bs-a.m4 codeset.m4 flex.m4 getline.m4 gettext.m4 glibc21.m4 iconv.m4 \ inttypes_h.m4 isc-posix.m4 lcmessage.m4 libtool.m4 mbrtowc.m4 mbstate_t.m4 \ -mbswidth.m4 mkdtemp.m4 progtest.m4 setlocale.m4 signed.m4 ssize_t.m4 \ -stdbool.m4 tmpdir.m4 uintmax_t.m4 ulonglong.m4 unionwait.m4 +mbswidth.m4 mkdtemp.m4 progtest.m4 setenv.m4 setlocale.m4 signed.m4 \ +ssize_t.m4 stdbool.m4 tmpdir.m4 uintmax_t.m4 ulonglong.m4 unionwait.m4 diff --git a/m4/setenv.m4 b/m4/setenv.m4 new file mode 100644 index 000000000..037d7bfaa --- /dev/null +++ b/m4/setenv.m4 @@ -0,0 +1,31 @@ +#serial 1 + +# Check if a variable is properly declared. +# gt_CHECK_VAR_DECL(includes,variable) +AC_DEFUN([gt_CHECK_VAR_DECL], +[ + define(gt_cv_var, [gt_cv_var_]$2[_declaration]) + AC_MSG_CHECKING([if $2 is properly declared]) + AC_CACHE_VAL(gt_cv_var, [ + AC_TRY_COMPILE([$1 + extern struct { int foo; } $2;], + [$2.foo = 1;], + gt_cv_var=no, + gt_cv_var=yes)]) + AC_MSG_RESULT($gt_cv_var) + if test $gt_cv_var = yes; then + AC_DEFINE([HAVE_]translit($2, [a-z], [A-Z])[_DECL], 1, + [Define if you have the declaration of $2.]) + fi +]) + +# Prerequisites of lib/setenv.c + +AC_DEFUN([gt_FUNC_SETENV], +[ + AC_REPLACE_FUNCS(setenv) + AC_CHECK_HEADERS(search.h stdlib.h string.h unistd.h) + AC_CHECK_FUNCS(tsearch) + gt_CHECK_VAR_DECL([#include ], errno) + gt_CHECK_VAR_DECL([#include ], environ) +])