Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.3 KiB

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