(getuidbyname): Declare parameter to be const.
[coreutils.git] / lib / putenv.c
blob44b89fdf32211057a40afef2ba61b8ad03548477
1 /* Copyright (C) 1991, 1994, 1997 Free Software Foundation, Inc.
3 NOTE: The canonical source of this file is maintained with the GNU C
4 Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #include <errno.h>
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
26 /* Disable the definition of putenv to rpl_putenv (from config.h) in this
27 file. Otherwise, we'd get conflicting prototypes for rpl_putenv on
28 systems like Irix 5.3. */
29 #undef putenv
31 #if defined (__GNU_LIBRARY__) || defined (HAVE_STDLIB_H)
32 # include <stdlib.h>
33 #endif
34 #if defined (__GNU_LIBRARY__) || defined (HAVE_STRING_H)
35 # include <string.h>
36 #endif
37 #if defined (__GNU_LIBRARY__) || defined (HAVE_UNISTD_H)
38 # include <unistd.h>
39 #endif
41 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_STRCHR)
42 # define strchr index
43 #endif
44 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_MEMCPY)
45 # define memcpy(d,s,n) bcopy ((s), (d), (n))
46 #endif
48 #if HAVE_GNU_LD
49 # define environ __environ
50 #else
51 extern char **environ;
52 #endif
55 /* Put STRING, which is of the form "NAME=VALUE", in the environment. */
56 int
57 rpl_putenv (string)
58 const char *string;
60 const char *const name_end = strchr (string, '=');
61 register size_t size;
62 register char **ep;
64 if (name_end == NULL)
66 /* Remove the variable from the environment. */
67 size = strlen (string);
68 for (ep = environ; *ep != NULL; ++ep)
69 if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
71 while (ep[1] != NULL)
73 ep[0] = ep[1];
74 ++ep;
76 *ep = NULL;
77 return 0;
81 size = 0;
82 for (ep = environ; *ep != NULL; ++ep)
83 if (!strncmp (*ep, string, name_end - string) &&
84 (*ep)[name_end - string] == '=')
85 break;
86 else
87 ++size;
89 if (*ep == NULL)
91 static char **last_environ = NULL;
92 char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
93 if (new_environ == NULL)
94 return -1;
95 (void) memcpy ((void *) new_environ, (void *) environ,
96 size * sizeof (char *));
97 new_environ[size] = (char *) string;
98 new_environ[size + 1] = NULL;
99 if (last_environ != NULL)
100 free ((void *) last_environ);
101 last_environ = new_environ;
102 environ = new_environ;
104 else
105 *ep = (char *) string;
107 return 0;