]> git.ozlabs.org Git - ccan/commitdiff
strmap: set errno on strmap_add() failures.
authorRusty Russell <rusty@rustcorp.com.au>
Wed, 26 Oct 2011 06:25:38 +0000 (16:55 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Wed, 26 Oct 2011 06:25:38 +0000 (16:55 +1030)
ccan/strmap/strmap.c
ccan/strmap/strmap.h
ccan/strmap/test/run.c

index 7d03cca43ad7ff709bdcd9699ac31b5ad74fe309..2b89fe0da66cdf8185b4096681175601a2a01fb0 100644 (file)
@@ -5,6 +5,7 @@
 #include <ccan/ilog/ilog.h>
 #include <assert.h>
 #include <stdlib.h>
+#include <errno.h>
 
 struct node {
        /* These point to strings or nodes. */
@@ -72,6 +73,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 +88,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;
index 77197507c742707a5c8ff3e3e41d36ad27d635c5..e85b8c8f898f599d9d292f8367d1dcf3d7468fdd 100644 (file)
@@ -94,8 +94,8 @@ void *strmap_get_(const struct strmap *map, const char *member);
  * @member: the string to place in the map.
  * @v: the (non-NULL) value.
  *
- * This returns false if we run out of memory, or (more normally) if that
- * string already appears in the map.
+ * This returns false if we run out of memory (errno = ENOMEM), or
+ * (more normally) if that string already appears in the map (EEXIST).
  *
  * Note that the pointer is placed in the map, the string is not copied.  If
  * you want a copy in the map, use strdup().  Similarly for the value.
index 349ef4f7176c040511defc65cfe87fa45e360728..34302846f120f873aa856e2c08f750c48af5745d 100644 (file)
@@ -14,7 +14,7 @@ int main(void)
        char *v;
 
        /* This is how many tests you plan to run */
-       plan_tests(31);
+       plan_tests(32);
 
        strmap_init(&map);
 
@@ -31,6 +31,7 @@ int main(void)
 
        /* Add a duplicate should fail. */
        ok1(!strmap_add(&map, dup, val));
+       ok1(errno == EEXIST);
        ok1(strmap_get(&map, dup) == val);
 
        /* Delete should return original string. */