From eb639c500494ee8c4ca8978c50451dca10112744 Mon Sep 17 00:00:00 2001 From: Daniel Jacobowitz Date: Thu, 11 Oct 2007 18:44:07 +0000 Subject: [PATCH] 2007-10-11 Jesper Nilsson * callback.c (cb_is_stdin, cb_is_stdout, cb_is_stderr): Add functions. * syscall.c (cb_syscall): Test for stdin/out/err, not just fd 0/1/2. 2007-10-11 Jesper Nilsson * callback.h (cb_is_stdin, cb_is_stdout, cb_is_stderr): Add prototypes. 2007-10-11 Jesper Nilsson * sim/cris/c/freopen2.c: Added testcase. --- include/gdb/ChangeLog | 2 +- include/gdb/callback.h | 4 ++- sim/common/ChangeLog | 4 +-- sim/common/callback.c | 11 ++++++++ sim/common/syscall.c | 4 +-- sim/testsuite/ChangeLog | 4 +++ sim/testsuite/sim/cris/c/freopen1.c | 5 +--- sim/testsuite/sim/cris/c/freopen2.c | 40 +++++++++++++++++++++++++++++ 8 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 sim/testsuite/sim/cris/c/freopen2.c diff --git a/include/gdb/ChangeLog b/include/gdb/ChangeLog index 70266e2ccf..67e5d970d5 100644 --- a/include/gdb/ChangeLog +++ b/include/gdb/ChangeLog @@ -1,6 +1,6 @@ 2007-10-11 Jesper Nilsson - * callback.h (cb_is_stdin): Add prototype. + * callback.h (cb_is_stdin, cb_is_stdout, cb_is_stderr): Add prototypes. 2007-08-23 Joel Brobecker diff --git a/include/gdb/callback.h b/include/gdb/callback.h index 2245c87dc4..eb991f74ec 100644 --- a/include/gdb/callback.h +++ b/include/gdb/callback.h @@ -315,8 +315,10 @@ int cb_host_to_target_stat PARAMS ((host_callback *, const struct stat *, PTR)); /* Translate a value to target endian. */ void cb_store_target_endian PARAMS ((host_callback *, char *, int, long)); -/* Test if the fd is stdin. */ +/* Tests for special fds. */ int cb_is_stdin PARAMS ((host_callback *, int)); +int cb_is_stdout PARAMS ((host_callback *, int)); +int cb_is_stderr PARAMS ((host_callback *, int)); /* Perform a system call. */ CB_RC cb_syscall PARAMS ((host_callback *, CB_SYSCALL *)); diff --git a/sim/common/ChangeLog b/sim/common/ChangeLog index bd09a5c536..87bc0ee2f2 100644 --- a/sim/common/ChangeLog +++ b/sim/common/ChangeLog @@ -1,7 +1,7 @@ 2007-10-11 Jesper Nilsson - * callback.c (cb_is_stdin): Add. - * syscall.c (cb_syscall): Test for stdin, not just fd 0. + * callback.c (cb_is_stdin, cb_is_stdout, cb_is_stderr): Add functions. + * syscall.c (cb_syscall): Test for stdin/out/err, not just fd 0/1/2. 2007-08-10 Nick Clifton diff --git a/sim/common/callback.c b/sim/common/callback.c index f83dd547c2..18bcc80d4d 100644 --- a/sim/common/callback.c +++ b/sim/common/callback.c @@ -1143,3 +1143,14 @@ cb_is_stdin (host_callback *cb, int fd) return fdbad (cb, fd) ? 0 : fdmap (cb, fd) == 0; } +int +cb_is_stdout (host_callback *cb, int fd) +{ + return fdbad (cb, fd) ? 0 : fdmap (cb, fd) == 1; +} + +int +cb_is_stderr (host_callback *cb, int fd) +{ + return fdbad (cb, fd) ? 0 : fdmap (cb, fd) == 2; +} diff --git a/sim/common/syscall.c b/sim/common/syscall.c index a3e7560bf4..b00244882f 100644 --- a/sim/common/syscall.c +++ b/sim/common/syscall.c @@ -344,12 +344,12 @@ cb_syscall (cb, sc) errcode = EINVAL; goto FinishSyscall; } - if (fd == 1) + if (cb_is_stdout(cb, fd)) { result = (int) (*cb->write_stdout) (cb, buf, bytes_read); (*cb->flush_stdout) (cb); } - else if (fd == 2) + else if (cb_is_stderr(cb, fd)) { result = (int) (*cb->write_stderr) (cb, buf, bytes_read); (*cb->flush_stderr) (cb); diff --git a/sim/testsuite/ChangeLog b/sim/testsuite/ChangeLog index c7c0acca51..168b05d9e4 100644 --- a/sim/testsuite/ChangeLog +++ b/sim/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2007-10-11 Jesper Nilsson + + * sim/cris/c/freopen2.c: Added testcase. + 2006-10-02 Hans-Peter Nilsson Edgar E. Iglesias diff --git a/sim/testsuite/sim/cris/c/freopen1.c b/sim/testsuite/sim/cris/c/freopen1.c index eeb6079fdb..0e0f28d961 100644 --- a/sim/testsuite/sim/cris/c/freopen1.c +++ b/sim/testsuite/sim/cris/c/freopen1.c @@ -1,7 +1,4 @@ -/* Check that basic freopen functionality works. -#xfail: *-*-* - Currently doesn't work, because syscall.c:cb_syscall case - CB_SYS_write intercepts writes to fd 1 and 2. */ +/* Check that basic freopen functionality works. */ #include #include diff --git a/sim/testsuite/sim/cris/c/freopen2.c b/sim/testsuite/sim/cris/c/freopen2.c new file mode 100644 index 0000000000..3959607db8 --- /dev/null +++ b/sim/testsuite/sim/cris/c/freopen2.c @@ -0,0 +1,40 @@ +/* Tests that stdin can be redirected from a normal file. */ +#include +#include +#include + +int +main (void) +{ + const char* fname = "freopen.dat"; + const char tsttxt[] + = "A random line of text, used to test correct freopen etc.\n"; + FILE* instream; + FILE *old_stderr; + char c1; + + /* Like the freopen call in flex. */ + old_stderr = freopen (fname, "w+", stderr); + if (old_stderr == NULL + || fwrite (tsttxt, 1, strlen (tsttxt), stderr) != strlen (tsttxt) + || fclose (stderr) != 0) + { + printf ("fail\n"); + exit (1); + } + + instream = freopen(fname, "r", stdin); + if (instream == NULL) { + printf("fail\n"); + exit(1); + } + + c1 = getc(instream); + if (c1 != 'A') { + printf("fail\n"); + exit(1); + } + + printf ("pass\n"); + exit(0); +} -- 2.34.1