Created
August 26, 2020 21:50
-
-
Save seaneshbaugh/d7e9f357302547d2852f7822bfa6ecc8 to your computer and use it in GitHub Desktop.
restart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _DARWIN_BETTER_REALPATH | |
#include <dlfcn.h> | |
#include <limits.h> | |
#include <mach-o/dyld.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
int main(int argc, char** argv) { | |
char response[256]; | |
printf("Press y to restart\n"); | |
if (fgets(response, sizeof(response), stdin)) { | |
if (response[0] == 'y' || response[0] == 'Y') { | |
pid_t pid = fork(); | |
if (pid == 0) { | |
char path[PATH_MAX]; | |
uint32_t size = (uint32_t)sizeof(path); | |
if (_NSGetExecutablePath(path, &size) == 0) { | |
printf("path = %s\n", path); | |
char resolved[PATH_MAX]; | |
realpath(path, resolved); | |
printf("resolved = %s\n", resolved); | |
char* args[] = { realpath, NULL }; | |
execvp(resolved, args); | |
return 0; | |
} else { | |
fprintf(stderr, "Error getting executable path.\n"); | |
return 1; | |
} | |
} else { | |
printf("Exiting parent.\n"); | |
return 0; | |
} | |
} else { | |
printf("Exiting\n"); | |
} | |
} else { | |
fprintf(stderr, "Error reading from stdin\n."); | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment