doc: Add top-level description about U-Boot documentation
[oweals/u-boot.git] / lib / hashtable.c
index 52aab6df6ebc6f4aca53e8e5350315109b345f33..0d288d12d991128a6f3d5690e49215d863c3b0dd 100644 (file)
@@ -40,6 +40,9 @@
 #define        CONFIG_ENV_MAX_ENTRIES 512
 #endif
 
+#define USED_FREE 0
+#define USED_DELETED -1
+
 #include <env_callback.h>
 #include <env_flags.h>
 #include <search.h>
@@ -303,7 +306,7 @@ int hsearch_r(ENTRY item, ACTION action, ENTRY ** retval,
                 */
                unsigned hval2;
 
-               if (htab->table[idx].used == -1
+               if (htab->table[idx].used == USED_DELETED
                    && !first_deleted)
                        first_deleted = idx;
 
@@ -335,13 +338,17 @@ int hsearch_r(ENTRY item, ACTION action, ENTRY ** retval,
                        if (idx == hval)
                                break;
 
+                       if (htab->table[idx].used == USED_DELETED
+                           && !first_deleted)
+                               first_deleted = idx;
+
                        /* If entry is found use it. */
                        ret = _compare_and_overwrite_entry(item, action, retval,
                                htab, flag, hval, idx);
                        if (ret != -1)
                                return ret;
                }
-               while (htab->table[idx].used);
+               while (htab->table[idx].used != USED_FREE);
        }
 
        /* An empty bucket has been found. */
@@ -433,7 +440,7 @@ static void _hdelete(const char *key, struct hsearch_data *htab, ENTRY *ep,
        free(ep->data);
        ep->callback = NULL;
        ep->flags = 0;
-       htab->table[idx].used = -1;
+       htab->table[idx].used = USED_DELETED;
 
        --htab->filled;
 }
@@ -542,9 +549,8 @@ static int match_string(int flag, const char *str, const char *pat, void *priv)
        case H_MATCH_REGEX:
                {
                        struct slre *slrep = (struct slre *)priv;
-                       struct cap caps[slrep->num_caps + 2];
 
-                       if (slre_match(slrep, str, strlen(str), caps))
+                       if (slre_match(slrep, str, strlen(str), NULL))
                                return 1;
                }
                break;
@@ -622,7 +628,7 @@ ssize_t hexport_r(struct hsearch_data *htab, const char sep, int flag,
 
                        list[n++] = ep;
 
-                       totlen += strlen(ep->key) + 2;
+                       totlen += strlen(ep->key);
 
                        if (sep == '\0') {
                                totlen += strlen(ep->data);
@@ -749,8 +755,11 @@ static int drop_var_from_set(const char *name, int nvars, char * vars[])
  *
  * The "flag" argument can be used to control the behaviour: when the
  * H_NOCLEAR bit is set, then an existing hash table will kept, i. e.
- * new data will be added to an existing hash table; otherwise, old
- * data will be discarded and a new hash table will be created.
+ * new data will be added to an existing hash table; otherwise, if no
+ * vars are passed, old data will be discarded and a new hash table
+ * will be created. If vars are passed, passed vars that are not in
+ * the linear list of "name=value" pairs will be removed from the
+ * current hash table.
  *
  * The separator character for the "name=value" pairs can be selected,
  * so we both support importing from externally stored environment
@@ -801,7 +810,7 @@ int himport_r(struct hsearch_data *htab,
        if (nvars)
                memcpy(localvars, vars, sizeof(vars[0]) * nvars);
 
-       if ((flag & H_NOCLEAR) == 0) {
+       if ((flag & H_NOCLEAR) == 0 && !nvars) {
                /* Destroy old hash table if one exists */
                debug("Destroy Hash Table: %p table = %p\n", htab,
                       htab->table);
@@ -933,6 +942,9 @@ int himport_r(struct hsearch_data *htab,
        debug("INSERT: free(data = %p)\n", data);
        free(data);
 
+       if (flag & H_NOCLEAR)
+               goto end;
+
        /* process variables which were not considered */
        for (i = 0; i < nvars; i++) {
                if (localvars[i] == NULL)
@@ -951,6 +963,7 @@ int himport_r(struct hsearch_data *htab,
                        printf("WARNING: '%s' not in imported env, deleting it!\n", localvars[i]);
        }
 
+end:
        debug("INSERT: done\n");
        return 1;               /* everything OK */
 }