#include <ccan/ilog/ilog.h>
#include <assert.h>
#include <stdlib.h>
+#include <errno.h>
struct node {
/* To differentiate us from strings. */
{
const char *str;
- /* Empty set? */
- if (!set->u.n)
- return NULL;
- str = closest(*set, member);
- if (streq(member, str))
- return (char *)str;
+ /* Non-empty set? */
+ if (set->u.n) {
+ str = closest(*set, member);
+ if (streq(member, str))
+ return (char *)str;
+ }
+ errno = ENOENT;
return NULL;
}
/* Substitute magic empty node if this is the empty string */
if (unlikely(!member[0])) {
n->u.n = malloc(sizeof(*n->u.n));
- if (unlikely(!n->u.n))
+ if (unlikely(!n->u.n)) {
+ errno = ENOMEM;
return false;
+ }
n->u.n->nul_byte = '\0';
n->u.n->byte_num = (size_t)-1;
/* Attach the string to child[0] */
for (byte_num = 0; str[byte_num] == member[byte_num]; byte_num++) {
if (member[byte_num] == '\0') {
/* All identical! */
+ errno = EEXIST;
return false;
}
}
/* Allocate new node. */
newn = malloc(sizeof(*newn));
if (!newn) {
- /* FIXME */
+ errno = ENOMEM;
return false;
}
newn->nul_byte = '\0';
u8 direction = 0; /* prevent bogus gcc warning. */
/* Empty set? */
- if (!set->u.n)
+ if (!set->u.n) {
+ errno = ENOENT;
return NULL;
+ }
/* Find closest, but keep track of parent. */
n = set;
if (unlikely(n->u.n->byte_num == (size_t)-1)) {
const char *empty_str = n->u.n->child[0].u.s;
- if (member[0])
+ if (member[0]) {
+ errno = ENOENT;
return NULL;
+ }
/* Sew empty string back so remaining logic works */
free(n->u.n);
}
/* Did we find it? */
- if (!streq(member, n->u.s))
+ if (!streq(member, n->u.s)) {
+ errno = ENOENT;
return NULL;
+ }
ret = n->u.s;