2 * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
3 * Copyright (C) 2005-2008 Kim Woelders
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:
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.
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.
38 while (isspace(s[l - 1]))
46 Estrdup(const char *s)
58 ss = EMALLOC(char, sz + 1);
59 strncpy(ss, s, sz + 1);
65 Estrndup(const char *s, size_t n)
76 ss = EMALLOC(char, n + 1);
84 Estrdupcat2(char *ss, const char *s1, const char *s2)
92 len = (ss) ? strlen(ss) : 0;
93 l1 = (s1) ? strlen(s1) : 0;
94 l2 = (s2) ? strlen(s2) : 0;
96 s = EREALLOC(char, ss, len + l1 + l2 + 1);
100 memcpy(s + len, s1, l1);
102 memcpy(s + len + l1, s2, l2);
103 s[len + l1 + l2] = '\0';
110 StrlistDup(char **lst, int num)
115 if (!lst || num <= 0)
118 ss = EMALLOC(char *, num + 1);
119 for (i = 0; i < num; i++)
120 ss[i] = Estrdup(lst[i]);
128 StrlistFree(char **lst, int num)
137 #if 0 /* FIXME - Remove? */
139 StrlistJoin(char **lst, int num)
144 if (!lst || num <= 0)
149 size = strlen(lst[0]) + 1;
150 s = EMALLOC(char, size);
152 for (i = 1; i < num; i++)
154 size += strlen(lst[i]) + 1;
155 s = EREALLOC(char, s, size);
166 StrlistEncodeEscaped(char *buf, int len, char **lst, int num)
171 if (!lst || num <= 0)
177 for (i = 0; i < len - 2; i++)
179 if (!p) /* A string list should not contain NULL items */
210 StrlistDecodeEscaped(const char *str, int *pnum)
229 lst = EREALLOC(char *, lst, num + 1);
240 lst[num] = EREALLOC(char, lst[num], len + p - s + 1);
242 memcpy(lst[num] + len, s, p - s);
244 lst[num][len] = '\0';
250 lst[num][len - 1] = ' ';
266 /* Append NULL item */
267 lst = EREALLOC(char *, lst, num + 1);
276 StrlistFromString(const char *str, int delim, int *num)
284 for (s = str; s; s = p)
286 p = strchr(s, delim);
299 lst = EREALLOC(char *, lst, n + 2);
301 lst[n++] = Estrndup(s, len);
311 Esetenv(const char *name, const char *value)
316 setenv(name, value, 1);
318 char buf[FILEPATH_LEN_MAX];
320 Esnprintf(buf, FILEPATH_LEN_MAX, "%s=%s", name, value);
321 putenv(Estrdup(buf));
330 putenv((char *)name);