strv.c revision 6091827530d6dd43479d6709fb6e9f745c11e900
/*-*- Mode: C; c-basic-offset: 8 -*-*/
#include <assert.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "util.h"
#include "strv.h"
assert(l);
for (; *l; l++)
return *l;
return NULL;
}
void strv_free(char **l) {
char **k;
if (!l)
return;
for (k = l; *k; k++)
free(*k);
free(l);
}
char **strv_copy(char **l) {
char **r, **k;
return NULL;
for (k = r; *l; k++, l++)
if (!(*k = strdup(*l)))
goto fail;
*k = NULL;
return r;
fail:
for (k--, l--; k >= r; k--, l--)
free(*k);
return NULL;
}
unsigned strv_length(char **l) {
unsigned n = 0;
if (!l)
return 0;
for (; *l; l++)
n++;
return n;
}
char **strv_new(const char *x, ...) {
const char *s;
char **a;
unsigned n = 0, i = 0;
if (x) {
n = 1;
n++;
}
if (!(a = new(char*, n+1)))
return NULL;
if (x) {
if (!(a[i] = strdup(x))) {
free(a);
return NULL;
}
i++;
if (!(a[i] = strdup(s)))
goto fail;
i++;
}
}
a[i] = NULL;
return a;
fail:
for (; i > 0; i--)
if (a[i-1])
free(a[i-1]);
free(a);
return NULL;
}