From 13ff9af7e13bb23b720cf4541fb2e3cad0af9ac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laci=20b=C3=A1?= Date: Thu, 26 Aug 2021 20:30:37 +0200 Subject: [PATCH 1/4] Add support for overridable VM name - A global variable added as vm_name - This variable can be filled with the `--vmname "Name"` or `-V "Name"` parameter. - If there are no such a parameter definied this variable will filled up with the directory name. - The Discord module displays this global variable, as VM name. - Various 86Box managers can use this feature to display fancy VM names, instead of GUID folder names. - This variable can be easily used later for adding cool things, like the VM name in title bar, etc. --- src/86box.c | 21 +++++++++++++++++++++ src/include/86box/86box.h | 1 + src/win/win_discord.c | 8 +++----- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/86box.c b/src/86box.c index 0a5c59258..7611732b6 100644 --- a/src/86box.c +++ b/src/86box.c @@ -110,6 +110,7 @@ uint64_t unique_id = 0; uint64_t source_hwnd = 0; #endif char log_path[1024] = { '\0'}; /* (O) full path of logfile */ +char vm_name[1024] = { '\0'}; /* (O) display name of the VM */ /* Configuration values. */ int window_w; /* (C) window size and */ @@ -417,6 +418,7 @@ usage: printf("-F or --fullscreen - start in fullscreen mode\n"); printf("-L or --logfile path - set 'path' to be the logfile\n"); printf("-P or --vmpath path - set 'path' to be root for vm\n"); + printf("-V or --vmname name - overrides the name of the running VM.\n"); printf("-S or --settings - show only the settings dialog\n"); printf("-N or --noconfirm - do not ask for confirmation on quit\n"); #ifdef _WIN32 @@ -446,6 +448,11 @@ usage: if ((c+1) == argc) goto usage; strcpy(path, argv[++c]); + } else if (!strcasecmp(argv[c], "--vmname") || + !strcasecmp(argv[c], "-V")) { + if ((c+1) == argc) goto usage; + + strcpy(vm_name, argv[++c]); } else if (!strcasecmp(argv[c], "--settings") || !strcasecmp(argv[c], "-S")) { settings_only = 1; @@ -551,6 +558,20 @@ usage: /* At this point, we can safely create the full path name. */ plat_append_filename(cfg_path, usr_path, p); + /* + * Get the current directory's name + * + * At this point usr_path is perfectly initialized. + * If no --vmname parameter specified we'll use the + * working directory name as the VM's name. + */ + if (strlen(vm_name) == 0) + { + plat_get_dirname(vm_name, usr_path); + p = plat_get_filename(vm_name); + strcpy(vm_name, p); + } + /* * This is where we start outputting to the log file, * if there is one. Create a little info header first. diff --git a/src/include/86box/86box.h b/src/include/86box/86box.h index 5879a632e..20738ab6b 100644 --- a/src/include/86box/86box.h +++ b/src/include/86box/86box.h @@ -73,6 +73,7 @@ extern uint64_t unique_id; extern uint64_t source_hwnd; #endif extern char log_path[1024]; /* (O) full path of logfile */ +extern char vm_name[1024]; /* (O) display name of the VM */ extern int window_w, window_h, /* (C) window size and */ diff --git a/src/win/win_discord.c b/src/win/win_discord.c index 73d15855d..61b300341 100644 --- a/src/win/win_discord.c +++ b/src/win/win_discord.c @@ -68,7 +68,7 @@ void discord_update_activity(int paused) { struct DiscordActivity activity; - char config_name[1024], cpufamily[1024]; + char cpufamily[1024]; char *paren; if(discord_activities == NULL) @@ -78,16 +78,14 @@ discord_update_activity(int paused) memset(&activity, 0x00, sizeof(activity)); - plat_get_dirname(config_name, usr_path); - strncpy(cpufamily, cpu_f->name, sizeof(cpufamily) - 1); paren = strchr(cpufamily, '('); if (paren) *(paren - 1) = '\0'; - if (strlen(plat_get_filename(config_name)) < 100) + if (strlen(vm_name) < 100) { - sprintf_s(activity.details, sizeof(activity.details), "Running \"%s\"", plat_get_filename(config_name)); + sprintf_s(activity.details, sizeof(activity.details), "Running \"%s\"", vm_name); sprintf_s(activity.state, sizeof(activity.state), "%s (%s/%s)", strchr(machine_getname(), ']') + 2, cpufamily, cpu_s->name); } else From 2634909dbd77979ed28db53145b802baf00b130a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laci=20b=C3=A1?= Date: Thu, 26 Aug 2021 21:49:25 +0200 Subject: [PATCH 2/4] Fix to avoid undefined behavior at strcpy - Using the `path` buffer to store the directory name, the possible overlapping areas can be avoided at strcpy. - The `path` buffer is unused in further parts of this function, so can be used freely here. The `temp` buffer is too small for this task. --- src/86box.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/86box.c b/src/86box.c index 7611732b6..3f5e897e0 100644 --- a/src/86box.c +++ b/src/86box.c @@ -564,11 +564,14 @@ usage: * At this point usr_path is perfectly initialized. * If no --vmname parameter specified we'll use the * working directory name as the VM's name. + * The directory name will stored in local buffer + * named `path` since it's unused in further parts + * of this function. */ if (strlen(vm_name) == 0) { - plat_get_dirname(vm_name, usr_path); - p = plat_get_filename(vm_name); + plat_get_dirname(path, usr_path); + p = plat_get_filename(path); strcpy(vm_name, p); } From 1980155e34a75d1ca70c4b28104d9e10e7931a63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laci=20b=C3=A1?= Date: Fri, 27 Aug 2021 09:02:31 +0200 Subject: [PATCH 3/4] Fix to avoid repurposing variables --- src/86box.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/86box.c b/src/86box.c index 3f5e897e0..155c5925a 100644 --- a/src/86box.c +++ b/src/86box.c @@ -564,15 +564,12 @@ usage: * At this point usr_path is perfectly initialized. * If no --vmname parameter specified we'll use the * working directory name as the VM's name. - * The directory name will stored in local buffer - * named `path` since it's unused in further parts - * of this function. */ if (strlen(vm_name) == 0) { - plat_get_dirname(path, usr_path); - p = plat_get_filename(path); - strcpy(vm_name, p); + char ltemp[1024] = {'/0'}; + plat_get_dirname(ltemp, usr_path); + strcpy(vm_name, plat_get_filename(ltemp)); } /* From 3da83e3ffee0efc83676bd8410a48924a4344402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laci=20b=C3=A1?= Date: Fri, 27 Aug 2021 09:09:46 +0200 Subject: [PATCH 4/4] Fix a typo in initialization of `ltemp` --- src/86box.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/86box.c b/src/86box.c index 155c5925a..452e9b448 100644 --- a/src/86box.c +++ b/src/86box.c @@ -567,7 +567,7 @@ usage: */ if (strlen(vm_name) == 0) { - char ltemp[1024] = {'/0'}; + char ltemp[1024] = { '\0'}; plat_get_dirname(ltemp, usr_path); strcpy(vm_name, plat_get_filename(ltemp)); }