Initialize the TYPE_FLOATFORMAT field of the h/w floating-point
[deliverable/binutils-gdb.git] / libiberty / setenv.c
CommitLineData
e2eaf477
ILT
1/* Copyright (C) 1992, 1995, 1996, 1997 Free Software Foundation, Inc.
2 This file based on setenv.c in the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
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
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
18
39423523
DD
19
20/*
21
22@deftypefn Supplemental int setenv (const char *@var{name}, const char *@var{value}, int @var{overwrite})
23@deftypefnx Supplemental void unsetenv (const char *@var{name})
24
25@code{setenv} adds @var{name} to the environment with value
26@var{value}. If the name was already present in the environment,
56056af5 27the new value will be stored only if @var{overwrite} is nonzero.
39423523
DD
28The companion @code{unsetenv} function removes @var{name} from the
29environment. This implementation is not safe for multithreaded code.
30
31@end deftypefn
32
33*/
34
e2eaf477
ILT
35#if HAVE_CONFIG_H
36# include <config.h>
37#endif
38
39#include "ansidecl.h"
40#include <sys/types.h> /* For `size_t' */
41#include <stdio.h> /* For `NULL' */
42
43#include <errno.h>
44#if !defined(errno) && !defined(HAVE_ERRNO_DECL)
45extern int errno;
46#endif
47#define __set_errno(ev) ((errno) = (ev))
48
49#if HAVE_STDLIB_H
50# include <stdlib.h>
51#endif
52#if HAVE_STRING_H
53# include <string.h>
54#endif
55#if HAVE_UNISTD_H
56# include <unistd.h>
57#endif
58
59#define __environ environ
60#ifndef HAVE_ENVIRON_DECL
61extern char **environ;
62#endif
63
64/* LOCK and UNLOCK are defined as no-ops. This makes the libiberty
65 * implementation MT-Unsafe. */
66#define LOCK
67#define UNLOCK
68
69/* Below this point, it's verbatim code from the glibc-2.0 implementation */
70
71/* If this variable is not a null pointer we allocated the current
72 environment. */
73static char **last_environ;
74
75
76int
77setenv (name, value, replace)
78 const char *name;
79 const char *value;
80 int replace;
81{
74bcd529 82 register char **ep = 0;
e2eaf477
ILT
83 register size_t size;
84 const size_t namelen = strlen (name);
85 const size_t vallen = strlen (value) + 1;
86
87 LOCK;
88
89 size = 0;
90 if (__environ != NULL)
eb383413
L
91 {
92 for (ep = __environ; *ep != NULL; ++ep)
93 if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
94 break;
95 else
96 ++size;
97 }
e2eaf477
ILT
98
99 if (__environ == NULL || *ep == NULL)
100 {
101 char **new_environ;
102 if (__environ == last_environ && __environ != NULL)
103 /* We allocated this space; we can extend it. */
104 new_environ = (char **) realloc (last_environ,
105 (size + 2) * sizeof (char *));
106 else
107 new_environ = (char **) malloc ((size + 2) * sizeof (char *));
108
109 if (new_environ == NULL)
110 {
111 UNLOCK;
112 return -1;
113 }
114
115 new_environ[size] = malloc (namelen + 1 + vallen);
116 if (new_environ[size] == NULL)
117 {
118 free ((char *) new_environ);
119 __set_errno (ENOMEM);
120 UNLOCK;
121 return -1;
122 }
123
124 if (__environ != last_environ)
125 memcpy ((char *) new_environ, (char *) __environ,
126 size * sizeof (char *));
127
128 memcpy (new_environ[size], name, namelen);
129 new_environ[size][namelen] = '=';
130 memcpy (&new_environ[size][namelen + 1], value, vallen);
131
132 new_environ[size + 1] = NULL;
133
134 last_environ = __environ = new_environ;
135 }
136 else if (replace)
137 {
138 size_t len = strlen (*ep);
139 if (len + 1 < namelen + 1 + vallen)
140 {
141 /* The existing string is too short; malloc a new one. */
142 char *new = malloc (namelen + 1 + vallen);
143 if (new == NULL)
144 {
145 UNLOCK;
146 return -1;
147 }
148 *ep = new;
149 }
150 memcpy (*ep, name, namelen);
151 (*ep)[namelen] = '=';
152 memcpy (&(*ep)[namelen + 1], value, vallen);
153 }
154
155 UNLOCK;
156
157 return 0;
158}
159
160void
161unsetenv (name)
162 const char *name;
163{
164 const size_t len = strlen (name);
165 char **ep;
166
167 LOCK;
168
169 for (ep = __environ; *ep; ++ep)
170 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
171 {
172 /* Found it. Remove this pointer by moving later ones back. */
173 char **dp = ep;
174 do
175 dp[0] = dp[1];
176 while (*dp++);
177 /* Continue the loop in case NAME appears again. */
178 }
179
180 UNLOCK;
181}
This page took 0.091772 seconds and 4 git commands to generate.