1 /*
2  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
3  * Copyright (C) 2005-2021 Kim Woelders
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to
7  * deal in the Software without restriction, including without limitation the
8  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9  * sell copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies of the Software, its documentation and marketing & publicity
14  * materials, and acknowledgment shall be given in the documentation, materials
15  * and software packages that this Software was used.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #include "config.h"
25 
26 #include <ctype.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include "util.h"
31 
32 __EXPORT__ void
EfreeNull(void ** p)33 EfreeNull(void **p)
34 {
35    Efree(*p);
36    *p = NULL;
37 }
38 
39 void
EfreeSet(void ** p,void * s)40 EfreeSet(void **p, void *s)
41 {
42    Efree(*p);
43    *p = s;
44 }
45 
46 void
EfreeDup(char ** p,const char * s)47 EfreeDup(char **p, const char *s)
48 {
49    Efree(*p);
50    *p = Estrdup(s);
51 }
52 
53 char               *
Estrtrim(char * s)54 Estrtrim(char *s)
55 {
56    int                 l;
57 
58    while (*s == ' ')
59       s++;
60    if (!*s)
61       return s;
62 
63    l = strlen(s);
64    while (isspace(s[l - 1]))
65       l--;
66    s[l] = '\0';
67 
68    return s;
69 }
70 
71 char               *
Estrdup(const char * s)72 Estrdup(const char *s)
73 {
74 #if USE_LIBC_STRDUP
75    if (s)
76       return strdup(s);
77    return NULL;
78 #else
79    int                 sz;
80    char               *ss;
81 
82    if (!s)
83       return NULL;
84    sz = strlen(s);
85    ss = EMALLOC(char, sz + 1);
86    memcpy(ss, s, sz + 1);
87    return ss;
88 #endif
89 }
90 
91 char               *
Estrndup(const char * s,size_t n)92 Estrndup(const char *s, size_t n)
93 {
94 #if USE_LIBC_STRNDUP
95    if (s)
96       return strndup(s, n);
97    return NULL;
98 #else
99    char               *ss;
100 
101    if (!s)
102       return NULL;
103    ss = EMALLOC(char, n + 1);
104    strncpy(ss, s, n);
105    ss[n] = '\0';
106    return ss;
107 #endif
108 }
109 
110 char               *
Estrdupcat2(char * ss,const char * s1,const char * s2)111 Estrdupcat2(char *ss, const char *s1, const char *s2)
112 {
113    char               *s;
114    int                 len, l1, l2;
115 
116    if (!ss)
117       return Estrdup(s2);
118 
119    len = strlen(ss);
120    l1 = (s1) ? strlen(s1) : 0;
121    l2 = (s2) ? strlen(s2) : 0;
122 
123    s = EREALLOC(char, ss, len + l1 + l2 + 1);
124    if (!s)
125       return NULL;
126    if (s1 && l1)
127       memcpy(s + len, s1, l1);
128    if (s2 && l2)
129       memcpy(s + len + l1, s2, l2);
130    s[len + l1 + l2] = '\0';
131 
132    return s;
133 }
134 
135 #if 0				/* Unused */
136 char              **
137 StrlistDup(char **lst, int num)
138 {
139    char              **ss;
140    int                 i;
141 
142    if (!lst || num <= 0)
143       return NULL;
144 
145    ss = EMALLOC(char *, num + 1);
146    for (i = 0; i < num; i++)
147       ss[i] = Estrdup(lst[i]);
148    ss[i] = NULL;
149 
150    return ss;
151 }
152 #endif
153 
154 void
StrlistFree(char ** lst,int num)155 StrlistFree(char **lst, int num)
156 {
157    if (!lst)
158       return;
159    while (num--)
160       Efree(lst[num]);
161    Efree(lst);
162 }
163 
164 #if 0				/* FIXME - Remove? */
165 char               *
166 StrlistJoin(char **lst, int num)
167 {
168    int                 i, size;
169    char               *s;
170 
171    if (!lst || num <= 0)
172       return NULL;
173 
174    s = NULL;
175 
176    size = strlen(lst[0]) + 1;
177    s = EMALLOC(char, size);
178    strcpy(s, lst[0]);
179    for (i = 1; i < num; i++)
180      {
181 	size += strlen(lst[i]) + 1;
182 	s = EREALLOC(char, s, size);
183 
184 	strcat(s, " ");
185 	strcat(s, lst[i]);
186      }
187 
188    return s;
189 }
190 #endif
191 
192 char               *
StrlistEncodeEscaped(char * buf,int len,char ** lst,int num)193 StrlistEncodeEscaped(char *buf, int len, char **lst, int num)
194 {
195    int                 i, j, ch;
196    char               *s, *p;
197 
198    if (!lst || num <= 0)
199       return NULL;
200 
201    j = 0;
202    s = buf;
203    p = lst[0];
204    for (i = 0; i < len - 2; i++)
205      {
206 	if (!p)			/* A string list should not contain NULL items */
207 	   break;
208 
209 	ch = *p++;
210 	switch (ch)
211 	  {
212 	  default:
213 	     *s++ = ch;
214 	     break;
215 	  case '\0':
216 	     if (++j >= num)
217 		goto done;
218 	     p = lst[j];
219 	     if (!p || !p[0])
220 		goto done;
221 	     *s++ = ' ';
222 	     break;
223 	  case ' ':
224 	     *s++ = '\\';
225 	     *s++ = ' ';
226 	     i++;
227 	     break;
228 	  }
229      }
230 
231  done:
232    *s = '\0';
233    return buf;
234 }
235 
236 char              **
StrlistDecodeEscaped(const char * str,int * pnum)237 StrlistDecodeEscaped(const char *str, int *pnum)
238 {
239    int                 num, len;
240    const char         *s, *p;
241    char              **lst;
242 
243    if (!str)
244       return NULL;
245 
246    lst = NULL;
247    num = 0;
248    s = str;
249    for (;;)
250      {
251 	while (*s == ' ')
252 	   s++;
253 	if (*s == '\0')
254 	   break;
255 
256 	lst = EREALLOC(char *, lst, num + 1);
257 
258 	lst[num] = NULL;
259 	len = 0;
260 
261 	for (;;)
262 	  {
263 	     p = strchr(s, ' ');
264 	     if (!p)
265 		p = s + strlen(s);
266 
267 	     lst[num] = EREALLOC(char, lst[num], len + p - s + 1);
268 
269 	     memcpy(lst[num] + len, s, p - s);
270 	     len += p - s;
271 	     lst[num][len] = '\0';
272 
273 	     s = p;
274 	     if (p[-1] == '\\')
275 	       {
276 		  if (*p)
277 		     lst[num][len - 1] = ' ';
278 		  else
279 		     break;
280 	       }
281 	     else
282 	       {
283 		  break;
284 	       }
285 	     while (*s == ' ')
286 		s++;
287 	     if (*s == '\0')
288 		break;
289 	  }
290 	num++;
291      }
292 
293    /* Append NULL item */
294    lst = EREALLOC(char *, lst, num + 1);
295 
296    lst[num] = NULL;
297 
298    *pnum = num;
299    return lst;
300 }
301 
302 char              **
StrlistFromString(const char * str,int delim,int * num)303 StrlistFromString(const char *str, int delim, int *num)
304 {
305    const char         *s, *p;
306    char              **lst;
307    int                 n, len;
308 
309    lst = NULL;
310    n = 0;
311    for (s = str; s; s = p)
312      {
313 	p = strchr(s, delim);
314 	if (p)
315 	  {
316 	     len = p - s;
317 	     p++;
318 	  }
319 	else
320 	  {
321 	     len = strlen(s);
322 	  }
323 	if (len <= 0)
324 	   continue;
325 
326 	lst = EREALLOC(char *, lst, n + 2);
327 
328 	lst[n++] = Estrndup(s, len);
329      }
330 
331    if (lst)
332       lst[n] = NULL;
333    *num = n;
334    return lst;
335 }
336 
337 static int
_qsort_strcmp(const void * s1,const void * s2)338 _qsort_strcmp(const void *s1, const void *s2)
339 {
340    return strcmp(*(const char **)s1, *(const char **)s2);
341 }
342 
343 void
StrlistSort(char ** lst,int len)344 StrlistSort(char **lst, int len)
345 {
346    qsort(lst, (unsigned int)len, sizeof(char *), _qsort_strcmp);
347 }
348 
349 void
Esetenv(const char * name,const char * value)350 Esetenv(const char *name, const char *value)
351 {
352    if (value)
353      {
354 #if HAVE_SETENV
355 	setenv(name, value, 1);
356 #else
357 	char                buf[4096];
358 
359 	Esnprintf(buf, sizeof(buf), "%s=%s", name, value);
360 	putenv(Estrdup(buf));
361 #endif
362      }
363    else
364      {
365 #if HAVE_UNSETENV
366 	unsetenv(name);
367 #else
368 	if (getenv(name))
369 	   putenv((char *)name);
370 #endif
371      }
372 }
373