Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chdir to exe path #345

Merged
merged 8 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/harness/harness.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,17 @@ void Harness_Init(int* argc, char* argv[]) {
}

char* root_dir = getenv("DETHRACE_ROOT_DIR");
if (root_dir == NULL) {
LOG_INFO("DETHRACE_ROOT_DIR is not set, assuming '.'");
if (root_dir != NULL) {
LOG_INFO("DETHRACE_ROOT_DIR is set to '%s'", root_dir);
} else {
printf("Data directory: %s\n", root_dir);
result = chdir(root_dir);
if (result != 0) {
LOG_PANIC("Failed to chdir. Error is %s", strerror(errno));
}
root_dir = OS_Dirname(argv[0]);
}
printf("Using root directory: %s\n", root_dir);
result = chdir(root_dir);
if (result != 0) {
LOG_PANIC("Failed to chdir. Error is %s", strerror(errno));
}

if (harness_game_info.mode == eGame_none) {
Harness_DetectGameMode();
}
Expand Down
4 changes: 4 additions & 0 deletions src/harness/include/harness/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ FILE* OS_fopen(const char* pathname, const char* mode);

size_t OS_ConsoleReadPassword(char* pBuffer, size_t pBufferLen);

char* OS_Dirname(const char* path);

char* OS_Basename(const char* path);

#endif
18 changes: 15 additions & 3 deletions src/harness/os/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
#include <unistd.h>

#define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
#define MAX_STACK_FRAMES 64
#define TRACER_PID_STRING "TracerPid:"

static int stack_nbr = 0;
static char _program_name[1024];
#define MAX_STACK_FRAMES 64

static void* stack_traces[MAX_STACK_FRAMES];
#define TRACER_PID_STRING "TracerPid:"
static char name_buf[4096];

struct dl_iterate_callback_data {
int initialized;
Expand Down Expand Up @@ -176,7 +178,7 @@ static void signal_handler(int sig, siginfo_t* siginfo, void* context) {
void resolve_full_path(char* path, const char* argv0) {
if (argv0[0] == '/') { // run with absolute path
strcpy(path, argv0);
} else { // run with relative path
} else { // run with relative path
if (NULL == getcwd(path, PATH_MAX)) {
perror("getcwd error");
return;
Expand Down Expand Up @@ -301,3 +303,13 @@ size_t OS_ConsoleReadPassword(char* pBuffer, size_t pBufferLen) {
tcsetattr(STDIN_FILENO, TCSANOW, &old);
return len;
}

char* OS_Dirname(const char* path) {
strcpy(name_buf, path);
return dirname(name_buf);
}

char* OS_Basename(const char* path) {
strcpy(name_buf, path);
return basename(name_buf);
}
13 changes: 12 additions & 1 deletion src/harness/os/macos.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ static int stack_nbr = 0;
static char _program_name[1024];
#define MAX_STACK_FRAMES 64
static void* stack_traces[MAX_STACK_FRAMES];
static char name_buf[4096];

// Resolve symbol name and source location given the path to the executable and an address
int addr2line(char const* const program_name, void const* const addr) {
Expand Down Expand Up @@ -150,7 +151,7 @@ static uint8_t alternate_stack[SIGSTKSZ];
void resolve_full_path(char* path, const char* argv0) {
if (argv0[0] == '/') { // run with absolute path
strcpy(path, argv0);
} else { // run with relative path
} else { // run with relative path
if (NULL == getcwd(path, PATH_MAX)) {
perror("getcwd error");
return;
Expand Down Expand Up @@ -217,3 +218,13 @@ size_t OS_ConsoleReadPassword(char* pBuffer, size_t pBufferLen) {
fgets(pBuffer, pBufferLen, stdin);
return strlen(pBuffer);
}

char* OS_Dirname(const char* path) {
strcpy(name_buf, path);
return dirname(name_buf);
}

char* OS_Basename(const char* path) {
strcpy(name_buf, path);
return basename(name_buf);
}
13 changes: 13 additions & 0 deletions src/harness/os/windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ void dr_dprintf(char* fmt_string, ...);
static int stack_nbr = 0;
static char _program_name[1024];

static char dirname_buf[_MAX_DIR];
static char fname_buf[_MAX_FNAME];

int addr2line(char const* const program_name, void const* const addr) {
char addr2line_cmd[512] = { 0 };

Expand Down Expand Up @@ -169,3 +172,13 @@ size_t OS_ConsoleReadPassword(char* pBuffer, size_t pBufferLen) {
fgets(pBuffer, pBufferLen, stdin);
return strlen(pBuffer);
}

char* OS_Dirname(const char* path) {
_splitpath(path, NULL, dirname_buf, NULL, NULL);
return dirname_buf;
}

char* OS_Basename(const char* path) {
_splitpath(path, NULL, NULL, fname_buf, NULL);
return fname_buf;
}
6 changes: 0 additions & 6 deletions src/harness/renderers/gl/gl_renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,6 @@ void GLRenderer_Init(int pRender_width, int pRender_height) {
LOG_INFO("OpenGL version string: %s", glGetString(GL_VERSION));
LOG_INFO("OpenGL shading language version string: %s", glGetString(GL_SHADING_LANGUAGE_VERSION));

int maxTextureImageUnits;
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureImageUnits);
if (maxTextureImageUnits < 3) {
LOG_PANIC("GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS is %d. Need at least 3", maxTextureImageUnits);
}

LoadShaders();
SetupFullScreenRectGeometry();

Expand Down
12 changes: 6 additions & 6 deletions src/harness/win95/polyfill.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "harness/hooks.h"
#include "harness/os.h"
#include "harness/win95_polyfill.h"

#include <assert.h>
Expand Down Expand Up @@ -237,12 +238,11 @@ void DirectDrawDevice_SetPaletteEntries(PALETTEENTRY_* palette, int pFirst_colou
}

void _splitpath_(char* path, char* drive, char* dir, char* fname, char* ext) {
#ifdef _WIN32
_splitpath(path, NULL, NULL, fname, NULL);
#else
char* base = basename(path);
strcpy(fname, base);
#endif
assert(dir == NULL);
assert(fname != NULL);

char* result = OS_Basename(path);
strcpy(fname, result);
}

int _CrtDbgReport_(int reportType, const char* filename, int linenumber, const char* moduleName, const char* format, ...) {
Expand Down
Loading