]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdlib/putenv.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / stdlib / putenv.c
CommitLineData
04277e02 1/* Copyright (C) 1991-2019 Free Software Foundation, Inc.
f0e44959 2 This file is part of the GNU C Library.
28f540f4 3
f0e44959 4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
28f540f4 8
f0e44959
UD
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 12 Lesser General Public License for more details.
28f540f4 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
28f540f4 17
ff152e3f 18#if defined _AIX && !defined __GNUC__
196980f5
RM
19 #pragma alloca
20#endif
28f540f4 21
f0e44959
UD
22#if HAVE_CONFIG_H
23# include <config.h>
28f540f4
RM
24#endif
25
196980f5 26#if _LIBC || HAVE_STDLIB_H
f0e44959 27# include <stdlib.h>
28f540f4 28#endif
196980f5 29#if _LIBC || HAVE_STRING_H
f0e44959 30# include <string.h>
28f540f4 31#endif
28f540f4 32
196980f5 33#if !__GNU_LIBRARY__ && !HAVE_STRCHR
f0e44959 34# define strchr index
28f540f4 35#endif
28f540f4 36
196980f5 37#ifndef _LIBC
f0e44959
UD
38# ifdef HAVE_ALLOCA_H
39# include <alloca.h>
40# else
41# ifdef __GNUC__
42# define alloca __builtin_alloca
43# else
196980f5 44extern char *alloca ();
f0e44959
UD
45# endif /* __GNUC__ */
46# endif /* HAVE_ALLOCA_H */
196980f5 47#endif /* _LIBC */
28f540f4
RM
48
49
50/* Put STRING, which is of the form "NAME=VALUE", in the environment. */
51int
9d46370c 52putenv (char *string)
28f540f4
RM
53{
54 const char *const name_end = strchr (string, '=');
28f540f4 55
86187531 56 if (name_end != NULL)
28f540f4 57 {
ea389b12 58 char *name;
86187531 59#ifdef _LIBC
ea389b12
UD
60 int use_malloc = !__libc_use_alloca (name_end - string + 1);
61 if (__builtin_expect (use_malloc, 0))
62 {
ae65d4f3 63 name = __strndup (string, name_end - string);
ea389b12
UD
64 if (name == NULL)
65 return -1;
66 }
67 else
68 name = strndupa (string, name_end - string);
86187531 69#else
ea389b12
UD
70# define use_malloc 1
71 name = malloc (name_end - string + 1);
72 if (name == NULL)
73 return -1;
196980f5
RM
74 memcpy (name, string, name_end - string);
75 name[name_end - string] = '\0';
86187531 76#endif
ea389b12
UD
77 int result = __add_to_environ (name, NULL, string, 1);
78
a1ffb40e 79 if (__glibc_unlikely (use_malloc))
ea389b12
UD
80 free (name);
81
82 return result;
28f540f4 83 }
28f540f4 84
50304ef0 85 __unsetenv (string);
28f540f4
RM
86 return 0;
87}