public inbox for newlib-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: newlib-cvs@sourceware.org
Subject: [newlib-cygwin/main] RISC-V: fix setting up std streams in init_semihosting()
Date: Wed, 10 Jan 2024 09:47:13 +0000 (GMT)	[thread overview]
Message-ID: <20240110094713.6C7DE38582B3@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=c419bbb341528a97d2de88539caf200c9dec1ffb

commit c419bbb341528a97d2de88539caf200c9dec1ffb
Author:     Venkata Ramanaiah Nalamothu <quic_vnalamot@quicinc.com>
AuthorDate: Wed Jan 3 12:22:16 2024 +0530
Commit:     Corinna Vinschen <corinna@vinschen.de>
CommitDate: Wed Jan 10 10:46:56 2024 +0100

    RISC-V: fix setting up std streams in init_semihosting()
    
    Currently init_semihosting() assumes the return value from _open()
    call as the file descriptor handle and that is incorrect.
    
    The semihost _open() call returns the fdtable index returned by the
    __add_fdentry() for the file opened.

Diff:
---
 libgloss/riscv/semihost-sys_fdtable.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/libgloss/riscv/semihost-sys_fdtable.c b/libgloss/riscv/semihost-sys_fdtable.c
index 152c92d1513b..d62e3f9708fe 100644
--- a/libgloss/riscv/semihost-sys_fdtable.c
+++ b/libgloss/riscv/semihost-sys_fdtable.c
@@ -24,25 +24,26 @@ static struct fdentry fdtable[RISCV_MAX_OPEN_FILES];
 void __attribute__ ((constructor))
 init_semihosting ()
 {
-  int handle;
+  int i;
 
-  for (int i=0; i<RISCV_MAX_OPEN_FILES; i++)
+  for (i=0; i<RISCV_MAX_OPEN_FILES; i++)
     fdtable[i].handle = -1;
 
-  /* Set up std streams.  */
+  /* Set up std streams. Note that the semihost _open() call returns an index
+     into the fdtable.  */
   /* stdin.  */
-  handle = _open (":tt", O_RDONLY);
-  fdtable[STDIN_FILENO].handle = handle;
+  i = _open (":tt", O_RDONLY);
+  fdtable[STDIN_FILENO].handle = fdtable[i].handle;
   fdtable[STDIN_FILENO].pos = 0;
 
   /* stdout.  */
-  handle = _open (":tt", O_WRONLY|O_CREAT|O_TRUNC);
-  fdtable[STDOUT_FILENO].handle = handle;
+  i = _open (":tt", O_WRONLY|O_CREAT|O_TRUNC);
+  fdtable[STDOUT_FILENO].handle = fdtable[i].handle;
   fdtable[STDOUT_FILENO].pos = 0;
 
   /* stderr.  */
-  handle = _open (":tt", O_WRONLY|O_CREAT|O_APPEND);
-  fdtable[STDERR_FILENO].handle = handle;
+  i = _open (":tt", O_WRONLY|O_CREAT|O_APPEND);
+  fdtable[STDERR_FILENO].handle = fdtable[i].handle;
   fdtable[STDERR_FILENO].pos = 0;
 }

                 reply	other threads:[~2024-01-10  9:47 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240110094713.6C7DE38582B3@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=newlib-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).