X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fprocess_runs.c;h=b5e8f110c5ac2cd77e9be07ceea7ce2ecbe39c42;hb=6a19709e65c6aac2085962e989f2429c539c5af5;hp=88979f0f72b426b33194adb357cae57e29f4d76e;hpb=cf091024335052e4774d44179e80e1948da8610f;p=i3%2Fi3status diff --git a/src/process_runs.c b/src/process_runs.c index 88979f0..b5e8f11 100644 --- a/src/process_runs.c +++ b/src/process_runs.c @@ -1,4 +1,4 @@ -// vim:ts=8:expandtab +// vim:ts=4:sw=4:expandtab #include #include #include @@ -15,19 +15,35 @@ * anything). kill() will return ESRCH if the process does not exist and 0 or * EPERM (depending on the uid) if it exists. * + * If multiple files match the glob pattern, all of them will be checked until + * the first running process is found. + * */ bool process_runs(const char *path) { - static char pidbuf[16]; - static glob_t globbuf; - memset(pidbuf, 0, sizeof(pidbuf)); + static char pidbuf[16]; + static glob_t globbuf; + memset(pidbuf, 0, sizeof(pidbuf)); - if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) - die("glob() failed\n"); - if (!slurp((globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path), pidbuf, sizeof(pidbuf))) { - globfree(&globbuf); - return false; - } + if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) + die("glob() failed\n"); + if (globbuf.gl_pathc == 0) { + /* No glob matches, the specified path does not contain a wildcard. */ globfree(&globbuf); - + if (!slurp(path, pidbuf, sizeof(pidbuf))) + return false; return (kill(strtol(pidbuf, NULL, 10), 0) == 0 || errno == EPERM); + } + for (size_t i = 0; i < globbuf.gl_pathc; i++) { + if (!slurp(globbuf.gl_pathv[i], pidbuf, sizeof(pidbuf))) { + globfree(&globbuf); + return false; + } + if (kill(strtol(pidbuf, NULL, 10), 0) == 0 || errno == EPERM) { + globfree(&globbuf); + return true; + } + } + globfree(&globbuf); + + return false; }