X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fstrmap%2Fstrmap.c;h=9fa51d0d40db4cf89fb16041585d71ea163b2238;hp=7d03cca43ad7ff709bdcd9699ac31b5ad74fe309;hb=516c47790828cfb892fecdbe03a6928c345d29b2;hpb=20f3b260313fb4d5566aeb0d0c5439574e914e2d;ds=sidebyside diff --git a/ccan/strmap/strmap.c b/ccan/strmap/strmap.c index 7d03cca4..9fa51d0d 100644 --- a/ccan/strmap/strmap.c +++ b/ccan/strmap/strmap.c @@ -5,6 +5,7 @@ #include #include #include +#include struct node { /* These point to strings or nodes. */ @@ -38,12 +39,13 @@ void *strmap_get_(const struct strmap *map, const char *member) { struct strmap *n; - /* Empty map? */ - if (!map->u.n) - return NULL; - n = closest((struct strmap *)map, member); - if (streq(member, n->u.s)) - return n->v; + /* Not empty map? */ + if (map->u.n) { + n = closest((struct strmap *)map, member); + if (streq(member, n->u.s)) + return n->v; + } + errno = ENOENT; return NULL; } @@ -72,6 +74,7 @@ bool strmap_add_(struct strmap *map, const char *member, const void *value) for (byte_num = 0; n->u.s[byte_num] == member[byte_num]; byte_num++) { if (member[byte_num] == '\0') { /* All identical! */ + errno = EEXIST; return false; } } @@ -86,7 +89,7 @@ bool strmap_add_(struct strmap *map, const char *member, const void *value) /* Allocate new node. */ newn = malloc(sizeof(*newn)); if (!newn) { - /* FIXME */ + errno = ENOMEM; return false; } newn->byte_num = byte_num; @@ -127,8 +130,10 @@ char *strmap_del_(struct strmap *map, const char *member, void **valuep) u8 direction = 0; /* prevent bogus gcc warning. */ /* Empty map? */ - if (!map->u.n) + if (!map->u.n) { + errno = ENOENT; return NULL; + } /* Find closest, but keep track of parent. */ n = map; @@ -146,8 +151,10 @@ char *strmap_del_(struct strmap *map, const char *member, void **valuep) } /* Did we find it? */ - if (!streq(member, n->u.s)) + if (!streq(member, n->u.s)) { + errno = ENOENT; return NULL; + } ret = n->u.s; if (valuep) @@ -167,17 +174,19 @@ char *strmap_del_(struct strmap *map, const char *member, void **valuep) } static bool iterate(struct strmap n, - bool (*handle)(const char *, void *, void *), void *data) + bool (*handle)(const char *, void *, void *), + const void *data) { if (n.v) - return handle(n.u.s, n.v, data); + return handle(n.u.s, n.v, (void *)data); return iterate(n.u.n->child[0], handle, data) - || iterate(n.u.n->child[1], handle, data); + && iterate(n.u.n->child[1], handle, data); } void strmap_iterate_(const struct strmap *map, - bool (*handle)(const char *, void *, void *), void *data) + bool (*handle)(const char *, void *, void *), + const void *data) { /* Empty map? */ if (!map->u.n)