[Libclc-developers] string function clc_stralloc()
Status: Planning
Brought to you by:
augestad
|
From: Hallvard B F. <h.b...@us...> - 2003-03-18 18:55:10
|
Here is a function I've seen with several names: stralloc(), concat(),
pstrcat(). I prefer clc_str<someting>.
This code is written by Hallvard Furuseth and put in the public domain.
Insert any copyright you want.
NAME
clc_stralloc - allocate a concatenation of strings
SYNOPSYS
#include "clc_string.h"
char *clc_stralloc(const char *arg1, ...);
DESCRIPTION
This function takes a number of string arguments followed by
(char*)0. It returns a malloced concatenation of the arguments,
or NULL if space could not be allocated.
#include <stdlib.h>
#include <stdarg.h>
#include "clc_string.h"
char *
clc_stralloc(const char *arg1, ...)
{
size_t len;
const char *arg;
char *ret, *end;
va_list ap;
len = 1;
va_start(ap, arg1);
for (arg = arg1; arg != NULL; arg = va_arg(ap, const char *))
len += strlen(arg);
va_end(ap);
ret = malloc(len);
if (ret != NULL) {
end = ret;
*end = '\0';
va_start(ap, arg1);
for (arg = arg1; arg != NULL; arg = va_arg(ap, const char *))
end = clc_stpcpy(end, arg);
va_end(ap);
}
return ret;
}
--
Hallvard
|