File: | src/lib/libc/stdlib/setenv.c |
Warning: | line 110, column 7 Although the value stored to 'C' is used in the enclosing expression, the value is never actually read from 'C' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* $OpenBSD: setenv.c,v 1.20 2022/08/08 22:40:03 millert Exp $ */ |
2 | /* |
3 | * Copyright (c) 1987 Regents of the University of California. |
4 | * All rights reserved. |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
9 | * 1. Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * 2. Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
14 | * 3. Neither the name of the University nor the names of its contributors |
15 | * may be used to endorse or promote products derived from this software |
16 | * without specific prior written permission. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
28 | * SUCH DAMAGE. |
29 | */ |
30 | |
31 | #include <errno(*__errno()).h> |
32 | #include <stdlib.h> |
33 | #include <string.h> |
34 | |
35 | static char **lastenv; /* last value of environ */ |
36 | |
37 | /* |
38 | * putenv -- |
39 | * Add a name=value string directly to the environmental, replacing |
40 | * any current value. |
41 | */ |
42 | int |
43 | putenv(char *str) |
44 | { |
45 | char **P, *cp; |
46 | size_t cnt = 0; |
47 | int offset = 0; |
48 | |
49 | for (cp = str; *cp && *cp != '='; ++cp) |
50 | ; |
51 | if (cp == str || *cp != '=') { |
52 | /* '=' is the first character of string or is missing. */ |
53 | errno(*__errno()) = EINVAL22; |
54 | return (-1); |
55 | } |
56 | |
57 | if (__findenv(str, (int)(cp - str), &offset) != NULL((void *)0)) { |
58 | environ[offset++] = str; |
59 | /* could be set multiple times */ |
60 | while (__findenv(str, (int)(cp - str), &offset)) { |
61 | for (P = &environ[offset];; ++P) |
62 | if (!(*P = *(P + 1))) |
63 | break; |
64 | } |
65 | return (0); |
66 | } |
67 | |
68 | /* create new slot for string */ |
69 | if (environ != NULL((void *)0)) { |
70 | for (P = environ; *P != NULL((void *)0); P++) |
71 | ; |
72 | cnt = P - environ; |
73 | } |
74 | P = reallocarray(lastenv, cnt + 2, sizeof(char *)); |
75 | if (!P) |
76 | return (-1); |
77 | if (lastenv != environ && environ != NULL((void *)0)) |
78 | memcpy(P, environ, cnt * sizeof(char *)); |
79 | lastenv = environ = P; |
80 | environ[cnt] = str; |
81 | environ[cnt + 1] = NULL((void *)0); |
82 | return (0); |
83 | } |
84 | DEF_WEAK(putenv)__asm__(".weak " "putenv" " ; " "putenv" " = " "_libc_putenv" ); |
85 | |
86 | /* |
87 | * setenv -- |
88 | * Set the value of the environmental variable "name" to be |
89 | * "value". If rewrite is set, replace any current value. |
90 | */ |
91 | int |
92 | setenv(const char *name, const char *value, int rewrite) |
93 | { |
94 | char *C, **P; |
95 | const char *np; |
96 | int l_value, offset = 0; |
97 | |
98 | if (!name || !*name) { |
99 | errno(*__errno()) = EINVAL22; |
100 | return (-1); |
101 | } |
102 | for (np = name; *np && *np != '='; ++np) |
103 | ; |
104 | if (*np) { |
105 | errno(*__errno()) = EINVAL22; |
106 | return (-1); /* has `=' in name */ |
107 | } |
108 | |
109 | l_value = strlen(value); |
110 | if ((C = __findenv(name, (int)(np - name), &offset)) != NULL((void *)0)) { |
Although the value stored to 'C' is used in the enclosing expression, the value is never actually read from 'C' | |
111 | int tmpoff = offset + 1; |
112 | if (!rewrite) |
113 | return (0); |
114 | #if 0 /* XXX - existing entry may not be writable */ |
115 | if (strlen(C) >= l_value) { /* old larger; copy over */ |
116 | while ((*C++ = *value++)) |
117 | ; |
118 | return (0); |
119 | } |
120 | #endif |
121 | /* could be set multiple times */ |
122 | while (__findenv(name, (int)(np - name), &tmpoff)) { |
123 | for (P = &environ[tmpoff];; ++P) |
124 | if (!(*P = *(P + 1))) |
125 | break; |
126 | } |
127 | } else { /* create new slot */ |
128 | size_t cnt = 0; |
129 | |
130 | if (environ != NULL((void *)0)) { |
131 | for (P = environ; *P != NULL((void *)0); P++) |
132 | ; |
133 | cnt = P - environ; |
134 | } |
135 | P = reallocarray(lastenv, cnt + 2, sizeof(char *)); |
136 | if (!P) |
137 | return (-1); |
138 | if (lastenv != environ && environ != NULL((void *)0)) |
139 | memcpy(P, environ, cnt * sizeof(char *)); |
140 | lastenv = environ = P; |
141 | offset = cnt; |
142 | environ[cnt + 1] = NULL((void *)0); |
143 | } |
144 | if (!(environ[offset] = /* name + `=' + value */ |
145 | malloc((int)(np - name) + l_value + 2))) |
146 | return (-1); |
147 | for (C = environ[offset]; (*C = *name++) && *C != '='; ++C) |
148 | ; |
149 | for (*C++ = '='; (*C++ = *value++); ) |
150 | ; |
151 | return (0); |
152 | } |
153 | DEF_WEAK(setenv)__asm__(".weak " "setenv" " ; " "setenv" " = " "_libc_setenv" ); |
154 | |
155 | /* |
156 | * unsetenv(name) -- |
157 | * Delete environmental variable "name". |
158 | */ |
159 | int |
160 | unsetenv(const char *name) |
161 | { |
162 | char **P; |
163 | const char *np; |
164 | int offset = 0; |
165 | |
166 | if (!name || !*name) { |
167 | errno(*__errno()) = EINVAL22; |
168 | return (-1); |
169 | } |
170 | for (np = name; *np && *np != '='; ++np) |
171 | ; |
172 | if (*np) { |
173 | errno(*__errno()) = EINVAL22; |
174 | return (-1); /* has `=' in name */ |
175 | } |
176 | |
177 | /* could be set multiple times */ |
178 | while (__findenv(name, (int)(np - name), &offset)) { |
179 | for (P = &environ[offset];; ++P) |
180 | if (!(*P = *(P + 1))) |
181 | break; |
182 | } |
183 | return (0); |
184 | } |
185 | DEF_WEAK(unsetenv)__asm__(".weak " "unsetenv" " ; " "unsetenv" " = " "_libc_unsetenv" ); |