Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdlib / setenv_r.c
blob84d87a6ed2da72fe1a7ef1cf82a23518fb5f2220
1 /* This file may have been modified by DJ Delorie (Jan 1991). If so,
2 ** these modifications are Copyright (C) 1991 DJ Delorie.
3 */
5 /*
6 * Copyright (c) 1987 Regents of the University of California.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms are permitted
10 * provided that: (1) source distributions retain this entire copyright
11 * notice and comment, and (2) distributions including binaries display
12 * the following acknowledgement: ``This product includes software
13 * developed by the University of California, Berkeley and its contributors''
14 * in the documentation or other materials provided with the distribution.
15 * Neither the name of the University nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 #include <reent.h>
25 #include <stddef.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29 #include <errno.h>
30 #include "envlock.h"
32 extern char **environ;
34 /* Only deal with a pointer to environ, to work around subtle bugs with shared
35 libraries and/or small data systems where the user declares his own
36 'environ'. */
37 static char ***p_environ = &environ;
39 /* _findenv_r is defined in getenv_r.c. */
40 extern char *_findenv_r (struct _reent *, const char *, int *);
43 * _setenv_r --
44 * Set the value of the environmental variable "name" to be
45 * "value". If rewrite is set, replace any current value.
46 * If "name" contains equal sign, -1 is returned, and errno is
47 * set to EINVAL;
50 int
51 _setenv_r (struct _reent *reent_ptr,
52 const char *name,
53 const char *value,
54 int rewrite)
56 static int alloced; /* if allocated space before */
57 register char *C;
58 int l_value, offset;
60 if (strchr(name, '='))
62 errno = EINVAL;
63 return -1;
66 ENV_LOCK;
68 l_value = strlen (value);
69 if ((C = _findenv_r (reent_ptr, name, &offset)))
70 { /* find if already exists */
71 if (!rewrite)
73 ENV_UNLOCK;
74 return 0;
76 if (strlen (C) >= l_value)
77 { /* old larger; copy over */
78 while ((*C++ = *value++) != 0);
79 ENV_UNLOCK;
80 return 0;
83 else
84 { /* create new slot */
85 register int cnt;
86 register char **P;
88 for (P = *p_environ, cnt = 0; *P; ++P, ++cnt);
89 if (alloced)
90 { /* just increase size */
91 *p_environ = (char **) _realloc_r (reent_ptr, (char *) environ,
92 (size_t) (sizeof (char *) * (cnt + 2)));
93 if (!*p_environ)
95 ENV_UNLOCK;
96 return -1;
99 else
100 { /* get new space */
101 alloced = 1; /* copy old entries into it */
102 P = (char **) _malloc_r (reent_ptr, (size_t) (sizeof (char *) * (cnt + 2)));
103 if (!P)
105 ENV_UNLOCK;
106 return (-1);
108 memcpy((char *) P,(char *) *p_environ, cnt * sizeof (char *));
109 *p_environ = P;
111 (*p_environ)[cnt + 1] = NULL;
112 offset = cnt;
114 for (C = (char *) name; *C && *C != '='; ++C); /* no `=' in name */
115 if (!((*p_environ)[offset] = /* name + `=' + value */
116 _malloc_r (reent_ptr, (size_t) ((int) (C - name) + l_value + 2))))
118 ENV_UNLOCK;
119 return -1;
121 for (C = (*p_environ)[offset]; (*C = *name++) && *C != '='; ++C);
122 for (*C++ = '='; (*C++ = *value++) != 0;);
124 ENV_UNLOCK;
126 return 0;
130 * _unsetenv_r(name) --
131 * Delete environmental variable "name".
134 _unsetenv_r (struct _reent *reent_ptr,
135 const char *name)
137 register char **P;
138 int offset;
140 /* Name cannot be NULL, empty, or contain an equal sign. */
141 if (name == NULL || name[0] == '\0' || strchr(name, '='))
143 errno = EINVAL;
144 return -1;
147 ENV_LOCK;
149 while (_findenv_r (reent_ptr, name, &offset)) /* if set multiple times */
151 for (P = &(*p_environ)[offset];; ++P)
152 if (!(*P = *(P + 1)))
153 break;
156 ENV_UNLOCK;
157 return 0;