From d57b06fbb2e931dab1b8b8669b43eee815a5db49 Mon Sep 17 00:00:00 2001 From: Timo Korvola Date: Thu, 1 Mar 2018 22:58:13 +0200 Subject: [PATCH] Fix dirname / basename problems. It is annoying how vaguely POSIX dirname and basename are defined, so make our own versions with well-defined behaviour. Now actually loads a story and starts. --- src/sdl/sf_util.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/sdl/sf_util.c b/src/sdl/sf_util.c index a8f32b0..162d3ef 100644 --- a/src/sdl/sf_util.c +++ b/src/sdl/sf_util.c @@ -3,6 +3,8 @@ #include #include +#include + #include #ifdef __WIN32__ @@ -307,6 +309,28 @@ static void parse_options (int argc, char **argv) * */ +/** + * Like dirname except well defined. + * Does not modify path. Always returns a new string (caller must free). + */ +static char *new_dirname(const char *path) +{ + char *p = strdup(path), *p2 = strdup(dirname(p)); + free(p); + return p2; +} + +/** + * Like basename except well defined. + * Does not modify path. Always returns a new string (caller must free). + */ +static char *new_basename(const char *path) +{ + char *p = strdup(path), *p2 = strdup(basename(p)); + free(p); + return p2; +} + void os_process_arguments (int argc, char *argv[]) { char *p; @@ -336,7 +360,7 @@ void os_process_arguments (int argc, char *argv[]) /* Strip path and extension off the story file name */ - f_setup.story_name = strdup(basename(argv[optind])); + f_setup.story_name = new_basename(argv[optind]); /* Now strip off the extension. */ p = strrchr(f_setup.story_name, '.'); @@ -359,8 +383,7 @@ void os_process_arguments (int argc, char *argv[]) *p = '\0'; /* extension removed */ } } - - f_setup.story_path = strdup(dirname(argv[optind])); + f_setup.story_path = new_dirname(argv[optind]); /* Create nice default file names */ -- 2.34.1