if (toys.optc>gof.maxargs)
error_exit("Max %d argument%s", gof.maxargs, letters[!(gof.maxargs-1)]);
if (CFG_HELP) toys.exithelp = 0;
+
+ if (CFG_TOYBOX_FREE) {
+ llist_traverse(gof.opts, free);
+ llist_traverse(gof.longopts, free);
+ }
}
char *data;
};
-void llist_free(void *list, void (*freeit)(void *data));
+void llist_traverse(void *list, void (*using)(void *data));
void *llist_pop(void *list); // actually void **list, but the compiler's dumb
void dlist_add_nomalloc(struct double_list **list, struct double_list *new);
struct double_list *dlist_add(struct double_list **list, char *data);
#include "toys.h"
-// Free all the elements of a linked list
-// if freeit!=NULL call freeit() on each element before freeing it.
-
-void llist_free(void *list, void (*freeit)(void *data))
+// Call a function (such as free()) on each element of a linked list.
+void llist_traverse(void *list, void (*using)(void *data))
{
while (list) {
void *pop = llist_pop(&list);
- if (freeit) freeit(pop);
- else free(pop);
+ using(pop);
// End doubly linked list too.
if (list==pop) break;
}
}
- if (CFG_TOYBOX_FREE) llist_free(mtlist, NULL);
+ if (CFG_TOYBOX_FREE) llist_traverse(mtlist, free);
}
// this file and advance to next file.
TT.state = 2;
- llist_free(TT.current_hunk, do_line);
+ llist_traverse(TT.current_hunk, do_line);
TT.current_hunk = NULL;
delete_tempfile(TT.filein, TT.fileout, &TT.tempname);
TT.state = 0;
out:
// We have a match. Emit changed data.
TT.state = "-+"[reverse];
- llist_free(TT.current_hunk, do_line);
+ llist_traverse(TT.current_hunk, do_line);
TT.current_hunk = NULL;
TT.state = 1;
done:
if (buf) {
buf->prev->next = NULL;
- llist_free(buf, do_line);
+ llist_traverse(buf, do_line);
}
return TT.state;
}
// Output stored data
- llist_free(list, dump_chunk);
+ llist_traverse(list, dump_chunk);
// In case of -f
lseek(fd, bytes, SEEK_SET);
}
// Output/free the buffer.
- llist_free(list, dump_chunk);
+ llist_traverse(list, dump_chunk);
// Measuring from the beginning of the file.
} else for (;;) {
// Run those commands
run_pipeline(&line);
- llist_free(line.cmd, free_cmd);
+ llist_traverse(line.cmd, free_cmd);
}
}
puts(list->str);
// If we should stop at one match, do so
if (!toys.optflags) {
- llist_free(list, NULL);
+ llist_traverse(list, free);
break;
}
}