see the size_t is unsigned cannot be negative. that is the problem.
i think if the terminal is close,the "size_t fwrite()"function should
return a negative number,and the nsh_catfile should be broken.
That is not the way that fwrite returns errors. If an error occurs
fwrite returns fewer item than you requested to send. That is required
by the specification:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/fwrite.html
On an error, fwrite will set the errno value and the error indictor
RETURN VALUE
The /fwrite/() function shall return the number of elements
successfully written, which *may be less than **/nitems/**if a write
error is encountered*. If /size/ or /nitems/ is 0, /fwrite/() shall
return 0 and the state of the stream remains unchanged.
Otherwise,*if a write error occurs, the error indicator for the
stream shall be set*, ^[CX] [Option Start] and /errno/ shall be set
to indicate the error.
If fwrite() returns fewer items than you requiested (or zero), you must
call ferror() to determine if an error occurred:
https://pubs.opengroup.org/onlinepubs/009695399/functions/ferror.html
If the error indicator is set, you can get the error from the errno
variable.
The error indicator is cleared with clearerr():
https://pubs.opengroup.org/onlinepubs/009695399/functions/clearerr.html
It may not return a negative value.