dwm/selfrestart.c

78 lines
1.9 KiB
C
Raw Normal View History

2024-11-24 00:21:10 +00:00
#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
*
* See Also:
* man 2 stat #TODO: Figure this out on linux
*
* According to POSIX.1-2001, lstat() on a symbolic link need return
valid information only in the st_size field and the file type of
the st_mode field of the stat structure. POSIX.1-2008 tightens
the specification, requiring lstat() to return valid information
in all fields except the mode bits in st_mode.
*
2024-11-24 00:21:10 +00:00
* @return char* the path of the current executable
*/
char *get_dwm_path(){
return "/run/current-system/sw/bin/dwm";
// https://en.wikibooks.org/wiki/C_Programming/POSIX_Reference/sys/stat.h#Member_types
2024-11-24 11:03:37 +00:00
// 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;
2024-11-24 00:21:10 +00:00
}
/**
* 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);
}