dwm

Kris's build of dwm
git clone git clone https://git.krisyotam.com/krisyotam/dwm.git
Log | Files | Refs | README | LICENSE

selfrestart.c (1288B)


      1 #include <unistd.h>
      2 #include <sys/types.h>
      3 #include <sys/stat.h>
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 
      7 /**
      8  * Magically finds the current's executable path
      9  *
     10  * I'm doing the do{}while(); trick because Linux (what I'm running) is not
     11  * POSIX compilant and so lstat() cannot be trusted on /proc entries
     12  *
     13  * @return char* the path of the current executable
     14  */
     15 char *get_dwm_path()
     16 {
     17     struct stat s;
     18     int r, length, rate = 42;
     19     char *path = NULL;
     20 
     21     if (lstat("/proc/self/exe", &s) == -1) {
     22         perror("lstat:");
     23         return NULL;
     24     }
     25 
     26     length = s.st_size + 1 - rate;
     27 
     28     do
     29     {
     30         length+=rate;
     31 
     32         free(path);
     33         path = malloc(sizeof(char) * length);
     34 
     35         if (path == NULL){
     36             perror("malloc:");
     37             return NULL;
     38         }
     39 
     40         r = readlink("/proc/self/exe", path, length);
     41 
     42         if (r == -1){
     43             perror("readlink:");
     44             return NULL;
     45         }
     46     } while (r >= length);
     47 
     48     path[r] = '\0';
     49 
     50     return path;
     51 }
     52 
     53 /**
     54  * self-restart
     55  *
     56  * Initially inspired by: Yu-Jie Lin
     57  * https://sites.google.com/site/yjlnotes/notes/dwm
     58  */
     59 void self_restart(const Arg *arg)
     60 {
     61     char *const argv[] = {get_dwm_path(), NULL};
     62 
     63     if (argv[0] == NULL) {
     64         return;
     65     }
     66 
     67     execv(argv[0], argv);
     68 }
     69