<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">From 59830fe24d0158bd9a88684dca8fb1a4e0a109f8 Mon Sep 17 00:00:00 2001
From: Adam Sampson &lt;ats@offog.org&gt;
Date: Thu, 15 May 2025 12:39:55 +0100
Subject: [PATCH 1/3] Use conventional exit statuses

The code uses "exit(-1)" to indicate an error in various places. It's
normal to "exit(1)" (i.e. "exit(EXIT_FAILURE)") on error, and this is
less messy if, e.g., you've got a shell prompt that shows you the exit
status of the previous command.

For "--help", the convention is to exit(0), since the program has
successfully done what you've asked for.
---
 configure.ac | 6 +++---
 src/1770.c   | 2 +-
 src/6502.c   | 6 +++---
 src/adf.c    | 2 +-
 src/fdi.c    | 2 +-
 src/linux.c  | 2 +-
 src/main.c   | 2 +-
 src/ssd.c    | 2 +-
 src/uef.c    | 2 +-
 9 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/configure.ac b/configure.ac
index fc0d6b8..0d11e18 100644
--- a/configure.ac
+++ b/configure.ac
@@ -40,13 +40,13 @@ fi
 
 AC_CHECK_LIB([z], [gzopen], [], \
 	[echo "You need to install the zlib library."
-	 exit -1])
+	 exit 1])
 AC_CHECK_LIB([openal], [alGetError], [], \
 	[echo "You need to install the OpenAL library."
-	 exit -1])
+	 exit 1])
 AC_CHECK_LIB([alut], [alutInit], [], \
    [echo "You need to install the ALUT library."
-    exit -1])
+    exit 1])
 
 AC_CHECK_LIB([m], [sin])
 
diff --git a/src/1770.c b/src/1770.c
index 8d6ea66..24c8ed1 100644
--- a/src/1770.c
+++ b/src/1770.c
@@ -172,7 +172,7 @@ void write1770(uint16_t addr, uint8_t val)
                         rpclog("Bad 1770 command %02X\n",val);
                         dumpregs();
                         dumpram();
-                        exit(-1);
+                        exit(1);
                 }
                 break;
                 case 0xFCC5:
diff --git a/src/6502.c b/src/6502.c
index dc44f24..9ca1d07 100644
--- a/src/6502.c
+++ b/src/6502.c
@@ -83,14 +83,14 @@ void ADCBCD(uint8_t temp, int tempc)
 {
         printf("BCD ADC!\n");
         dumpregs();
-        exit(-1);
+        exit(1);
 }
 
 void SBCBCD(uint8_t temp, int tempc)
 {
         printf("BCD SBC!\n");
         dumpregs();
-        exit(-1);
+        exit(1);
 }
 
 /*ADC/SBC temp variables*/
@@ -1682,7 +1682,7 @@ void exec6502()
                         dumpregs();
                         rpclog("Bad 6502 opcode %02X %04X %04X %i %i\n",opcode,oldpc,oldpc2,extrom,rombank);
                         fflush(stdout);
-                        exit(-1);*/
+                        exit(1);*/
                 }
 //                if (output) rpclog("%04X : %02X %02X %02X %02X\n",pc,a,x,y,s);
 //                if (pc==0x1000) rpclog("Loop!\n");
diff --git a/src/adf.c b/src/adf.c
index 9477f32..789adec 100644
--- a/src/adf.c
+++ b/src/adf.c
@@ -266,7 +266,7 @@ void adf_poll()
                 {
                         return; /*Hack - wait until data is available*/
 //                        printf("Data overflow!\n");
-//                        exit(-1);
+//                        exit(1);
                 }
                 trackinfoa[adfdrive][adfside][(adfsector*adfsize[adfdrive])+adfreadpos]=c;
                 adfreadpos++;
diff --git a/src/fdi.c b/src/fdi.c
index e3c59dd..dd388aa 100644
--- a/src/fdi.c
+++ b/src/fdi.c
@@ -262,7 +262,7 @@ void fdi_poll()
                                                 {
 //                                                        printf("Header CRC error : %02X %02X %02X %02X\n",crc&gt;&gt;8,crc&amp;0xFF,fdisectordat[4],fdisectordat[5]);
 //                                                        dumpregs();
-//                                                        exit(-1);
+//                                                        exit(1);
                                                         inreadop=0;
                                                         if (fdireadaddr)
                                                         {
diff --git a/src/linux.c b/src/linux.c
index 8a181ad..0363142 100644
--- a/src/linux.c
+++ b/src/linux.c
@@ -47,7 +47,7 @@ int main(int argc, char *argv[])
         if (ret != 0)
         {
                 fprintf(stderr, "Error %d initializing Allegro.\n", ret);
-                exit(-1);
+                exit(1);
         }
         initelk(argc,argv);
         set_close_button_callback(native_window_close_button_handler);
diff --git a/src/main.c b/src/main.c
index ef88a38..1db99b8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -105,7 +105,7 @@ void initelk(int argc, char *argv[])
                         printf("-serialdebug n  - set serial debugging output level to n\n");
                         printf("-rom number rom - load rom into the numbered bank\n");
                         printf("-debug          - start debugger\n");
-                        exit(-1);
+                        exit(0);
                 }
                 else
 #endif
diff --git a/src/ssd.c b/src/ssd.c
index bbfeca2..5023b6d 100644
--- a/src/ssd.c
+++ b/src/ssd.c
@@ -222,7 +222,7 @@ void ssd_poll()
                 {
                         return;
 //                        printf("Data overflow!\n");
-//                        exit(-1);
+//                        exit(1);
                 }
                 trackinfo[ssddrive][ssdside][(ssdsector&lt;&lt;8)+ssdreadpos]=c;
                 ssdreadpos++;
diff --git a/src/uef.c b/src/uef.c
index 04180e9..f642d19 100644
--- a/src/uef.c
+++ b/src/uef.c
@@ -248,7 +248,7 @@ void polluef()
         }
         allegro_exit();
         printf("Bad chunk ID %04X length %i\n",chunkid,chunklen);
