summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn van Groningen2004-06-25 12:53:02 +0000
committerJohn van Groningen2004-06-25 12:53:02 +0000
commitf751af1a0ff0307dce00332a9057bfa6aa47aa34 (patch)
tree4217b63174b404aa39f30d85fbd0757d685bdbb9
parentfix freads and freadline if stdin is redirected, use buffered io if stdin or (diff)
fix freads and freadline for redirected stdio on macho,
use _unlocked functions for reading and writing strings
-rw-r--r--mcon.c123
-rw-r--r--mcon.h2
-rw-r--r--mfileIO3.c57
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<length; ++i){
+ int c;
+
+ c=string[i];
+ string[i]=swap_nl_cr (c);
+ }
+
+ return length;
+ }
+#endif
+
+ length=0;
+ while (length!=max_length){
+ *string++=w_get_char();
+ ++length;
+ }
+
+ return length;
+}
+
+unsigned long w_get_line (char *string,unsigned long max_length)
+{
+ unsigned long length;
+
+#ifdef MAYBE_USE_STDIO
+ if (use_stdio){
+ int c;
+ length=0;
+
+ flockfile (stdin);
+
+ while (length!=max_length && (c=getchar_unlocked(),c!=EOF)){
+ *string++=c;
+ ++length;
+ if (c=='\n'){
+ funlockfile (stdin);
+ return length;
+ }
+ }
+
+ funlockfile (stdin);
+
+ if (c!=EOF)
+ return -1;
+
+ return length;
+ }
+#endif
+
+ length=0;
+
+ while (length!=max_length){
+ int c;
+
+ c=w_get_char();
+ *string++=c;
+ ++length;
+ if (c==NEWLINE_CHAR)
+ return length;
+ }
+
+ return -1;
+}
+
#define is_digit(n) ((unsigned)((n)-'0')<(unsigned)10)
int w_get_int (int *i_p)
@@ -1164,7 +1251,17 @@ void w_print_string (char *s)
#ifdef MAYBE_USE_STDIO
if (use_stdio){
- oputs (s);
+ int c;
+
+ flockfile (stdin);
+
+ while ((c=*s)!='\0'){
+ putchar_unlocked (swap_nl_cr (c));
+ ++s;
+ }
+
+ funlockfile (stdin);
+
return;
}
#endif
@@ -1206,7 +1303,17 @@ void ew_print_string (char *s)
#ifdef MAYBE_USE_STDIO
if (use_stdio){
- eputs (s);
+ int c;
+
+ flockfile (stderr);
+
+ while ((c=*s)!='\0'){
+ putc_unlocked (swap_nl_cr (c),stderr);
+ ++s;
+ }
+
+ funlockfile (stderr);
+
return;
}
#endif
diff --git a/mcon.h b/mcon.h
index 1a17c38..414d939 100644
--- a/mcon.h
+++ b/mcon.h
@@ -1,7 +1,9 @@
extern int w_get_char();
extern int w_get_int (int *i_p);
extern int w_get_real (double *r_p);
+extern unsigned long w_get_string (char *string,unsigned long max_length);
extern unsigned long w_get_text (char *string,unsigned long max_length);
+extern unsigned long w_get_line (char *string,unsigned long max_length);
extern void w_print_char (char c);
extern void w_print_int (int i);
extern void w_print_real (double r);
diff --git a/mfileIO3.c b/mfileIO3.c
index 8e65d0f..a23f9f3 100644
--- a/mfileIO3.c
+++ b/mfileIO3.c
@@ -8,6 +8,7 @@
#if defined(powerc) || defined (MACHO)
# define USE_CLIB 1
+# include <stdio.h>
#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<end_p){