Add clearitem() to recursively free a dir item - sacc - sacc(omys), simple console gopher client
HTML git clone git://bitreich.org/sacc/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/sacc/
DIR Log
DIR Files
DIR Refs
DIR Tags
DIR LICENSE
---
DIR commit da486e90d17187e1fe8d8ed26982840059aaf968
DIR parent 97eaeb752ee3fd28ac14199692a5082e257ef10e
HTML Author: Quentin Rameau <quinq@fifth.space>
Date: Tue, 11 Jul 2017 10:31:38 +0200
Add clearitem() to recursively free a dir item
Diffstat:
M common.h | 2 ++
M sacc.c | 23 ++++++++++++++++++++++-
2 files changed, 24 insertions(+), 1 deletion(-)
---
DIR diff --git a/common.h b/common.h
@@ -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;
DIR diff --git a/sacc.c b/sacc.c
@@ -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)
{
@@ -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();
}