diff options
author | 2016-06-11 15:02:26 -0400 | |
---|---|---|
committer | 2016-06-11 15:02:26 -0400 | |
commit | 6388e1e288009fc12af3fe418a6faf8cfe9926f7 (patch) | |
tree | d1b4fb33848d09c71602005c57919a903ce3d72d /common/util.c | |
parent | Many improvements to man pages (diff) | |
parent | cleanup + add timeouts for pid_workspace list (diff) | |
download | sway-6388e1e288009fc12af3fe418a6faf8cfe9926f7.tar.gz sway-6388e1e288009fc12af3fe418a6faf8cfe9926f7.tar.zst sway-6388e1e288009fc12af3fe418a6faf8cfe9926f7.zip |
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 @@ | |||
1 | #include <math.h> | 1 | #include <math.h> |
2 | 2 | #include <stdio.h> | |
3 | #include <string.h> | ||
4 | #include <stdlib.h> | ||
5 | #include "readline.h" | ||
3 | #include "util.h" | 6 | #include "util.h" |
7 | #include "log.h" | ||
4 | 8 | ||
5 | int wrap(int i, int max) { | 9 | int wrap(int i, int max) { |
6 | return ((i % max) + max) % max; | 10 | return ((i % max) + max) % max; |
@@ -64,3 +68,28 @@ int get_modifier_names(const char **names, uint32_t modifier_masks) { | |||
64 | 68 | ||
65 | return length; | 69 | return length; |
66 | } | 70 | } |
71 | |||
72 | pid_t get_parent_pid(pid_t child) { | ||
73 | pid_t parent; | ||
74 | char file_name[100]; | ||
75 | char *buffer = NULL; | ||
76 | char *token = NULL; | ||
77 | const char *sep = " "; | ||
78 | FILE *stat = NULL; | ||
79 | |||
80 | sprintf(file_name, "/proc/%d/stat", child); | ||
81 | |||
82 | if ((stat = fopen(file_name, "r")) && (buffer = read_line(stat))) { | ||
83 | fclose(stat); | ||
84 | |||
85 | token = strtok(buffer, sep); // pid | ||
86 | token = strtok(NULL, sep); // executable name | ||
87 | token = strtok(NULL, sep); // state | ||
88 | token = strtok(NULL, sep); // parent pid | ||
89 | |||
90 | parent = strtol(token, NULL, 10); | ||
91 | return (parent == child) ? -1 : parent; | ||
92 | } | ||
93 | |||
94 | return -1; | ||
95 | } | ||