summaryrefslogtreecommitdiffstats
path: root/src/basic/util.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2018-11-18 10:42:39 +0100
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2018-11-18 16:12:53 +0100
commit7f6bfc561fea20251eb42b9c2fb17348172ccbbf (patch)
treec1c1255eb071e5e79e8a89d7d15eb1ec885f0e16 /src/basic/util.c
parenttest-util: modernize (diff)
downloadsystemd-7f6bfc561fea20251eb42b9c2fb17348172ccbbf.tar.xz
systemd-7f6bfc561fea20251eb42b9c2fb17348172ccbbf.zip
basic/util: import memeqzero from casync
Diffstat (limited to 'src/basic/util.c')
-rw-r--r--src/basic/util.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/basic/util.c b/src/basic/util.c
index cd75529cfe..00052c5e1d 100644
--- a/src/basic/util.c
+++ b/src/basic/util.c
@@ -165,6 +165,28 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
return NULL;
}
+bool memeqzero(const void *data, size_t length) {
+ /* Does the buffer consist entirely of NULs?
+ * Copied from https://github.com/systemd/casync/, copied in turn from
+ * https://github.com/rustyrussell/ccan/blob/master/ccan/mem/mem.c#L92,
+ * which is licensed CC-0.
+ */
+
+ const uint8_t *p = data;
+ size_t i;
+
+ /* Check first 16 bytes manually */
+ for (i = 0; i < 16; i++, length--) {
+ if (length == 0)
+ return true;
+ if (p[i])
+ return false;
+ }
+
+ /* Now we know first 16 bytes are NUL, memcmp with self. */
+ return memcmp(data, p + i, length) == 0;
+}
+
int on_ac_power(void) {
bool found_offline = false, found_online = false;
_cleanup_closedir_ DIR *d = NULL;