aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/dwm/util.c
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2020-06-06 19:56:41 -0700
committerNicholas Noll <nbnoll@eml.cc>2020-06-06 19:56:41 -0700
commit4e0318919dd726f485ea3a495ff0989f69f35630 (patch)
treeff22628bcaa163881afad87fc2501d5dffa43be2 /sys/cmd/dwm/util.c
parent186fe25ab798fe559be242dbe2eaff8e553c4e06 (diff)
hand added swallow patch
Diffstat (limited to 'sys/cmd/dwm/util.c')
-rw-r--r--sys/cmd/dwm/util.c41
1 files changed, 37 insertions, 4 deletions
diff --git a/sys/cmd/dwm/util.c b/sys/cmd/dwm/util.c
index 1306c9c..445d479 100644
--- a/sys/cmd/dwm/util.c
+++ b/sys/cmd/dwm/util.c
@@ -1,8 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include "dwm.h"
void
fatal(char *fmt, ...) {
@@ -31,3 +28,39 @@ ecalloc(size_t nmemb, size_t size)
fatal("calloc:");
return p;
}
+
+pid_t
+getparentprocess(pid_t p)
+{
+ uint v = 0;
+
+#if defined(__linux__)
+ Stream *f;
+ char buf[256];
+ snprintf(buf, sizeof(buf) - 1, "/proc/%u/stat", (unsigned)p);
+
+ if (!(f = fopen(buf, "r")))
+ return (pid_t)0;
+
+ if (fscanf(f, "%*u %*s %*c %u", (unsigned *)&v) != 1)
+ v = (pid_t)0;
+ fclose(f);
+#elif defined(__FreeBSD__)
+ struct kinfo_proc *proc = kinfo_getproc(p);
+ if (!proc)
+ return (pid_t)0;
+
+ v = proc->ki_ppid;
+ free(proc);
+#endif
+ return (pid_t)v;
+}
+
+int
+isdescendent(pid_t p, pid_t c)
+{
+ while (p != c && c != 0)
+ c = getparentprocess(c);
+
+ return (int)c;
+}