Fix COMSPEC path handling when launching DOS programs from another directory
authorRobert Pengelly <robertapengelly@hotmail.com>
Mon, 19 Oct 2026 03:19:18 +0000 (04:19 +0100)
committerRobert Pengelly <robertapengelly@hotmail.com>
Mon, 19 Oct 2026 03:19:18 +0000 (04:19 +0100)
interrupts.c
interrupts.h
main.c

index 44793045255895e30a2648128b6eabc4efa66d02..86db1ae5a332f9f145227058339efbf9be52c40e 100644 (file)
@@ -1442,6 +1442,29 @@ static void dos_normalize_path (char *path) {
 
 static char dos_drive_root[260] = ".";
 
+static char dos_initial_host_path[520];
+static char dos_initial_guest_path[260];
+
+const char *dos_register_initial_program (const char *path) {
+
+    const char *base = path;
+    const char *p;
+    
+    for (p = path; *p; p++) {
+    
+        if (*p == '/' || *p == '\\') {
+            base = p + 1;
+        }
+    
+    }
+    
+    snprintf (dos_initial_host_path, sizeof (dos_initial_host_path), "%s", path);
+    snprintf (dos_initial_guest_path, sizeof (dos_initial_guest_path), "C:\\%s", base);
+    
+    return dos_initial_guest_path;
+
+}
+
 static int dos_is_drive_root_path (const char *path) {
 
     size_t len = strlen (path);
@@ -1519,6 +1542,13 @@ static void dos_resolve_path (const char *in, char *out, size_t out_sz) {
         return;
     }
     
+    if (in && dos_initial_guest_path[0] && strcasecmp (in, dos_initial_guest_path) == 0) {
+    
+        snprintf (out, out_sz, "%s", dos_initial_host_path);
+        return;
+    
+    }
+    
     tmp[0] = '\0';
     dos_append_bounded (tmp, sizeof (tmp), in ? in : "");
     
index 082e29b6e2806dedeaddbdff9480092d049c2418..03fca6449c6a52e7f58e7ad9feb4ef23d8ccda8b 100644 (file)
@@ -12,4 +12,6 @@ void init_ivt (CPU8086 *cpu);
 void handle_interrupt (CPU8086 *cpu, uint8_t interrupt_number);
 void native_interrupt (CPU8086 *cpu, uint8_t interrupt_number);
 
+const char *dos_register_initial_program (const char *path);
+
 #endif      /* _INTERRUPTS_H */
diff --git a/main.c b/main.c
index 57b6ce84cc8af0ebec132e4badbb58a6faabe1b7..b806350cdd0fb1ad50f7da46682bcde263fc4e33 100644 (file)
--- a/main.c
+++ b/main.c
@@ -115,6 +115,8 @@ int main (int argc, char **argv) {
     CPU8086 cpu;
     
     const char *path = NULL;
+    const char *dos_path;
+    
     char cmd_tail[128] = {0};
     
     int trace = 0, rc;
@@ -169,11 +171,12 @@ int main (int argc, char **argv) {
     }
     
     cpu_reset (&cpu, mem, MEM_SIZE);
+    dos_path = dos_register_initial_program (path);
     
     if (load_mz_file (path, &cpu, EXE_LOAD_SEG << 4, (uint16_t) ((MEM_SIZE - ((uint32_t) (EXE_LOAD_SEG << 4) << 4)) / 16))) {
-        init_psp (path, &cpu, cmd_tail, EXE_LOAD_SEG);
+        init_psp (dos_path, &cpu, cmd_tail, EXE_LOAD_SEG);
     } else if (load_com_file (path, &cpu, mem, MEM_SIZE, EXE_LOAD_SEG, &loaded)) {
-        init_psp (path, &cpu, cmd_tail, EXE_LOAD_SEG);
+        init_psp (dos_path, &cpu, cmd_tail, EXE_LOAD_SEG);
     } else {
     
         fprintf (stderr, "Failed to load %s\n", path);