From f751af1a0ff0307dce00332a9057bfa6aa47aa34 Mon Sep 17 00:00:00 2001 From: John van Groningen Date: Fri, 25 Jun 2004 12:53:02 +0000 Subject: fix freads and freadline for redirected stdio on macho, use _unlocked functions for reading and writing strings --- mcon.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- mcon.h | 2 + mfileIO3.c | 57 ++++++++++++++-------------- 3 files changed, 144 insertions(+), 38 deletions(-) diff --git a/mcon.c b/mcon.c index 672d6c4..aba1fb3 100644 --- a/mcon.c +++ b/mcon.c @@ -457,8 +457,6 @@ static int use_stdio; #define swap_nl_cr(c) (((c)=='\n' || (c)=='\r') ? ((c) ^ ((char)('\n' ^ '\r'))) : (c)) #define oputc(c) putchar(swap_nl_cr(c)) #define eputc(c) putc(swap_nl_cr (c),stderr) -inline void oputs(const char *s) {while (*s) {oputc(*s);s++;}} -inline void eputs(const char *s) {while (*s) {eputc(*s);s++;}} #endif void w_print_char (char c) @@ -524,11 +522,19 @@ void w_print_text (char *s,unsigned long length) int l; l=length; - if (l) + if (l){ + flockfile (stdout); + do { - oputc (*s); + int c; + + c=*s; + putchar_unlocked (swap_nl_cr (c)); ++s; } while (--l); + + funlockfile (stdout); + } return; } #endif @@ -686,10 +692,17 @@ void ew_print_text (char *s,unsigned long length) l=length; if (l){ + flockfile (stderr); + do { - eputc (*s); + int c; + + c=*s; + putc (swap_nl_cr (c),stderr); ++s; } while (--l); + + funlockfile (stderr); } return; } @@ -902,7 +915,7 @@ int w_get_char (void) #ifdef MAYBE_USE_STDIO if (use_stdio){ c=getchar(); - return swap_nl_cr(c); + return swap_nl_cr (c); } #endif @@ -938,6 +951,80 @@ int w_get_char (void) return c; } +unsigned long w_get_string (char *string,unsigned long max_length) +{ + unsigned long length; + +#ifdef MAYBE_USE_STDIO + if (use_stdio){ + int i; + + length=fread (string,1,max_length,stdin); + + for (i=0; i #else # define USE_CLIB 0 #endif @@ -1115,26 +1116,16 @@ unsigned long file_read_characters (struct file *f,unsigned long *length_p,char if (f==file_table) IO_error ("freads: can't read from stderr"); else if (f==&file_table[1]){ - char *string; unsigned long length; - - length=0; - -#if OLD_READ_STRING - string=s->characters; -#else - string=s; -#endif - while (length!=max_length){ - *string++=w_get_char(); - ++length; - } #if OLD_READ_STRING + length=w_get_string (s->characters,max_length); s->length=length; #else + length=w_get_string (s,max_length); *length_p=length; #endif + return length; } else IO_error ("freads: can't open this file"); @@ -1242,23 +1233,9 @@ unsigned long file_read_line (struct file *f,unsigned long max_length,char *stri if (is_special_file (f)){ if (f==file_table) IO_error ("freadline: can't read from stderr"); - else if (f==&file_table[1]){ - unsigned long length; - - length=0; - - while (length!=max_length){ - int c; - - c=w_get_char(); - *string++=c; - ++length; - if (c==NEWLINE_CHAR) - return length; - } - - return -1; - } else + else if (f==&file_table[1]) + return w_get_line (string,max_length); + else IO_error ("freadline: can't open this file"); } else { unsigned char *end_string,*begin_string; @@ -2694,6 +2671,26 @@ void er_print_int (int i) } } +void er_print_real (double r) +{ +#if MACOSX + if (!(flags & WRITE_STDERR_TO_FILE_MASK)) + ew_print_real (r); + else { +#else + ew_print_real (r); + + if (flags & WRITE_STDERR_TO_FILE_MASK){ +#endif + if (file_table[3].file_mode==0){ + if (open_stderr_file_failed || !open_stderr_file()) + return; + } + + file_write_real (r,&file_table[3]); + } +} + static void write_chars (unsigned char *p,unsigned char *end_p,struct file *f) { while (p