Root/
Source at commit 6a1f486 created 13 years 3 months ago. By Peter Zotov, Reworked directory structure. | |
---|---|
1 | /* |
2 | * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors. |
3 | * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com> |
4 | * |
5 | * This program is free software: you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation, either version 3 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | */ |
18 | |
19 | #include <string.h> |
20 | #include <stdlib.h> |
21 | #include <string.h> |
22 | #include <stdio.h> |
23 | |
24 | #include "config.h" |
25 | #include "debug.h" |
26 | |
27 | char **cfg_environ = NULL; |
28 | |
29 | static int env_size() { |
30 | int size = 0; |
31 | |
32 | if(cfg_environ == NULL) |
33 | return 0; |
34 | |
35 | for(int i = 0; cfg_environ[i] != NULL; i++) |
36 | size++; |
37 | |
38 | return size; |
39 | } |
40 | |
41 | char *cfg_getenv(const char *variable) { |
42 | if(cfg_environ == NULL) |
43 | return NULL; |
44 | |
45 | size_t len = strlen(variable); |
46 | |
47 | for(int i = 0; cfg_environ[i] != NULL; i++) { |
48 | char *str = cfg_environ[i], *sep = strchr(str, '='); |
49 | |
50 | if(sep - str == len && memcmp(str, variable, len) == 0) { |
51 | return sep + 1; |
52 | } |
53 | } |
54 | |
55 | return NULL; |
56 | } |
57 | |
58 | void cfg_unsetenv(const char *variable) { |
59 | int size = env_size(); |
60 | |
61 | if(size == 0) |
62 | return; |
63 | |
64 | size_t len = strlen(variable); |
65 | |
66 | for(int i = 0; cfg_environ[i] != NULL; i++) { |
67 | char *str = cfg_environ[i], *sep = strchr(str, '='); |
68 | |
69 | if(sep - str == len && memcmp(str, variable, len) == 0) { |
70 | free(str); |
71 | |
72 | memcpy(cfg_environ + i, cfg_environ + i + 1, sizeof(char *) * (size - i)); |
73 | |
74 | cfg_environ = realloc(cfg_environ, sizeof(char *) * size); |
75 | |
76 | return; |
77 | } |
78 | } |
79 | } |
80 | |
81 | void cfg_setenv(const char *variable, const char *newval) { |
82 | int size = env_size(); |
83 | |
84 | size_t len = strlen(variable); |
85 | |
86 | char *newstr = malloc(len + 1 + strlen(newval) + 1); |
87 | |
88 | strcpy(newstr, variable); |
89 | strcat(newstr, "="); |
90 | strcat(newstr, newval); |
91 | |
92 | if(size > 0) { |
93 | for(int i = 0; cfg_environ[i] != NULL; i++) { |
94 | char *str = cfg_environ[i], *sep = strchr(str, '='); |
95 | |
96 | if(sep - str == len && memcmp(str, variable, len) == 0) { |
97 | free(str); |
98 | |
99 | cfg_environ[i] = newstr; |
100 | |
101 | return; |
102 | } |
103 | } |
104 | } |
105 | |
106 | cfg_environ = realloc(cfg_environ, sizeof(char *) * (size + 2)); |
107 | |
108 | cfg_environ[size] = newstr; |
109 | cfg_environ[size + 1] = NULL; |
110 | } |
111 |