util: add hexmem() and strextend() calls

This commit is contained in:
Lennart Poettering 2013-03-19 20:01:18 +01:00
parent a9c55a882f
commit 66e35261ef
2 changed files with 72 additions and 1 deletions

View File

@ -1029,7 +1029,6 @@ int rmdir_parents(const char *path, const char *stop) {
return 0;
}
char hexchar(int x) {
static const char table[16] = "0123456789abcdef";
@ -1050,6 +1049,23 @@ int unhexchar(char c) {
return -1;
}
char *hexmem(const void *p, size_t l) {
char *r, *z;
const uint8_t *x;
z = r = malloc(l * 2 + 1);
if (!r)
return NULL;
for (x = p; x < (const uint8_t*) p + l; x++) {
*(z++) = hexchar(*x >> 4);
*(z++) = hexchar(*x & 15);
}
*z = 0;
return r;
}
char octchar(int x) {
return '0' + (x & 7);
}
@ -5729,3 +5745,55 @@ fail2:
rmdir(template);
return r;
}
char *strextend(char **x, ...) {
va_list ap;
size_t f, l;
char *r, *p;
assert(x);
l = f = *x ? strlen(*x) : 0;
va_start(ap, x);
for (;;) {
const char *t;
size_t n;
t = va_arg(ap, const char *);
if (!t)
break;
n = strlen(t);
if (n > ((size_t) -1) - l) {
va_end(ap);
return NULL;
}
l += n;
}
va_end(ap);
r = realloc(*x, l+1);
if (!r)
return NULL;
p = r + f;
va_start(ap, x);
for (;;) {
const char *t;
t = va_arg(ap, const char *);
if (!t)
break;
p = stpcpy(p, t);
}
va_end(ap);
*p = 0;
*x = r;
return r + l;
}

View File

@ -605,3 +605,6 @@ static inline void *mempset(void *s, int c, size_t n) {
memset(s, c, n);
return (char*)s + n;
}
char *hexmem(const void *p, size_t l);
char *strextend(char **x, ...);