tAdd clearitem() to recursively free a dir item - sacc - sacc (saccomys): simple gopher client.
Log
Files
Refs
LICENSE
---
commit da486e90d17187e1fe8d8ed26982840059aaf968
parent 97eaeb752ee3fd28ac14199692a5082e257ef10e
Author: Quentin Rameau 
Date:   Tue, 11 Jul 2017 10:31:38 +0200

Add clearitem() to recursively free a dir item

Diffstat:
  common.h                            |       2 ++
  sacc.c                              |      23 ++++++++++++++++++++++-

2 files changed, 24 insertions(+), 1 deletion(-)
---
diff --git a/common.h b/common.h
t@@ -1,3 +1,5 @@
+#define clear(p)        do { void **_p = (void **)(p); free(*_p); *_p = NULL; } while (0);
+
 typedef struct item Item;
 typedef struct dir Dir;
 
diff --git a/sacc.c b/sacc.c
t@@ -91,6 +91,26 @@ usage(void)
         die("usage: sacc URL");
 }
 
+static void
+clearitem(Item *item)
+{
+        Dir *dir = item->dir;
+        Item **items;
+        size_t i;
+
+        if (dir) {
+                items = dir->items;
+                for (i = 0; i < dir->nitems; ++i) {
+                        clearitem(items[i]);
+                        free(items[i]);
+                }
+                free(items);
+                clear(&item->dir);
+        }
+
+        clear(&item->raw);
+}
+
 const char *
 typedisplay(char t)
 {
t@@ -559,7 +579,8 @@ moldentry(char *url)
 static void
 cleanup(void)
 {
-        free(mainentry); /* TODO free all tree recursively */
+        clearitem(mainentry);
+        free(mainentry);
         free(mainurl);
         uicleanup();
 }