summaryrefslogtreecommitdiff
path: root/patch/selfrestart.c
diff options
context:
space:
mode:
authorBear <bear@bengtsson.win>2021-12-27 09:29:58 +0000
committerBear <bear@bengtsson.win>2021-12-27 09:29:58 +0000
commit69262b01ced79c2d776fab9b889926d1816a1e7a (patch)
treef304cd6fa8734e83a7772d07dc9b484781565155 /patch/selfrestart.c
Added DWM
Diffstat (limited to 'patch/selfrestart.c')
-rw-r--r--patch/selfrestart.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/patch/selfrestart.c b/patch/selfrestart.c
new file mode 100644
index 0000000..f733867
--- /dev/null
+++ b/patch/selfrestart.c
@@ -0,0 +1,69 @@
+#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);
+}
+