+/*
+ * Reads /proc/<pid>/stat and returns (via pointers) the name and parent pid of
+ * the specified pid.
+ * When false is returned, parsing failed and the contents of outname and
+ * outpid are undefined.
+ *
+ */
+static bool parse_proc_stat(pid_t pid, char **outname, pid_t *outppid) {
+ char path[255];
+ /* the relevant contents (for us) are:
+ * <pid> (<program name>) <status> <ppid>
+ * which should well fit into one page of 4096 bytes */
+ char buffer[4096];
+
+ if (snprintf(path, sizeof(path), "/proc/%d/stat", pid) == -1 ||
+ !slurp(path, buffer, sizeof(buffer)))
+ return false;
+
+ char *leftbracket = strchr(buffer, '(');
+ char *rightbracket = strrchr(buffer, ')');
+ if (!leftbracket ||
+ !rightbracket ||
+ sscanf(rightbracket + 2, "%*c %d", outppid) != 1)
+ return false;
+ *rightbracket = '\0';
+ *outname = strdup(leftbracket + 1);
+ return true;
+}
+
+static char *format_for_process(const char *name) {
+ if (strcasecmp(name, "i3bar") == 0)
+ return "i3bar";
+ else if (strcasecmp(name, "dzen2") == 0)
+ return "dzen2";
+ else if (strcasecmp(name, "xmobar") == 0)
+ return "xmobar";
+ else
+ return NULL;
+}
+