OSDN Git Service

Genericize llist code a bit: rename llist_free() to llist_traverse(), and no longer...
authorRob Landley <rob@landley.net>
Sun, 15 Jul 2012 22:22:04 +0000 (17:22 -0500)
committerRob Landley <rob@landley.net>
Sun, 15 Jul 2012 22:22:04 +0000 (17:22 -0500)
lib/args.c
lib/lib.h
lib/llist.c
toys/df.c
toys/patch.c
toys/tail.c
toys/toysh.c
toys/which.c

index f4ceba1..e3dcb26 100644 (file)
@@ -413,4 +413,9 @@ notflag:
        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);
+       }
 }
index 4c23009..8028737 100644 (file)
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -35,7 +35,7 @@ struct double_list {
        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);
index 8588721..4799db1 100644 (file)
@@ -6,15 +6,12 @@
 
 #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;
index cff8584..72c1519 100644 (file)
--- a/toys/df.c
+++ b/toys/df.c
@@ -150,5 +150,5 @@ void df_main(void)
                }
        }
 
-       if (CFG_TOYBOX_FREE) llist_free(mtlist, NULL);
+       if (CFG_TOYBOX_FREE) llist_traverse(mtlist, free);
 }
index 95702ae..647bb18 100644 (file)
@@ -108,7 +108,7 @@ static void fail_hunk(void)
        // 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;
@@ -221,13 +221,13 @@ static int apply_one_hunk(void)
 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;
index a029eca..8783d6b 100644 (file)
@@ -130,7 +130,7 @@ static int try_lseek(int fd, long bytes, long lines)
        }
 
        // Output stored data
-       llist_free(list, dump_chunk);
+       llist_traverse(list, dump_chunk);
 
        // In case of -f
        lseek(fd, bytes, SEEK_SET);
@@ -201,7 +201,7 @@ static void do_tail(int fd, char *name)
                }
 
                // Output/free the buffer.
-               llist_free(list, dump_chunk);
+               llist_traverse(list, dump_chunk);
 
        // Measuring from the beginning of the file.
        } else for (;;) {
index 365c47f..c2f494d 100644 (file)
@@ -342,7 +342,7 @@ static void handle(char *command)
                // Run those commands
 
                run_pipeline(&line);
-               llist_free(line.cmd, free_cmd);
+               llist_traverse(line.cmd, free_cmd);
        }
 }
 
index 0ffc725..4923859 100644 (file)
@@ -53,7 +53,7 @@ static int which_in_path(char *filename)
                        puts(list->str);
                        // If we should stop at one match, do so
                        if (!toys.optflags) {
-                               llist_free(list, NULL);
+                               llist_traverse(list, free);
                                break;
                        }
                }