Skip to content

Commit

Permalink
Remove double quotes, escape spaces instead
Browse files Browse the repository at this point in the history
Use the linux standard of backslash to escape any spaces, instead of enclosing everything in quotes.
Note, this requires the latest Amiberry version (2024-08-24 or newer) to work properly!
  • Loading branch information
midwan committed Aug 24, 2024
1 parent 4c1d183 commit c01c391
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
32 changes: 23 additions & 9 deletions src/host-multiview.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
#include <stdlib.h>
#include "uae_pragmas.h"

static const char __ver[40] = "$VER: Host-MultiView v1.1 (2024-08-23)";
static const char __ver[40] = "$VER: Host-MultiView v1.2 (2024-08-24)";

int print_usage()
{
printf("Host-MultiView v1.2\n");
printf("Host-MultiView is a command line tool to open files with the host default handler, from within UAE.\n");
printf("%s\nUsage: host-multiview <filename>\n", __ver);
return 0;
}
Expand All @@ -15,9 +17,11 @@ int main(int argc, char *argv[])
{
BPTR lock;
char command[100];
char filename[4095];
char filename[256];
char escaped_filename[512];
memset(command, '\0', 100);
memset(filename, '\0', 4095);
memset(filename, '\0', 256);
memset(escaped_filename, '\0', 512);

if (!InitUAEResource())
{
Expand Down Expand Up @@ -45,18 +49,28 @@ int main(int argc, char *argv[])
{
if (lock = Lock(argv[i], ACCESS_READ))
{
NativeDosOp((ULONG)0, (ULONG)lock, (ULONG)filename, (ULONG)100);
NativeDosOp((ULONG)0, (ULONG)lock, (ULONG)filename, (ULONG)256);
UnLock(lock);
strncat(command, "\"", sizeof(command) - strlen(command) - 1);

// Escape spaces in filename
int j = 0;
for (int k = 0; k < strlen(filename); k++)
{
if (filename[k] == ' ')
{
escaped_filename[j++] = '\\';
}
escaped_filename[j++] = filename[k];
}
escaped_filename[j] = '\0';

strncat(command, filename, sizeof(command) - strlen(command) - 1);
strncat(command, "\"", sizeof(command) - strlen(command) - 1);
memset(filename, '\0', 4095);
memset(filename, '\0', 256);
memset(escaped_filename, '\0', 512);
}
else
{
strncat(command, "\"", sizeof(command) - strlen(command) - 1);
strncat(command, argv[i], sizeof(command) - strlen(command) - 1);
strncat(command, "\"", sizeof(command) - strlen(command) - 1);
}

if (i != argc - 1)
Expand Down
40 changes: 27 additions & 13 deletions src/host-run.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@
#include <stdlib.h>
#include "uae_pragmas.h"

static const char __ver[40] = "$VER: Host-Run v1.6 (2024-08-23)";
static const char __ver[40] = "$VER: Host-Run v1.7 (2024-08-24)";

int print_usage()
{
printf("Host-Run v1.7\n");
printf("Host-Run is a command line tool to run host commands from within UAE.\n");
printf("%s\nUsage: host-run <command> <argument1> <argument2> ...\n", __ver);
return 0;
}

int main(int argc, char *argv[])
{
BPTR lock;
char command[2048];
char filename[100];
memset(command, '\0', 2048);
memset(filename, '\0', 100);
char command[4096];
char filename[256];
char escaped_filename[512];
memset(command, '\0', 4096);
memset(filename, '\0', 256);
memset(escaped_filename, '\0', 512);

if (!InitUAEResource())
{
Expand Down Expand Up @@ -45,18 +49,28 @@ int main(int argc, char *argv[])
{
if (lock = Lock(argv[i], ACCESS_READ))
{
NativeDosOp((ULONG)0, (ULONG)lock, (ULONG)filename, (ULONG)100);
NativeDosOp((ULONG)0, (ULONG)lock, (ULONG)filename, (ULONG)256);
UnLock(lock);
strncat(command, "\"", sizeof(command) - strlen(command) - 1);
strncat(command, filename, sizeof(command) - strlen(command) - 1);
strncat(command, "\"", sizeof(command) - strlen(command) - 1);
memset(filename, '\0', 100);

// Escape spaces in filename
int j = 0;
for (int k = 0; k < strlen(filename); k++)
{
if (filename[k] == ' ')
{
escaped_filename[j++] = '\\';
}
escaped_filename[j++] = filename[k];
}
escaped_filename[j] = '\0';

strncat(command, escaped_filename, sizeof(command) - strlen(command) - 1);
memset(filename, '\0', 256);
memset(escaped_filename, '\0', 512);
}
else
{
strncat(command, "\"", sizeof(command) - strlen(command) - 1);
strncat(command, argv[i], sizeof(command) - strlen(command) - 1);
strncat(command, "\"", sizeof(command) - strlen(command) - 1);
}

if (i != argc - 1)
Expand All @@ -67,5 +81,5 @@ int main(int argc, char *argv[])
printf("DEBUG: argc=%d, command=%s\n", argc, command);
#endif
ExecuteOnHost((UBYTE *)&command);
memset(command, '\0', 2048);
memset(command, '\0', 4096);
}

0 comments on commit c01c391

Please sign in to comment.