1 | //env.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2013 |
---|
5 | * |
---|
6 | * This file is part of libroar a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * libroar is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | * NOTE for everyone want's to change something and send patches: |
---|
25 | * read README and HACKING! There a addition information on |
---|
26 | * the license of this document you need to read before you send |
---|
27 | * any patches. |
---|
28 | * |
---|
29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
30 | * or libpulse*: |
---|
31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
34 | */ |
---|
35 | |
---|
36 | #include "libroar.h" |
---|
37 | |
---|
38 | int roar_env_set(struct roar_keyval * keyval) { |
---|
39 | #if defined(ROAR_HAVE_PUTENV) && !defined(ROAR_HAVE_SETENV) |
---|
40 | size_t len; |
---|
41 | char * str; |
---|
42 | #endif |
---|
43 | |
---|
44 | if ( keyval == NULL || keyval->key == NULL ) { |
---|
45 | roar_err_set(ROAR_ERROR_FAULT); |
---|
46 | return -1; |
---|
47 | } |
---|
48 | |
---|
49 | #ifdef ROAR_HAVE_SETENV |
---|
50 | return setenv(keyval->key, keyval->value, 1); |
---|
51 | #elif defined(ROAR_HAVE_PUTENV) |
---|
52 | len = strlen(keyval->key) + strlen(keyval->value) + 2; |
---|
53 | |
---|
54 | // TODO: does this leak memory? |
---|
55 | if ( (str = malloc(len)) == NULL ) { |
---|
56 | return -1; |
---|
57 | } |
---|
58 | |
---|
59 | snprintf(str, len, "%s=%s", keyval->key, keyval->value); |
---|
60 | |
---|
61 | return putenv(str) == 0 ? 0 : -1; |
---|
62 | #else |
---|
63 | roar_strap(ROAR_TRAP_GROUP_LIBROAR, "env_set.not-implemented"); |
---|
64 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
65 | return -1; |
---|
66 | #endif |
---|
67 | } |
---|
68 | |
---|
69 | const char * roar_env_get(const char * key) { |
---|
70 | const char * ret; |
---|
71 | |
---|
72 | if ( key == NULL ) { |
---|
73 | roar_err_set(ROAR_ERROR_FAULT); |
---|
74 | return NULL; |
---|
75 | } |
---|
76 | |
---|
77 | roar_err_clear_all(); |
---|
78 | ret = getenv(key); |
---|
79 | roar_err_update(); |
---|
80 | |
---|
81 | if ( ret == NULL && roar_error == ROAR_ERROR_NONE ) |
---|
82 | roar_err_set(ROAR_ERROR_NOENT); |
---|
83 | |
---|
84 | return ret; |
---|
85 | } |
---|
86 | |
---|
87 | const char * roar_env_get_home(int level) { |
---|
88 | const char * home = roar_env_get("HOME"); |
---|
89 | |
---|
90 | (void)level; |
---|
91 | |
---|
92 | return home; |
---|
93 | } |
---|
94 | |
---|
95 | int roar_env_get_home_r(int level, char * str, size_t len) { |
---|
96 | size_t homelen; |
---|
97 | const char * home; |
---|
98 | |
---|
99 | if ( len == 0 ) |
---|
100 | return 0; |
---|
101 | |
---|
102 | if ( str == NULL ) |
---|
103 | return -1; |
---|
104 | |
---|
105 | *str = 0; // write a single termination byte to be safe... |
---|
106 | |
---|
107 | home = roar_env_get_home(level); |
---|
108 | |
---|
109 | if ( home == NULL ) |
---|
110 | return -1; |
---|
111 | |
---|
112 | homelen = strlen(home); |
---|
113 | |
---|
114 | if ( (len - 1) < homelen ) |
---|
115 | return -1; |
---|
116 | |
---|
117 | strncpy(str, home, len); |
---|
118 | str[homelen] = 0; |
---|
119 | |
---|
120 | return 0; |
---|
121 | } |
---|
122 | |
---|
123 | static char * __get_path(const char ** inpath) { |
---|
124 | char buf_path[64]; |
---|
125 | size_t i; |
---|
126 | |
---|
127 | (*inpath)++; |
---|
128 | |
---|
129 | for (i = 0; i < sizeof(buf_path); i++) { |
---|
130 | if ( (*inpath)[i] == '/' || (*inpath)[i] == 0 ) { |
---|
131 | break; |
---|
132 | } else { |
---|
133 | buf_path[i] = (*inpath)[i]; |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | if ( i == sizeof(buf_path) ) { |
---|
138 | roar_err_set(ROAR_ERROR_NOSPC); |
---|
139 | return NULL; |
---|
140 | } |
---|
141 | |
---|
142 | buf_path[i] = 0; |
---|
143 | |
---|
144 | (*inpath) += i; |
---|
145 | |
---|
146 | return roar_libroar_get_path(buf_path, 0, NULL, NULL); |
---|
147 | } |
---|
148 | |
---|
149 | int roar_env_render_path_r(char * out, size_t len, const char * inpath) { |
---|
150 | size_t inlen; |
---|
151 | size_t needlen = 0; |
---|
152 | size_t homelen = 0; |
---|
153 | const char * home; |
---|
154 | const char * prefix = NULL; |
---|
155 | char * prefix_path = NULL; |
---|
156 | enum { UNKNOWN, COPY, HOME, PREFIX } mode = UNKNOWN; |
---|
157 | |
---|
158 | if ( len == 0 && inpath == NULL ) |
---|
159 | return 0; |
---|
160 | |
---|
161 | if ( inpath == NULL ) |
---|
162 | return -1; |
---|
163 | |
---|
164 | inlen = strlen(inpath); |
---|
165 | |
---|
166 | if ( len == 0 && inlen == 0 ) |
---|
167 | return 0; |
---|
168 | |
---|
169 | if ( len == 0 ) |
---|
170 | return -1; |
---|
171 | |
---|
172 | if ( out == NULL ) |
---|
173 | return -1; |
---|
174 | |
---|
175 | home = roar_env_get_home(0); |
---|
176 | |
---|
177 | if ( home != NULL ) |
---|
178 | homelen = strlen(home); |
---|
179 | |
---|
180 | if ( inpath[0] == '/' ) { |
---|
181 | needlen = inlen; |
---|
182 | mode = COPY; |
---|
183 | } else if ( inpath[0] == '~' && inpath[1] == '/' ) { |
---|
184 | if ( homelen == 0 ) /* we don't know our $HOME */ |
---|
185 | return -1; |
---|
186 | |
---|
187 | needlen = inlen + homelen - 1; |
---|
188 | mode = HOME; |
---|
189 | prefix = home; |
---|
190 | inpath++; |
---|
191 | } else if ( inpath[0] == '$' ) { |
---|
192 | prefix_path = __get_path(&inpath); |
---|
193 | if ( prefix_path == NULL ) |
---|
194 | return -1; |
---|
195 | needlen = roar_mm_strlen(inpath) + roar_mm_strlen(prefix_path) + 1; |
---|
196 | mode = PREFIX; |
---|
197 | prefix = prefix_path; |
---|
198 | } |
---|
199 | |
---|
200 | if ( len < (needlen + 1) ) { |
---|
201 | if ( prefix_path != NULL ) |
---|
202 | roar_mm_free(prefix_path); |
---|
203 | roar_err_set(ROAR_ERROR_NOSPC); |
---|
204 | return -1; |
---|
205 | } |
---|
206 | |
---|
207 | switch (mode) { |
---|
208 | case COPY: |
---|
209 | strncpy(out, inpath, len); |
---|
210 | out[len-1] = 0; |
---|
211 | break; |
---|
212 | case HOME: |
---|
213 | case PREFIX: |
---|
214 | roar_mm_strlcpy(out, prefix, len); |
---|
215 | roar_mm_strlcat(out, inpath, len); // strip only ~, so we have the / if home is not /-terminated. |
---|
216 | break; |
---|
217 | default: |
---|
218 | if ( prefix_path != NULL ) |
---|
219 | roar_mm_free(prefix_path); |
---|
220 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
221 | return -1; |
---|
222 | break; |
---|
223 | } |
---|
224 | |
---|
225 | if ( prefix_path != NULL ) |
---|
226 | roar_mm_free(prefix_path); |
---|
227 | return 0; |
---|
228 | } |
---|
229 | |
---|
230 | //ll |
---|