-        exit(-1);
+        exit(1);
 }
 
 void closeuef()
-- 
2.49.0

From 20c38c3ffd08f0ebc1359aa0dd84cc46d49813a2 Mon Sep 17 00:00:00 2001
From: Adam Sampson &lt;ats@offog.org&gt;
Date: Thu, 15 May 2025 12:57:10 +0100
Subject: [PATCH 2/3] Disable rpclog in a more obvious way

This is debugging code that's usually disabled, but the "return;" in the
middle of the code was a bit non-obvious; use an #ifdef instead.
---
 src/main.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/main.c b/src/main.c
index 1db99b8..387c694 100644
--- a/src/main.c
+++ b/src/main.c
@@ -10,8 +10,8 @@ int autoboot;
 FILE *rlog;
 void rpclog(char *format, ...)
 {
+#if 0
    char buf[256];
-   return;
    if (!rlog) rlog=fopen("e:/devcpp/cycleelk/rlog.txt","wt");
 //turn;
    va_list ap;
@@ -20,6 +20,7 @@ void rpclog(char *format, ...)
    va_end(ap);
    fputs(buf,rlog);
    fflush(rlog);
+#endif
 }
 
 /*int waiting,waiting2;
-- 
2.49.0

From 28d1c15992d02cace446330bb5e5e0cf366d442b Mon Sep 17 00:00:00 2001
From: Adam Sampson &lt;ats@offog.org&gt;
Date: Thu, 15 May 2025 12:57:58 +0100
Subject: [PATCH 3/3] Always check the result of fopen

A few places in the code called fopen and then continued without
checking whether it had returned NULL; for example, if you don't have a
config file then Elkulator will crash after failing to open it.

Add the missing checks, with an error message where it's a file that the
user is likely to recognise.
---
 src/config.c    | 10 ++++++++++
 src/disc.c      |  1 +
 src/mem.c       |  1 +
 src/savestate.c | 12 ++++++++++++
 4 files changed, 24 insertions(+)

diff --git a/src/config.c b/src/config.c
index 3a83f34..2e44684 100644
--- a/src/config.c
+++ b/src/config.c
@@ -89,6 +89,11 @@ void loadconfig()
         char fn[MAX_PATH_FILENAME_BUFFER_SIZE + strlen(elk_cfg_filename)];
         sprintf(fn,"%s%s",exedir, elk_cfg_filename);
         cfgfile=fopen(fn,"rt");
+        if (!cfgfile)
+        {
+                fprintf(stderr,"Error opening %s for reading.\n",fn);
+                return;
+        }
         tapespeed=getintcfg("tapespeed",0);
         plus1=getintcfg("plus1",0);
         plus3=getintcfg("plus3",0);
@@ -152,6 +157,11 @@ void saveconfig()
         char fn[MAX_PATH_FILENAME_BUFFER_SIZE + strlen(elk_cfg_filename)];
         sprintf(fn,"%s%s",exedir, elk_cfg_filename);
         cfgfile=fopen(fn,"wt");
+        if (!cfgfile)
+        {
+                fprintf(stderr,"Error opening %s for writing.\n",fn);
+                return;
+        }
         writeintcfg("tapespeed",tapespeed);
         writeintcfg("plus1",plus1);
         writeintcfg("plus3",plus3);
diff --git a/src/disc.c b/src/disc.c
index 16e7b9d..bb9c641 100644
--- a/src/disc.c
+++ b/src/disc.c
@@ -125,6 +125,7 @@ void newdisc(int drive, char *fn)
                 if (!strcasecmp(p,loaders[c].ext) &amp;&amp; loaders[c].size!=-1)
                 {
                         f=fopen(fn,"wb");
+                        if (!f) continue;
                         for (d=0;d&lt;loaders[c].size;d++) putc(0,f);
                         if (!strcasecmp(p,"ADF"))
                         {
diff --git a/src/mem.c b/src/mem.c
index d05a5ee..7a9651c 100644
--- a/src/mem.c
+++ b/src/mem.c
@@ -132,6 +132,7 @@ void unloadcart()
 void dumpram()
 {
         FILE *f=fopen("ram.dmp","wb");
+        if (!f) return;
         fwrite(ram,32768,1,f);
         fclose(f);
 }
diff --git a/src/savestate.c b/src/savestate.c
index bdd9020..60f6f4b 100644
--- a/src/savestate.c
+++ b/src/savestate.c
@@ -16,6 +16,12 @@ void loadstate()
 void dosavestate()
 {
         FILE *f=fopen(ssname,"wb");
+        if (!f) {
+                fprintf(stderr,"Error opening %s for writing.\n",ssname);
+                wantsavestate=0;
+                return;
+        }
+
         putc('E',f); putc('L',f); putc('K',f); putc('S',f);
         putc('N',f); putc('A',f); putc('P',f); putc('1',f);
 
@@ -46,6 +52,12 @@ void doloadstate()
 {
         int c;
         FILE *f=fopen(ssname,"rb");
+        if (!f) {
+                fprintf(stderr,"Error opening %s for reading.\n",ssname);
+                wantloadstate=0;
+                return;
+        }
+
         for (c=0;c&lt;8;c++) getc(f);
         
         turbo=getc(f);
-- 
2.49.0

</pre></body></html>