From 6bc697710abc84cebfc2a19986fc584f88da8e66 Mon Sep 17 00:00:00 2001 From: David Griffith Date: Wed, 15 Feb 2017 17:05:15 -0800 Subject: [PATCH] Fix #31: Several struct f_setup variables were not being set up at all. In the interest of making sure this still compiles and runs on super-old machinery, I opted to use a local implementation of strdup() instead of using what's in string.h. Later I'll address the possibility of strrchr() not being available. Code shamelessly ripped from http://stackoverflow.com/questions/37132549 --- src/dumb/dumb_init.c | 45 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/src/dumb/dumb_init.c b/src/dumb/dumb_init.c index 8f65399..4867166 100644 --- a/src/dumb/dumb_init.c +++ b/src/dumb/dumb_init.c @@ -72,6 +72,7 @@ static bool plain_ascii = FALSE; void os_process_arguments(int argc, char *argv[]) { int c; + char *p = NULL; do_more_prompts = TRUE; /* Parse the options */ @@ -114,17 +115,26 @@ void os_process_arguments(int argc, char *argv[]) printf("While running, enter \"\\help\" to list the runtime escape sequences\n\n"); exit(1); } -/* - if (((argc - zoptind) != 1) && ((argc - zoptind) != 2)) { - puts(usage); - exit(1); - } -*/ - f_setup.story_file = argv[zoptind++]; + + /* Create nice default file names */ + + f_setup.story_file = strdup(argv[zoptind++]); if (zoptind < argc) - graphics_filename = argv[zoptind++]; + graphics_filename = strdup(argv[zoptind++]); + + f_setup.story_name = strdup(basename(f_setup.story_file)); - f_setup.save_name = malloc(FILENAME_MAX); + /* Now strip off the extension */ + p = strrchr(f_setup.story_name, '.'); + *p = '\0'; /* extension removed */ + + f_setup.save_name = malloc(strlen(f_setup.story_name) * sizeof(char) + 5); + strncpy(f_setup.save_name, f_setup.story_name, strlen(f_setup.story_name)); + strncat(f_setup.save_name, EXT_SAVE, strlen(EXT_SAVE)); + + f_setup.script_name = malloc(strlen(f_setup.story_name) * sizeof(char) + 5); + strncpy(f_setup.script_name, f_setup.story_name, strlen(f_setup.story_name)); + strncat(f_setup.script_name, EXT_SCRIPT, strlen(EXT_SCRIPT)); } void os_init_screen(void) @@ -214,3 +224,20 @@ void os_init_setup(void) f_setup.restore_mode = 0; } + + +char *my_strdup(char *src) +{ + char *str; + char *p; + int len = 0; + + while (src[len]) + len++; + str = malloc(len + 1); + p = str; + while (*src) + *p++ = *src++; + *p = '\0'; + return str; +} -- 2.34.1