From 946f7d63d6c39c8a6555da30f54bf3a1548befde Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Tue, 22 Feb 2022 07:36:47 -0600 Subject: [PATCH] Traverse doubly linked list non-infinitely. (Can't test this, don't have the hardware, so trying to do only simple cleanups. Still screwed it up.) --- toys/other/gpiod.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/toys/other/gpiod.c b/toys/other/gpiod.c index 53cfbf9c..cc3ce64f 100644 --- a/toys/other/gpiod.c +++ b/toys/other/gpiod.c @@ -96,15 +96,17 @@ static int comparator(const void *a, const void *b) return strcmp(lhs->data, rhs->data); } +// call cb() in sorted order static void foreach_chip(void (*cb)(char *name)) { - struct double_list **sorted, *chip; + struct double_list **sorted; int i = 0; dirtree_flagread("/dev", DIRTREE_SHUTUP, collect_chips); + if (!TT.chips) return; sorted = xmalloc(TT.chip_count*sizeof(void *)); - for (chip = TT.chips; chip; chip=chip->next, i++) sorted[i] = chip; + for (i = 0; inext; qsort(sorted, TT.chip_count, sizeof(void *), comparator); for (i = 0; i