From e90a69222344e47901991440f24d37d1d8365212 Mon Sep 17 00:00:00 2001 From: sezero Date: Wed, 7 Aug 2019 06:28:24 +0300 Subject: [PATCH 1/5] fix fopen() overwriting stdin file handle See https://github.com/adtools/libnix/issues/2 Fix is extracted from the fork by @bebbo, specifically the two following commits: https://github.com/bebbo/libnix/commit/fa82b7a1aa5ca575d79ff8d4b5c55da24b19f835 https://github.com/bebbo/libnix/commit/95dbee7b50409bad6bae725c8f5eab0c836a2338 --- sources/nix/stdio/fopen.c | 1 + sources/nix/stdio/freopen.c | 3 ++- sources/nix/stdio/open.c | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/sources/nix/stdio/fopen.c b/sources/nix/stdio/fopen.c index bbac775..f347c42 100644 --- a/sources/nix/stdio/fopen.c +++ b/sources/nix/stdio/fopen.c @@ -19,6 +19,7 @@ FILE *fopen(const char *filename,const char *mode) { if((node->FILE.buffer=(char *)malloc(BUFSIZ))!=NULL) { node->FILE.bufsize=BUFSIZ; node->FILE.flags|=__SMBF; /* Buffer is malloc'ed */ + node->FILE.file = -1; if(freopen(filename,mode,&node->FILE)!=NULL) { AddHead((struct List *)&__filelist,(struct Node *)&node->node); return &node->FILE; } diff --git a/sources/nix/stdio/freopen.c b/sources/nix/stdio/freopen.c index c248318..ca1c3e6 100644 --- a/sources/nix/stdio/freopen.c +++ b/sources/nix/stdio/freopen.c @@ -11,7 +11,8 @@ extern void __seterrno(void); FILE *freopen(const char *filename,const char *mode,FILE *stream) { int error=__fflush(stream); - close(stream->file); + if (stream->file >= 0) + close(stream->file); if(stream->name!=NULL) /* file is temporary */ { BPTR cd=CurrentDir(stream->tmpdir); /* cd t: */ if(!DeleteFile(stream->name)) /* delete file */ diff --git a/sources/nix/stdio/open.c b/sources/nix/stdio/open.c index af7b356..2e111d8 100644 --- a/sources/nix/stdio/open.c +++ b/sources/nix/stdio/open.c @@ -40,7 +40,7 @@ static __inline StdFileDes *_allocfd(void) int file,max; for(sfd=stdfiledes,max=stdfilesize,file=0;filelx_inuse) + if(!sfd[0] || (!sfd[0]->lx_inuse && !sfd[0]->lx_sys)) break; if(file>SHRT_MAX) From 01cf5bd387b7e6f069ab2fc29d1b9426f1e5df74 Mon Sep 17 00:00:00 2001 From: sezero Date: Mon, 28 Feb 2022 23:10:10 +0300 Subject: [PATCH 2/5] do not append garbage to amiga hunks from the fork by @bebbo, specifically the two following commits: https://github.com/bebbo/libnix/commit/480d298f6e109b6f2813a22b06aef30cfe9ef786 https://github.com/bebbo/libnix/commit/b3b28a22251f5e4ac0f9d9b505f13ea2e27d1a08 --- sources/math/Makefile.in | 2 +- sources/misc/Makefile.in | 2 +- sources/nix/Makefile.in | 2 +- sources/nix13/Makefile.in | 2 +- sources/nix20/Makefile.in | 2 +- sources/nix_main/Makefile.in | 2 +- sources/nixmain/Makefile.in | 2 +- sources/socket/Makefile.in | 2 +- sources/stack/Makefile.in | 2 +- sources/startup/Makefile.in | 18 +++++++----------- sources/stubs/Makefile.in | 2 +- 11 files changed, 17 insertions(+), 21 deletions(-) diff --git a/sources/math/Makefile.in b/sources/math/Makefile.in index 17c5468..9fc57cd 100644 --- a/sources/math/Makefile.in +++ b/sources/math/Makefile.in @@ -63,7 +63,7 @@ libm.a: $(foreach f,$(SUBDIRS2),$(CURDIR)/$(f)) $(OBJECTS2) ../../sources/math/M -rm -f $@ $(AR) -q $@ $(OBJECTS2) $(RANLIB) $@ - echo "\$$$(V)" >>$@ +# echo "\\$$$(V)" >>$@ stdio/vfprintf.o: $(srcdir)/../nix/stdio/vfprintf.c $(CC) $(OPTIONS) $^ -c -o stdio/vfprintf.o diff --git a/sources/misc/Makefile.in b/sources/misc/Makefile.in index 6417e9e..f11e943 100644 --- a/sources/misc/Makefile.in +++ b/sources/misc/Makefile.in @@ -34,7 +34,7 @@ REDEF=-D_DOSBase=___DOSBase \ $(CC) $(OPTIONS) $*2.S -c -o $*.o -rm $*.S $*2.S -if test ! -s $*.err; then rm $*.err; fi - echo "\$$$(V)" >>$*.o +# echo "\$$$(V)" >>$*.o .PHONY: all filelist subdirs clean veryclean diff --git a/sources/nix/Makefile.in b/sources/nix/Makefile.in index 9aa1323..34784a3 100644 --- a/sources/nix/Makefile.in +++ b/sources/nix/Makefile.in @@ -53,4 +53,4 @@ libnix.a: $(foreach f,$(SUBDIRS),$(CURDIR)/$(f)) $(OBJECTS) ../../sources/nix/Ma -rm -f $@ $(AR) -q $@ $(OBJECTS) $(RANLIB) $@ - echo "\$$$(V)" >>$@ +# echo "\\$$$(V)" >>$@ diff --git a/sources/nix13/Makefile.in b/sources/nix13/Makefile.in index f023ece..784b3d5 100644 --- a/sources/nix13/Makefile.in +++ b/sources/nix13/Makefile.in @@ -53,4 +53,4 @@ libnix13.a: $(foreach f,$(SUBDIRS),$(CURDIR)/$(f)) $(OBJECTS) ../../sources/nix1 -rm -f $@ $(AR) -q $@ $(OBJECTS) $(RANLIB) $@ - echo "\$$$(V)" >>$@ +# echo "\\$$$(V)" >>$@ diff --git a/sources/nix20/Makefile.in b/sources/nix20/Makefile.in index fb7a352..5863e9c 100644 --- a/sources/nix20/Makefile.in +++ b/sources/nix20/Makefile.in @@ -53,4 +53,4 @@ libnix20.a: $(foreach f,$(SUBDIRS),$(CURDIR)/$(f)) $(OBJECTS) ../../sources/nix2 -rm -f $@ $(AR) -q $@ $(OBJECTS) $(RANLIB) $@ - echo "\$$$(V)" >>$@ +# echo "\\$$$(V)" >>$@ diff --git a/sources/nix_main/Makefile.in b/sources/nix_main/Makefile.in index c8cb024..1c9837d 100644 --- a/sources/nix_main/Makefile.in +++ b/sources/nix_main/Makefile.in @@ -51,4 +51,4 @@ libnix_main.a: $(OBJECTS) ../../sources/nix_main/Makefile -rm -f $@ $(AR) -q $@ $(OBJECTS) $(RANLIB) $@ - echo "\$$$(V)" >>$@ +# echo "\\$$$(V)" >>$@ diff --git a/sources/nixmain/Makefile.in b/sources/nixmain/Makefile.in index 238ded8..4ee6db2 100644 --- a/sources/nixmain/Makefile.in +++ b/sources/nixmain/Makefile.in @@ -51,4 +51,4 @@ libnixmain.a: $(OBJECTS) ../../sources/nixmain/Makefile -rm -f $@ $(AR) -q $@ $(OBJECTS) $(RANLIB) $@ - echo "\$$$(V)" >>$@ +# echo "\\$$$(V)" >>$@ diff --git a/sources/socket/Makefile.in b/sources/socket/Makefile.in index 1bc2b15..fbc3125 100644 --- a/sources/socket/Makefile.in +++ b/sources/socket/Makefile.in @@ -71,4 +71,4 @@ libsocket.a: $(foreach f,$(SUBDIRS2),$(CURDIR)/$(f)) $(OBJECTS2) ../../sources/s -rm -f $@ $(AR) -q $@ $(OBJECTS2) $(RANLIB) $@ - echo "\$$$(V)" >>$@ +# echo "\\$$$(V)" >>$@ diff --git a/sources/stack/Makefile.in b/sources/stack/Makefile.in index 7982664..7c33ff4 100644 --- a/sources/stack/Makefile.in +++ b/sources/stack/Makefile.in @@ -42,4 +42,4 @@ libstack.a: $(OBJECTS) ../../sources/stack/Makefile ../../sources/stack/filelist -rm -f $@ $(AR) -q $@ $(OBJECTS) $(RANLIB) $@ - echo "\$$$(V)" >>$@ +# echo "\\$$$(V)" >>$@ diff --git a/sources/startup/Makefile.in b/sources/startup/Makefile.in index 8d5f07d..6eb56be 100644 --- a/sources/startup/Makefile.in +++ b/sources/startup/Makefile.in @@ -22,7 +22,7 @@ OPTIONS=-I$(srcdir)/../headers -O -fomit-frame-pointer %.o: %.c $(CC) $(OPTIONS) -c $^ 2>&1|tee $*.err -if test ! -s $*.err; then rm $*.err; fi - echo "\$$$(V)" >>$@ +# echo "\$$$(V)" >>$@ .PHONY: all filelist subdirs clean veryclean @@ -38,31 +38,27 @@ veryclean: -rm *.o # baserelative resident startup-code - nrcrt0.o: nrcrt0.S $(CC) -c $^ - echo "\$$$(V)" >>$@ +# echo "\$$$(V)" >>$@ # large baserelative resident startup-code - nlrcrt0.o: nlrcrt0.S $(CC) -m68020 -c $^ - echo "\$$$(V)" >>$@ -# baserelative startup code +# echo "\$$$(V)" >>$@ +# baserelative startup code nbcrt0.o: nbcrt0.S $(CC) -c $^ - echo "\$$$(V)" >>$@ +# echo "\$$$(V)" >>$@ # large baserelative startup code - nlbcrt0.o: nlbcrt0.S $(CC) -m68020 -c $^ - echo "\$$$(V)" >>$@ +# echo "\$$$(V)" >>$@ # normal (=large) startup code - ncrt0.o: ncrt0.S $(CC) -c $^ - echo "\$$$(V)" >>$@ +# echo "\$$$(V)" >>$@ diff --git a/sources/stubs/Makefile.in b/sources/stubs/Makefile.in index 223bb41..b33b64f 100644 --- a/sources/stubs/Makefile.in +++ b/sources/stubs/Makefile.in @@ -46,4 +46,4 @@ libstubs.a: $(foreach f,$(SUBDIRS2),$(CURDIR)/$(f)) $(OBJECTS2) ../../sources/st rm -f $@ $(AR) -q $@ $(OBJECTS2) $(RANLIB) $@ - echo "\$$$(V)" >>$@ +# echo "\\$$$(V)" >>$@ From 120105ba564b9f5989f4162b02c3f3eb207a80ae Mon Sep 17 00:00:00 2001 From: sezero Date: Mon, 28 Feb 2022 23:10:10 +0300 Subject: [PATCH 3/5] do not mix variables and functions from the fork by @bebbo, specifically the following commit: https://github.com/bebbo/libnix/commit/480d298f6e109b6f2813a22b06aef30cfe9ef786 --- examples/helloworld.c | 4 ++-- sources/stubs/misc/__cpucheck.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/helloworld.c b/examples/helloworld.c index 9de0032..a3cdf7c 100644 --- a/examples/helloworld.c +++ b/examples/helloworld.c @@ -3,8 +3,8 @@ #include #include -int __nocommandline=1; /* Disable commandline parsing */ -int __initlibraries=0; /* Disable auto-library-opening */ +void __nocommandline(){}; /* Disable commandline parsing */ +void __initlibraries(){}; /* Disable auto-library-opening */ struct DosLibrary *DOSBase=NULL; diff --git a/sources/stubs/misc/__cpucheck.c b/sources/stubs/misc/__cpucheck.c index 7953bf0..e3257f8 100644 --- a/sources/stubs/misc/__cpucheck.c +++ b/sources/stubs/misc/__cpucheck.c @@ -47,6 +47,6 @@ ADD2INIT(__cpucheck,-80); /* Highest priority */ #else -int __cpucheck; +void __cpucheck(){} #endif From 47a514f9252e7d053b9914d1f2d7a66419077e1a Mon Sep 17 00:00:00 2001 From: sezero Date: Mon, 28 Feb 2022 23:10:10 +0300 Subject: [PATCH 4/5] fix typos in stack/stkrst.c from the fork by @bebbo, specifically the following commit: https://github.com/bebbo/libnix/commit/1e56fc31d3273e5ee402e68aa9843b94799f2859 --- sources/stack/stkrst.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sources/stack/stkrst.c b/sources/stack/stkrst.c index 11d2c25..dc43238 100644 --- a/sources/stack/stkrst.c +++ b/sources/stack/stkrst.c @@ -19,14 +19,14 @@ asm( "| Race condition ahead! exec might preempt our task at any time storing" "| the current register set on top of the free stack. NEVER set a sp higher" "| than the location of important data." - "|" + "|\n" " .comm ___used_stack,8;" /* pointer to used stackframes */ /* pointer to unused stackframes */ " .text;" " .even;" " .globl ___stkrst;" - "__stkrst:;" + "___stkrst:;" " exg d0,a3;" /* better use an address register*/ " movel a2,sp@-;" " moveml d0/d1/a0/a1/a5/a6,sp@-;" From 1822e962f48275f6121a9620484089060a74a237 Mon Sep 17 00:00:00 2001 From: Ozkan Sezer Date: Mon, 4 Apr 2022 11:11:10 +0300 Subject: [PATCH 5/5] hack to add swapstack.o into libnix13.a and libnix20.a: bebbo's toolchain does it differently, same end result. --- sources/Makefile.in | 2 +- sources/nix13/Makefile.in | 3 +++ sources/nix20/Makefile.in | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/sources/Makefile.in b/sources/Makefile.in index b1c24cc..e3c33af 100644 --- a/sources/Makefile.in +++ b/sources/Makefile.in @@ -26,7 +26,7 @@ CURDIR = $(shell pwd) MAKELIST=$(MAKE) -f $(CURDIR)/Makefile -SUBDIRS=nixmain nix_main misc math nix nix13 nix20 socket stack stubs +SUBDIRS=misc nixmain nix_main math nix nix13 nix20 socket stack stubs all: $(foreach f,$(SUBDIRS),$(CURDIR)/$(f)) for subdir in $(SUBDIRS); do \ diff --git a/sources/nix13/Makefile.in b/sources/nix13/Makefile.in index 784b3d5..587ba49 100644 --- a/sources/nix13/Makefile.in +++ b/sources/nix13/Makefile.in @@ -49,6 +49,9 @@ veryclean: $(foreach f,$(SUBDIRS),$(CURDIR)/$(f)): mkdir $@ +# hack to add swapstack.o into libnix13.a: +OBJECTS += ../misc/swapstack.o + libnix13.a: $(foreach f,$(SUBDIRS),$(CURDIR)/$(f)) $(OBJECTS) ../../sources/nix13/Makefile ../../sources/nix13/filelist -rm -f $@ $(AR) -q $@ $(OBJECTS) diff --git a/sources/nix20/Makefile.in b/sources/nix20/Makefile.in index 5863e9c..1183e9c 100644 --- a/sources/nix20/Makefile.in +++ b/sources/nix20/Makefile.in @@ -49,6 +49,9 @@ veryclean: $(foreach f,$(SUBDIRS),$(CURDIR)/$(f)): mkdir $@ +# hack to add swapstack.o into libnix20.a: +OBJECTS += ../misc/swapstack.o + libnix20.a: $(foreach f,$(SUBDIRS),$(CURDIR)/$(f)) $(OBJECTS) ../../sources/nix20/Makefile ../../sources/nix20/filelist -rm -f $@ $(AR) -q $@ $(OBJECTS)