From 36adcbe4671da75d7fa73c4426434596e3ff2bc0 Mon Sep 17 00:00:00 2001 From: PQCraft <58464017+PQCraft@users.noreply.github.com> Date: Wed, 28 Apr 2021 09:10:28 -0400 Subject: [PATCH] 0.14.1 Stopped CLIBASIC from trying to load directories. --- clibasic.c | 22 ++++++++++++++++++++-- commands.c | 1 + 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/clibasic.c b/clibasic.c index a23bcc0..87a3382 100644 --- a/clibasic.c +++ b/clibasic.c @@ -12,7 +12,9 @@ #include #include #include +#include #include +#include int tab_width = 4; @@ -58,7 +60,7 @@ int tab_width = 4; } #endif -char VER[] = "0.14"; +char VER[] = "0.14.1"; #ifndef CB_BUF_SIZE #define CB_BUF_SIZE 32768 @@ -265,6 +267,7 @@ uint8_t getVal(char* inbuf, char* outbuf); void resetTimer(); void loadProg(); void updateTxtAttrib(); +bool isFile(); char* gethome() { if (!homepath) { @@ -345,6 +348,7 @@ int main(int argc, char* argv[]) { progFilename = malloc(strlen(argv[i]) + 1); copyStr(argv[i], progFilename); if (!prog) {fprintf(stderr, "File not found: '%s'.\n", progFilename); free(progFilename); err = ENOENT; cleanExit();} + if (!isFile(progFilename)) {puts("Expected file instead of directory."); err = EISDIR; cleanExit();} } } if (exit) cleanExit(); @@ -394,7 +398,9 @@ int main(int argc, char* argv[]) { prog = fopen("AUTORUN.BAS", "r"); progFilename = malloc(12); strcpy(progFilename, "AUTORUN.BAS"); if (!prog) {prog = fopen("autorun.bas", "r"); strcpy(progFilename, "autorun.bas");} if (!prog) {free(progFilename);} - else {loadProg(); inProg = true;} + else { + if (isFile(progFilename)) {loadProg(); inProg = true;} + } ret = chdir(tmpcwd); (void)ret; } @@ -530,6 +536,12 @@ void wait(uint64_t d) { #endif } +bool isFile(char *path) { + struct stat pathstat; + stat(path, &pathstat); + return (bool)(S_ISREG(pathstat.st_mode)); +} + void getCurPos() { fflush(stdout); cury = 0; curx = 0; @@ -1460,6 +1472,12 @@ void runcmd() { case 17: printf("Cannot change to directory '%s'", errstr); break; + case 18: + fputs("Expected file instead of directory.", stdout); + break; + case 19: + fputs("Expected directory instead of file.", stdout); + break; case 127: printf("Not a function: '%s'", errstr); break; diff --git a/commands.c b/commands.c index 82807dc..726c7c7 100644 --- a/commands.c +++ b/commands.c @@ -169,6 +169,7 @@ if (chkCmd(1, arg[0], "RUN")) { } progFilename = malloc(argl[1] + 1); copyStr(arg[1], progFilename); + if (!isFile(progFilename)) {cerr = 18; goto cmderr;} loadProg(); chkinProg = true; goto cmderr;