Blame SOURCES/0057-daemon-Add-filter_list-utility-function.patch

d60042
From 1c976cc5d1add98eee09c38ac8e01634c94f3778 Mon Sep 17 00:00:00 2001
d60042
From: "Richard W.M. Jones" <rjones@redhat.com>
d60042
Date: Thu, 12 Mar 2020 13:59:05 +0000
d60042
Subject: [PATCH] daemon: Add filter_list utility function.
d60042
d60042
For filtering lists of strings based on a predicate.
d60042
d60042
(cherry picked from commit af8ed266a282bb20882a9ffb611bd64243d19218)
d60042
---
d60042
 daemon/daemon.h |  2 ++
d60042
 daemon/utils.c  | 30 ++++++++++++++++++++++++++++++
d60042
 2 files changed, 32 insertions(+)
d60042
d60042
diff --git a/daemon/daemon.h b/daemon/daemon.h
d60042
index 66bfdc49e..115591728 100644
d60042
--- a/daemon/daemon.h
d60042
+++ b/daemon/daemon.h
d60042
@@ -22,6 +22,7 @@
d60042
 #include <stdio.h>
d60042
 #include <stdarg.h>
d60042
 #include <stdint.h>
d60042
+#include <stdbool.h>
d60042
 #include <errno.h>
d60042
 #include <unistd.h>
d60042
 
d60042
@@ -74,6 +75,7 @@ extern void free_stringsbuf (struct stringsbuf *sb);
d60042
 extern struct stringsbuf split_lines_sb (char *str);
d60042
 extern char **split_lines (char *str);
d60042
 extern char **empty_list (void);
d60042
+extern char **filter_list (bool (*p) (const char *), char **strs);
d60042
 extern int is_power_of_2 (unsigned long v);
d60042
 extern void trim (char *str);
d60042
 extern int parse_btrfsvol (const char *desc, mountable_t *mountable);
d60042
diff --git a/daemon/utils.c b/daemon/utils.c
d60042
index c3f88bcab..e87233d0f 100644
d60042
--- a/daemon/utils.c
d60042
+++ b/daemon/utils.c
d60042
@@ -24,6 +24,7 @@
d60042
 
d60042
 #include <stdio.h>
d60042
 #include <stdlib.h>
d60042
+#include <stdbool.h>
d60042
 #include <string.h>
d60042
 #include <unistd.h>
d60042
 #include <rpc/types.h>
d60042
@@ -482,6 +483,35 @@ empty_list (void)
d60042
   return ret.argv;
d60042
 }
d60042
 
d60042
+/**
d60042
+ * Filter a list of strings.  Returns a newly allocated list of only
d60042
+ * the strings where C

.

d60042
+ *
d60042
+ * B<Note> it does not copy the strings, be careful not to double-free
d60042
+ * them.
d60042
+ */
d60042
+char **
d60042
+filter_list (bool (*p) (const char *str), char **strs)
d60042
+{
d60042
+  DECLARE_STRINGSBUF (ret);
d60042
+  size_t i;
d60042
+
d60042
+  for (i = 0; strs[i] != NULL; ++i) {
d60042
+    if (p (strs[i])) {
d60042
+      if (add_string_nodup (&ret, strs[i]) == -1) {
d60042
+        free (ret.argv);
d60042
+        return NULL;
d60042
+      }
d60042
+    }
d60042
+  }
d60042
+  if (end_stringsbuf (&ret) == -1) {
d60042
+    free (ret.argv);
d60042
+    return NULL;
d60042
+  }
d60042
+
d60042
+  return take_stringsbuf (&ret;;
d60042
+}
d60042
+
d60042
 /**
d60042
  * Skip leading and trailing whitespace, updating the original string
d60042
  * in-place.
d60042
-- 
b155d0
2.26.2
d60042