--- libaitio/src/Attic/tools.c 2011/05/17 19:49:55 1.4.4.2 +++ libaitio/src/Attic/tools.c 2011/06/07 11:49:39 1.5 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: tools.c,v 1.4.4.2 2011/05/17 19:49:55 misho Exp $ +* $Id: tools.c,v 1.5 2011/06/07 11:49:39 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -239,3 +239,59 @@ io_CopyEnv(const char **oldenv) return newenv; } +/* + * io_ExecArgs() Build exec arguments from other array + * @psProg = Program name for execute + * @oldarg = Arguments array + * return: NULL error; !=NULL Allocated execution array(must be free) +*/ +char ** +io_ExecArgs(const char *psProg, const char **oldarg) +{ + char **newarg, **el; + register int i, num; + + if (!psProg || !oldarg) + return NULL; + else + newarg = el = NULL; + + /* count items arguments */ + for (num = 0; oldarg[num]; num++); + + /* create and copy new arguments */ + newarg = calloc(num + 2, sizeof(char*)); + if (!newarg) { + LOGERR; + return NULL; + } else + el = newarg; + + *el = strdup(psProg); + el++; + + for (i = 0; oldarg[i]; i++, el++) + *el = strdup(oldarg[i]); + *el = NULL; + + return newarg; +} + +/* + * io_FreeNullTerm() Free dynamic allocated null terminated array with strings + * @arr = Pointer to array for free + * return: none +*/ +inline void +io_FreeNullTerm(char *** __restrict arr) +{ + char **a; + + if (arr && *arr) { + a = *arr; + while (a && *a) + free(*a++); + free(*arr); + *arr = NULL; + } +}