freopen improvements

This adds a check for the filename argument being null, in which case it bails out and returns NULL. Previously, it would just dereference the null pointer and treat the contents of memory location 0 as a filename, with unpredictable results. A null filename indicates freopen should try to reopen the same file with a different mode, but the degree of support for that is implementation-defined. We still don't really support it, but at least this will report the failure and avoid unpredictable behavior.

It also removes some unused code at the end, but still forces fputc and fgetc to be linked in. These are needed because there are weak references to them in putchar and getchar, which may need to be used if stdin/stdout have been reopened.
This commit is contained in:
Stephen Heumann 2022-06-26 21:20:23 -05:00
parent 8cfb73a474
commit 7c2fb70c4a
1 changed files with 6 additions and 27 deletions

View File

@ -1079,7 +1079,10 @@ cl1 ldy #FILE_file branch if the file is not a disk file
;
; create a GS/OS file name
;
cn1 ph4 filename get the length of the name buffer
cn1 lda filename bail out if filename is NULL
ora filename+2 (reopening same file not supported)
jeq rt2
ph4 filename get the length of the name buffer
jsl ~osname
sta OSname
stx OSname+2
@ -1259,33 +1262,9 @@ crFileType ds 2
cl dc i'1' parameter block for OSclose
clRefNum ds 2
;
; Patch for standard out
; Force inclusion of functions that have weak references elsewhere
;
stdoutFile jmp stdoutPatch
stdoutPatch phb
plx
ply
pla
pha
pha
pha
phy
phx
plb
lda >stdout
sta 6,S
lda >stdout+2
sta 8,S
brl fputc
;
; Patch for standard in
;
stdinFile jmp stdinPatch
stdinPatch ph4 #stdin+4
jsl fgetc
rtl
dc r'fputc,fgetc'
end
****************************************************************