From 4cb4128a739c1c54a1e9803e4948572cd01008f1 Mon Sep 17 00:00:00 2001 From: John van Groningen Date: Tue, 22 Jun 2004 15:25:47 +0000 Subject: detect EOF in file_read_string and file_read_line for redirected files and improve performance of these functions --- ufileIO2.c | 75 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/ufileIO2.c b/ufileIO2.c index 66ec322..f079f70 100644 --- a/ufileIO2.c +++ b/ufileIO2.c @@ -497,7 +497,7 @@ int file_read_char (long fn) case 0: IO_error ("FReadC: can't read from StdErr"); case 1: - return w_get_char(); + return getchar(); default: IO_error ("FReadC: can't open this file"); } @@ -608,28 +608,15 @@ unsigned long file_read_string (long fn,unsigned long max_length,struct clean_st case 0: IO_error ("FReadS: can't read from StdErr"); case 1: - { - char *string; - - length=0; - string=s->characters; - - while (length!=max_length){ - *string++=w_get_char(); - ++length; - } - + length = fread (s->characters,1,max_length,stdin); s->length=length; return length; - } default: IO_error ("FReadS: can't open this file"); } } else { struct file *f; FILE *fd; - char *string; - int c; f=&file_table[fn]; @@ -637,14 +624,7 @@ unsigned long file_read_string (long fn,unsigned long max_length,struct clean_st IO_error ("FReadS: read from an output file"); fd=f->file; - length=0; - string=s->characters; - - while (length!=max_length && (c=getc (fd),c!=EOF)){ - *string++=c; - ++length; - } - + length = fread (s->characters,1,max_length,fd); s->length=length; return length; @@ -661,19 +641,26 @@ unsigned long file_read_line (long fn,unsigned long max_length,char *string) IO_error ("FReadLine: can't read from StdErr"); case 1: { + int c; length=0; - while (length!=max_length){ - int c; + flockfile (stdin); - c=w_get_char(); + while (length!=max_length && (c=getchar_unlocked(),c!=EOF)){ *string++=c; ++length; - if (c=='\n') + if (c=='\n'){ + funlockfile (stdin); return length; + } } - return -1; + funlockfile (stdin); + + if (c!=EOF) + return -1; + + return length; } default: IO_error ("FReadLine: can't open this file"); @@ -690,21 +677,29 @@ unsigned long file_read_line (long fn,unsigned long max_length,char *string) c=0; if (f->mode & (1<mode & (1<