Fix #31: Several struct f_setup variables were not being set up at all.
authorDavid Griffith <dave@661.org>
Thu, 16 Feb 2017 01:05:15 +0000 (17:05 -0800)
committerDavid Griffith <dave@661.org>
Thu, 16 Feb 2017 01:05:15 +0000 (17:05 -0800)
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

index 8f65399989ebccecdf8618d44089177f5d8de020..4867166a2c1e08da6666fde725b9833894a189ca 100644 (file)
@@ -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;
+}