Files
glibc/stdlib/putenv.c
T

88 lines
2.1 KiB
C
Raw Normal View History

/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
1996-11-02 01:28:01 +00:00
This file is part of the GNU C Library.
1995-02-18 01:27:10 +00:00
1996-11-02 01:28:01 +00:00
The GNU C Library is free software; you can redistribute it and/or
2001-07-06 04:58:11 +00:00
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
1995-02-18 01:27:10 +00:00
1996-11-02 01:28:01 +00:00
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
2001-07-06 04:58:11 +00:00
Lesser General Public License for more details.
1995-02-18 01:27:10 +00:00
2001-07-06 04:58:11 +00:00
You should have received a copy of the GNU Lesser General Public
2012-02-09 23:18:22 +00:00
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
1995-02-18 01:27:10 +00:00
1998-05-14 19:14:48 +00:00
#if defined _AIX && !defined __GNUC__
#pragma alloca
#endif
1995-02-18 01:27:10 +00:00
1996-11-02 01:28:01 +00:00
#if HAVE_CONFIG_H
# include <config.h>
1995-02-18 01:27:10 +00:00
#endif
#if _LIBC || HAVE_STDLIB_H
1996-11-02 01:28:01 +00:00
# include <stdlib.h>
1995-02-18 01:27:10 +00:00
#endif
#if _LIBC || HAVE_STRING_H
1996-11-02 01:28:01 +00:00
# include <string.h>
1995-02-18 01:27:10 +00:00
#endif
#if !__GNU_LIBRARY__ && !HAVE_STRCHR
1996-11-02 01:28:01 +00:00
# define strchr index
1995-02-18 01:27:10 +00:00
#endif
#ifndef _LIBC
1996-11-02 01:28:01 +00:00
# ifdef HAVE_ALLOCA_H
# include <alloca.h>
# else
# ifdef __GNUC__
# define alloca __builtin_alloca
# else
extern char *alloca ();
1996-11-02 01:28:01 +00:00
# endif /* __GNUC__ */
# endif /* HAVE_ALLOCA_H */
#endif /* _LIBC */
1995-02-18 01:27:10 +00:00
/* Put STRING, which is of the form "NAME=VALUE", in the environment. */
int
putenv (char *string)
1995-02-18 01:27:10 +00:00
{
const char *const name_end = strchr (string, '=');
1997-11-06 00:02:46 +00:00
if (name_end != NULL)
1995-02-18 01:27:10 +00:00
{
2011-05-16 10:13:54 -04:00
char *name;
1997-11-06 00:02:46 +00:00
#ifdef _LIBC
2011-05-16 10:13:54 -04:00
int use_malloc = !__libc_use_alloca (name_end - string + 1);
if (__builtin_expect (use_malloc, 0))
{
name = __strndup (string, name_end - string);
2011-05-16 10:13:54 -04:00
if (name == NULL)
return -1;
}
else
name = strndupa (string, name_end - string);
1997-11-06 00:02:46 +00:00
#else
2011-05-16 10:13:54 -04:00
# define use_malloc 1
name = malloc (name_end - string + 1);
if (name == NULL)
return -1;
memcpy (name, string, name_end - string);
name[name_end - string] = '\0';
1997-11-06 00:02:46 +00:00
#endif
2011-05-16 10:13:54 -04:00
int result = __add_to_environ (name, NULL, string, 1);
2014-02-10 14:45:42 +01:00
if (__glibc_unlikely (use_malloc))
2011-05-16 10:13:54 -04:00
free (name);
return result;
1995-02-18 01:27:10 +00:00
}
1998-07-16 11:44:36 +00:00
__unsetenv (string);
1995-02-18 01:27:10 +00:00
return 0;
}