Skip to content

Commit e90a692

Browse files
committed
fix fopen() overwriting stdin file handle
See adtools#2 Fix is extracted from the fork by @bebbo, specifically the two following commits: bebbo/libnix@fa82b7a bebbo/libnix@95dbee7
1 parent e7bfc56 commit e90a692

File tree

3 files changed

+4
-2
lines changed

3 files changed

+4
-2
lines changed

Diff for: sources/nix/stdio/fopen.c

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ FILE *fopen(const char *filename,const char *mode)
1919
{ if((node->FILE.buffer=(char *)malloc(BUFSIZ))!=NULL)
2020
{ node->FILE.bufsize=BUFSIZ;
2121
node->FILE.flags|=__SMBF; /* Buffer is malloc'ed */
22+
node->FILE.file = -1;
2223
if(freopen(filename,mode,&node->FILE)!=NULL)
2324
{ AddHead((struct List *)&__filelist,(struct Node *)&node->node);
2425
return &node->FILE; }

Diff for: sources/nix/stdio/freopen.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ extern void __seterrno(void);
1111
FILE *freopen(const char *filename,const char *mode,FILE *stream)
1212
{ int error=__fflush(stream);
1313

14-
close(stream->file);
14+
if (stream->file >= 0)
15+
close(stream->file);
1516
if(stream->name!=NULL) /* file is temporary */
1617
{ BPTR cd=CurrentDir(stream->tmpdir); /* cd t: */
1718
if(!DeleteFile(stream->name)) /* delete file */

Diff for: sources/nix/stdio/open.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static __inline StdFileDes *_allocfd(void)
4040
int file,max;
4141

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

4646
if(file>SHRT_MAX)

0 commit comments

Comments
 (0)