return (-1);
}
- debug("EXPORT table = %p, htab.size = %d, htab.filled = %d, size = %d\n",
- htab, htab->size, htab->filled, size);
+ debug("EXPORT table = %p, htab.size = %d, htab.filled = %d, "
+ "size = %zu\n", htab, htab->size, htab->filled, size);
/*
* Pass 1:
* search used entries,
/* Check if the user supplied buffer size is sufficient */
if (size) {
if (size < totlen + 1) { /* provided buffer too small */
- printf("Env export buffer too small: %d, but need %d\n",
- size, totlen + 1);
+ printf("Env export buffer too small: %zu, "
+ "but need %zu\n", size, totlen + 1);
__set_errno(ENOMEM);
return (-1);
}
* himport()
*/
+/* Check whether variable name is amongst vars[] */
+static int is_var_in_set(const char *name, int nvars, char * const vars[])
+{
+ int i = 0;
+
+ /* No variables specified means process all of them */
+ if (nvars == 0)
+ return 1;
+
+ for (i = 0; i < nvars; i++) {
+ if (!strcmp(name, vars[i]))
+ return 1;
+ }
+ debug("Skipping non-listed variable %s\n", name);
+
+ return 0;
+}
+
/*
* Import linearized data into hash table.
*
*/
int himport_r(struct hsearch_data *htab,
- const char *env, size_t size, const char sep, int flag)
+ const char *env, size_t size, const char sep, int flag,
+ int nvars, char * const vars[], int do_apply)
{
char *data, *sp, *dp, *name, *value;
/* we allocate new space to make sure we can write to the array */
if ((data = malloc(size)) == NULL) {
- debug("himport_r: can't malloc %d bytes\n", size);
+ debug("himport_r: can't malloc %zu bytes\n", size);
__set_errno(ENOMEM);
return 0;
}
*dp++ = '\0'; /* terminate name */
debug("DELETE CANDIDATE: \"%s\"\n", name);
+ if (!is_var_in_set(name, nvars, vars))
+ continue;
if (hdelete_r(name, htab) == 0)
debug("DELETE ERROR ##############################\n");
*sp++ = '\0'; /* terminate value */
++dp;
+ /* Skip variables which are not supposed to be processed */
+ if (!is_var_in_set(name, nvars, vars))
+ continue;
+
/* enter into hash table */
e.key = name;
e.data = value;
+ /* if there is an apply function, check what it has to say */
+ if (do_apply && htab->apply != NULL) {
+ debug("searching before calling cb function"
+ " for %s\n", name);
+ /*
+ * Search for variable in existing env, so to pass
+ * its previous value to the apply callback
+ */
+ hsearch_r(e, FIND, &rv, htab);
+ debug("previous value was %s\n", rv ? rv->data : "");
+ if (htab->apply(name, rv ? rv->data : NULL,
+ value, flag)) {
+ debug("callback function refused to set"
+ " variable %s, skipping it!\n", name);
+ continue;
+ }
+ }
+
hsearch_r(e, ENTER, &rv, htab);
if (rv == NULL) {
printf("himport_r: can't insert \"%s=%s\" into hash table\n",