Skip to content

Instantly share code, notes, and snippets.

@W4RH4WK
Created July 19, 2018 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save W4RH4WK/cd1d2442e4be6fd1036f3bde3189977e to your computer and use it in GitHub Desktop.
Save W4RH4WK/cd1d2442e4be6fd1036f3bde3189977e to your computer and use it in GitHub Desktop.
#include <libgen.h>
#include <linux/limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static const char MARKER_FILE[] = "build.ninja";
static bool exe_in_build_dir(void)
{
char exe_path[PATH_MAX + 1];
ssize_t n = readlink("/proc/self/exe", exe_path, PATH_MAX);
if (n == -1) {
return false;
}
exe_path[n] = '\0';
char path[PATH_MAX + sizeof(MARKER_FILE) + 1];
snprintf(path, sizeof(path), "%s/%s", dirname(exe_path), MARKER_FILE);
return access(path, F_OK) == 0;
}
const char* resource_path(void)
{
if (exe_in_build_dir()) {
return "@BUILD_DIR@";
} else {
return "@INSTALL_DIR@";
}
}
#ifndef RESOURCE_PATH_H
#define RESOURCE_PATH_H
const char* resource_path(void);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment