aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2018-09-20 15:52:24 -0700
committerH. Peter Anvin <hpa@zytor.com>2018-09-20 15:52:53 -0700
commit92801a5fb46db72240912786be5706c326fa2372 (patch)
tree930a9540a553e9d18e8a9f2947c5047ba014ff10
parent79c9eae9775941914729272b0795efac10c257c6 (diff)
downloadabc80sim-92801a5fb46db72240912786be5706c326fa2372.tar.gz
abc80sim-92801a5fb46db72240912786be5706c326fa2372.tar.xz
abc80sim-92801a5fb46db72240912786be5706c326fa2372.zip
console: improve the Windows implementation
Improve the Windows implementation; it now seems to work correctly-ish (we still end up reattaching to a console which has already been abandoned; much like ending with an & in Unix.)
-rw-r--r--console.c28
1 files changed, 16 insertions, 12 deletions
diff --git a/console.c b/console.c
index e731c9b..4e5d19b 100644
--- a/console.c
+++ b/console.c
@@ -8,22 +8,23 @@
#if defined(HAVE_ATTACHCONSOLE) || !defined(HAVE_DAEMON)
-static int redirect_stdio(const char *whereto)
+static int redirect_stdio(const char *from, const char *to)
{
- int fd = open(whereto, O_RDWR);
- if (fd < 0)
- return -1;
+ bool err = false;
fflush(NULL);
- dup2(fd, STDERR_FILENO);
- dup2(fd, STDOUT_FILENO);
- dup2(fd, STDIN_FILENO);
+ if (!freopen(from, "r+t", stdin))
+ err = !freopen(from, "r+t", stdin);
- if (fd > STDERR_FILENO)
- close(fd);
+ if (!freopen(to, "w+t", stdout))
+ err |= !freopen(to, "wt", stdout);
- return 0;
+ if (!freopen(to, "w+t", stderr))
+ err |= !freopen(to, "wt", stderr);
+ setvbuf(stderr, NULL, _IONBF, 0);
+
+ return -err;
}
#endif
@@ -38,13 +39,16 @@ void attach_console(void)
if (!AttachConsole(ATTACH_PARENT_PROCESS))
return; /* Attach failed */
- if (redirect_stdio("\\\\?\\CON:"))
+ if (redirect_stdio("CONIN$", "CONOUT$"))
detach_console();
+
+ /* We are probably displaying a command prompt, so start with a newline */
+ putchar('\n');
}
void detach_console(void)
{
- redirect_stdio("\\\\?\\NUL:");
+ redirect_stdio("\\Device\\Null", "\\Device\\Null");
FreeConsole();
}