e16/src/memory.c

250 lines
4.9 KiB
C

/*
* Copyright (C) 2000-2005 Carsten Haitzler, Geoff Harrison and various contributors
* Copyright (C) 2005 Kim Woelders
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of the Software, its documentation and marketing & publicity
* materials, and acknowledgment shall be given in the documentation, materials
* and software packages that this Software was used.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "E.h"
#if !USE_LIBC_STRDUP
char *
Estrdup(const char *s)
{
char *ss;
int sz;
if (!s)
return NULL;
sz = strlen(s);
ss = Emalloc(sz + 1);
strncpy(ss, s, sz + 1);
return ss;
}
#endif
#if !USE_LIBC_STRNDUP
char *
Estrndup(const char *s, int n)
{
char *ss;
if (!s)
return NULL;
ss = Emalloc(n + 1);
strncpy(ss, s, n);
ss[n] = '\0';
return ss;
}
#endif
char *
Estrdupcat2(char *ss, const char *s1, const char *s2)
{
char *s;
int len, l1, l2;
if (!ss)
return Estrdup(s2);
len = (ss) ? strlen(ss) : 0;
l1 = (s1) ? strlen(s1) : 0;
l2 = (s2) ? strlen(s2) : 0;
s = Erealloc(ss, len + l1 + l2 + 1);
if (l1)
memcpy(s + len, s1, l1);
if (l2)
memcpy(s + len + l1, s2, l2);
s[len + l1 + l2] = '\0';
return s;
}
char **
EstrlistDup(char **lst, int num)
{
char **ss;
int i;
if (!lst || num <= 0)
return NULL;
ss = (char **)Emalloc((num + 1) * sizeof(char *));
for (i = 0; i < num; i++)
ss[i] = Estrdup(lst[i]);
ss[i] = NULL;
return ss;
}
void
EstrlistFree(char **lst, int num)
{
if (!lst)
return;
while (num--)
if (lst[num])
Efree(lst[num]);
Efree(lst);
}
#if 0 /* FIXME - Remove? */
char *
EstrlistJoin(char **lst, int num)
{
int i, size;
char *s;
if (!lst || num <= 0)
return NULL;
s = NULL;
size = strlen(lst[0]) + 1;
s = Emalloc(size);
strcpy(s, lst[0]);
for (i = 1; i < num; i++)
{
size += strlen(lst[i]) + 1;
s = Erealloc(s, size);
strcat(s, " ");
strcat(s, lst[i]);
}
return s;
}
#endif
char *
EstrlistEncodeEscaped(char *buf, int len, char **lst, int num)
{
int i, j, ch;
char *s, *p;
if (!lst || num <= 0)
return NULL;
j = 0;
s = buf;
p = lst[0];
for (i = 0; i < len - 2; i++)
{
if (!p) /* A string list should not contain NULL items */
break;
ch = *p++;
switch (ch)
{
default:
*s++ = ch;
break;
case '\0':
if (++j >= num)
goto done;
p = lst[j];
*s++ = ' ';
break;
case ' ':
*s++ = '\\';
*s++ = ' ';
i++;
break;
}
}
done:
*s = '\0';
return buf;
}
char **
EstrlistDecodeEscaped(const char *str, int *pnum)
{
int num, len;
const char *s, *p;
char **lst;
if (!str)
return NULL;
lst = NULL;
num = 0;
s = str;
for (;;)
{
while (*s == ' ')
s++;
if (*s == '\0')
break;
lst = Erealloc(lst, (num + 1) * sizeof(char *));
lst[num] = NULL;
len = 0;
for (;;)
{
p = strchr(s, ' ');
if (!p)
p = s + strlen(s);
lst[num] = Erealloc(lst[num], len + p - s + 1);
memcpy(lst[num] + len, s, p - s);
len += p - s;
lst[num][len] = '\0';
s = p;
if (p[-1] == '\\')
{
if (*p)
lst[num][len - 1] = ' ';
else
break;
}
else
{
break;
}
while (*s == ' ')
s++;
if (*s == '\0')
break;
}
num++;
}
/* Append NULL item */
lst = Erealloc(lst, (num + 1) * sizeof(char *));
lst[num] = NULL;
*pnum = num;
return lst;
}
#if !USE_LIBC_SETENV
int
Esetenv(const char *name, const char *value, int overwrite __UNUSED__)
{
char envvar[FILEPATH_LEN_MAX];
Esnprintf(envvar, FILEPATH_LEN_MAX, "%s=%s", name, value);
return putenv(Estrdup(envvar));
}
#endif