diff options
author | Drew DeVault <sir@cmpwn.com> | 2016-06-11 15:02:26 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-06-11 15:02:26 -0400 |
commit | 6388e1e288009fc12af3fe418a6faf8cfe9926f7 (patch) | |
tree | d1b4fb33848d09c71602005c57919a903ce3d72d /common/util.c | |
parent | cb0cc32265136522f7bfbb768f55f35718248d71 (diff) | |
parent | 2298143d09ce8810d9772f95e1cb605fb6b08536 (diff) |
Merge pull request #701 from zandrmartin/assign-command
messy start of a fix for #462
Diffstat (limited to 'common/util.c')
-rw-r--r-- | common/util.c | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/common/util.c b/common/util.c index 13397437..12cb7470 100644 --- a/common/util.c +++ b/common/util.c @@ -1,6 +1,10 @@ #include <math.h> - +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "readline.h" #include "util.h" +#include "log.h" int wrap(int i, int max) { return ((i % max) + max) % max; @@ -64,3 +68,28 @@ int get_modifier_names(const char **names, uint32_t modifier_masks) { return length; } + +pid_t get_parent_pid(pid_t child) { + pid_t parent; + char file_name[100]; + char *buffer = NULL; + char *token = NULL; + const char *sep = " "; + FILE *stat = NULL; + + sprintf(file_name, "/proc/%d/stat", child); + + if ((stat = fopen(file_name, "r")) && (buffer = read_line(stat))) { + fclose(stat); + + token = strtok(buffer, sep); // pid + token = strtok(NULL, sep); // executable name + token = strtok(NULL, sep); // state + token = strtok(NULL, sep); // parent pid + + parent = strtol(token, NULL, 10); + return (parent == child) ? -1 : parent; + } + + return -1; +} |