minecraft-pi-reborn/libreborn/include/libreborn/exec.h

64 lines
1.7 KiB
C
Raw Normal View History

2021-06-17 21:32:24 +00:00
#pragma once
#include <unistd.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include "log.h"
#include "string.h"
#include "util.h"
// Safe execvpe()
__attribute__((noreturn)) static inline void safe_execvpe(const char *pathname, char *argv[], char *const envp[]) {
argv[0] = (char *) pathname;
int ret = execvpe(pathname, argv, envp);
if (ret == -1) {
ERR("Unable To Execute Program: %s: %s", pathname, strerror(errno));
} else {
ERR("%s", "Unknown execvpe() Error");
}
}
2022-03-09 23:47:31 +00:00
// Chop Off Last Component
static inline void chop_last_component(char **str) {
size_t length = strlen(*str);
for (size_t i = 0; i < length; i++) {
size_t j = length - i - 1;
if ((*str)[j] == '/') {
(*str)[j] = '\0';
break;
}
}
}
2021-06-17 21:32:24 +00:00
// Get Binary Directory (Remember To Free)
static inline char *get_binary_directory() {
2021-08-16 03:11:03 +00:00
// Get Path To Current Executable
char *exe = realpath("/proc/self/exe", NULL);
2021-06-17 21:32:24 +00:00
ALLOC_CHECK(exe);
// Chop Off Last Component
2022-03-09 23:47:31 +00:00
chop_last_component(&exe);
2021-06-17 21:32:24 +00:00
// Return
return exe;
}
2022-03-09 23:47:31 +00:00
2021-06-17 21:32:24 +00:00
// Safe execvpe() Relative To Binary
__attribute__((noreturn)) static inline void safe_execvpe_relative_to_binary(const char *pathname, char *argv[], char *const envp[]) {
// Get Binary Directory
char *binary_directory = get_binary_directory();
// Create Full Path
char *full_path = NULL;
safe_asprintf(&full_path, "%s/%s", binary_directory, pathname);
// Free Binary Directory
free(binary_directory);
// Run
safe_execvpe(full_path, argv, envp);
}
2022-03-09 23:47:31 +00:00
// Get MCPI Directory
static inline char *get_mcpi_directory() {
return getenv("MCPI_DIRECTORY");
}