Merge pull request #24 from daschewie/RunExecutablesWithoutExtension

Added Support for running executables without providing an extension
This commit is contained in:
pweingar 2021-11-28 11:07:28 -05:00 committed by GitHub
commit b4853f4160
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,6 +9,7 @@
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "log.h"
#include "syscalls.h"
#include "fsys.h"
@ -1138,51 +1139,44 @@ short fsys_pgx_loader(short chan, long destination, long * start) {
return result;
}
/*
* Load a file into memory at the designated destination address.
*
* If destination = 0, the file must be in a recognized binary format
* that specifies its own loading address.
*
* Inputs:
* path = the path to the file to load
* destination = the destination address (0 for use file's address)
* start = pointer to the long variable to fill with the starting address
* (0 if not an executable, any other number if file is executable
* with a known starting address)
*
* Returns:
* 0 on success, negative number on error
*/
short fsys_load(const char * path, long destination, long * start) {
static bool get_app_ext(const char * path, char * extension) {
char * point = strrchr(path, '.');
extension[0] = 0;
if (point != 0) {
point++;
for (int i = 0; i < MAX_EXT; i++) {
char c = *point++;
if (c) {
extension[i] = toupper(c);
} else {
extension[i] = 0;
return true;
}
}
} else {
return false;
}
}
static bool loader_exists(const char * extension) {
int i;
for (i = 0; i < MAX_LOADERS; i++) {
if (g_file_loader[i].status) {
if (strcmp(g_file_loader[i].extension, extension) == 0) {
return true;
}
}
}
return false;
}
static short fsys_load_ext(const char * path, const char * extension, long destination, long * start) {
int i;
char extension[MAX_EXT];
short chan = -1;
p_file_loader loader = 0;
TRACE("fsys_load");
/* Clear out the extension */
for (i = 0; i <= MAX_EXT; i++) {
extension[i] = 0;
}
if (destination == 0) {
/* Find the extension */
char * point = strrchr(path, '.');
if (point != 0) {
point++;
for (i = 0; i < MAX_EXT; i++) {
char c = *point++;
if (c) {
extension[i] = toupper(c);
} else {
break;
}
}
}
}
log2(LOG_VERBOSE, "fsys_load ext: ", extension);
if (extension[0] == 0) {
@ -1247,6 +1241,72 @@ short fsys_load(const char * path, long destination, long * start) {
}
}
/*
* Load a file into memory at the designated destination address.
*
* If destination = 0, the file must be in a recognized binary format
* that specifies its own loading address.
*
* Inputs:
* path = the path to the file to load
* destination = the destination address (0 for use file's address)
* start = pointer to the long variable to fill with the starting address
* (0 if not an executable, any other number if file is executable
* with a known starting address)
*
* Returns:
* 0 on success, negative number on error
*/
short fsys_load(const char * path, long destination, long * start) {
int i;
char extension[MAX_EXT];
char spath[MAX_PATH_LEN];
bool found_extension = false;
bool found_loader = false;
FRESULT fr; /* Return value */
DIR dj; /* Directory object */
FILINFO fno; /* File information */
/* Clear out the extension */
for (i = 0; i <= MAX_EXT; i++) {
extension[i] = 0;
}
found_extension = get_app_ext(path, extension);
if (found_extension || destination != 0) {
// extension provided, pass to loader
fsys_load_ext(path, extension, destination, start);
} else {
// extension not provided, search for a matching file.
strcpy(spath, path);
strcat(spath, ".*");
// TODO: Iterate through path, and replace "".
fr = f_findfirst(&dj, &fno, "", spath); /* Start to search for executables */
while (fr == FR_OK && fno.fname[0]) { /* Repeat while an item is found */
get_app_ext(fno.fname, extension);
if (loader_exists(extension)) {
strcpy(spath, fno.fname);
found_loader = true;
break;
}
fr = f_findnext(&dj, &fno); /* Search for next item */
}
f_closedir(&dj);
if(found_loader) {
// Found path with valid loader
fsys_load_ext(spath, extension, destination, start);
} else {
log(LOG_ERROR, "Command not found.");
}
}
}
/*
* Register a file loading routine
*