diff options
| author | Andrew <saintruler@gmail.com> | 2021-08-26 16:33:26 +0400 |
|---|---|---|
| committer | Andrew <saintruler@gmail.com> | 2021-08-26 16:33:26 +0400 |
| commit | 80e3581af2ce0892bc14b4691f8eb0f07299e89f (patch) | |
| tree | c0f60729edc3cbb0359143fd8e946a95f869f5b5 /selfrestart.c | |
| parent | a7aea68aa1a58f0aa934878b7dc8f0a0a3b7a03d (diff) | |
Added new patches: movestack, selfrestart, resizecorners
Diffstat (limited to 'selfrestart.c')
| -rw-r--r-- | selfrestart.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/selfrestart.c b/selfrestart.c new file mode 100644 index 0000000..d695d48 --- /dev/null +++ b/selfrestart.c @@ -0,0 +1,65 @@ +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <stdio.h> +#include <stdlib.h> + +/** + * Magically finds the current's executable path + * + * I'm doing the do{}while(); trick because Linux (what I'm running) is not + * POSIX compilant and so lstat() cannot be trusted on /proc entries + * + * @return char* the path of the current executable + */ +char *get_dwm_path(){ + struct stat s; + int r, length, rate = 42; + char *path = NULL; + + if(lstat("/proc/self/exe", &s) == -1){ + perror("lstat:"); + return NULL; + } + + length = s.st_size + 1 - rate; + + do{ + length+=rate; + + free(path); + path = malloc(sizeof(char) * length); + + if(path == NULL){ + perror("malloc:"); + return NULL; + } + + r = readlink("/proc/self/exe", path, length); + + if(r == -1){ + perror("readlink:"); + return NULL; + } + }while(r >= length); + + path[r] = '\0'; + + return path; +} + +/** + * self-restart + * + * Initially inspired by: Yu-Jie Lin + * https://sites.google.com/site/yjlnotes/notes/dwm + */ +void self_restart(const Arg *arg) { + char *const argv[] = {get_dwm_path(), NULL}; + + if(argv[0] == NULL){ + return; + } + + execv(argv[0], argv); +} |