commit e1f433927a0ca2e71ffdf6502c15ef09b3a6bfe8
parent a567766e11a78f57b8fab6283cf6e2738a26ed97
Author: Cem Keylan <cem@ckyln.com>
Date: Wed, 7 Oct 2020 21:36:02 +0300
rm.c: directory removing function
Diffstat:
1 file changed, 39 insertions(+), 0 deletions(-)
diff --git a/libutil/rm.c b/libutil/rm.c
@@ -0,0 +1,39 @@
+/* Remove directories
+ *
+ * This file is part of sysmgr.
+ *
+ * Function mostly taken from:
+ * https://stackoverflow.com/a/5467788
+ * Licensed under CC BY-SA 2.5
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <ftw.h>
+
+#define UNUSED(x) (void)(x)
+
+#include "../util.h"
+
+static int
+rm_files(const char *pathname, const struct stat *sbuf, int type, struct FTW *ftwb)
+{
+ UNUSED(sbuf);
+ UNUSED(ftwb);
+ UNUSED(type);
+
+ if (remove(pathname) < 0) {
+ perror("remove");
+ return -1;
+ }
+ return 0;
+}
+
+int
+rm_rf(char *path)
+{
+ if (nftw(path, rm_files, 64, FTW_DEPTH|FTW_PHYS) < 0) {
+ perror("nftw");
+ return -1;
+ }
+ return 0;
+}