Skip to content

fix fopen() overwriting stdin file handle #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/helloworld.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <inline/exec.h>
#include <inline/dos.h>

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;

Expand Down
2 changes: 1 addition & 1 deletion sources/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
2 changes: 1 addition & 1 deletion sources/math/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sources/misc/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion sources/nix/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -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)" >>$@
1 change: 1 addition & 0 deletions sources/nix/stdio/fopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
3 changes: 2 additions & 1 deletion sources/nix/stdio/freopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion sources/nix/stdio/open.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static __inline StdFileDes *_allocfd(void)
int file,max;

for(sfd=stdfiledes,max=stdfilesize,file=0;file<max;sfd++,file++)
if(!sfd[0] || !sfd[0]->lx_inuse)
if(!sfd[0] || (!sfd[0]->lx_inuse && !sfd[0]->lx_sys))
break;

if(file>SHRT_MAX)
Expand Down
5 changes: 4 additions & 1 deletion sources/nix13/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ 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)
$(RANLIB) $@
echo "\$$$(V)" >>$@
# echo "\\$$$(V)" >>$@
5 changes: 4 additions & 1 deletion sources/nix20/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ 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)
$(RANLIB) $@
echo "\$$$(V)" >>$@
# echo "\\$$$(V)" >>$@
2 changes: 1 addition & 1 deletion sources/nix_main/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ libnix_main.a: $(OBJECTS) ../../sources/nix_main/Makefile
-rm -f $@
$(AR) -q $@ $(OBJECTS)
$(RANLIB) $@
echo "\$$$(V)" >>$@
# echo "\\$$$(V)" >>$@
2 changes: 1 addition & 1 deletion sources/nixmain/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ libnixmain.a: $(OBJECTS) ../../sources/nixmain/Makefile
-rm -f $@
$(AR) -q $@ $(OBJECTS)
$(RANLIB) $@
echo "\$$$(V)" >>$@
# echo "\\$$$(V)" >>$@
2 changes: 1 addition & 1 deletion sources/socket/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ libsocket.a: $(foreach f,$(SUBDIRS2),$(CURDIR)/$(f)) $(OBJECTS2) ../../sources/s
-rm -f $@
$(AR) -q $@ $(OBJECTS2)
$(RANLIB) $@
echo "\$$$(V)" >>$@
# echo "\\$$$(V)" >>$@
2 changes: 1 addition & 1 deletion sources/stack/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ libstack.a: $(OBJECTS) ../../sources/stack/Makefile ../../sources/stack/filelist
-rm -f $@
$(AR) -q $@ $(OBJECTS)
$(RANLIB) $@
echo "\$$$(V)" >>$@
# echo "\\$$$(V)" >>$@
4 changes: 2 additions & 2 deletions sources/stack/stkrst.c
Original file line number Diff line number Diff line change
Expand Up @@ -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@-;"
Expand Down
18 changes: 7 additions & 11 deletions sources/startup/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)" >>$@
2 changes: 1 addition & 1 deletion sources/stubs/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ libstubs.a: $(foreach f,$(SUBDIRS2),$(CURDIR)/$(f)) $(OBJECTS2) ../../sources/st
rm -f $@
$(AR) -q $@ $(OBJECTS2)
$(RANLIB) $@
echo "\$$$(V)" >>$@
# echo "\\$$$(V)" >>$@
2 changes: 1 addition & 1 deletion sources/stubs/misc/__cpucheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ ADD2INIT(__cpucheck,-80); /* Highest priority */

#else

int __cpucheck;
void __cpucheck(){}

#endif