*
* 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
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);
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)
printf("WARNING: '%s' not in imported env, deleting it!\n", localvars[i]);
}
+end:
debug("INSERT: done\n");
return 1; /* everything OK */
}