unix question about stdin

John Kaminski ugkamins at sunybcs.uucp
Sat Apr 22 15:19:13 AEST 1989


The original post wanted to know how to reconnect the stdin to the terminal
after it had been attached to a file, assumedly through redirection.  What I
just tried on a BSD system, and works:

  fclose(stdin);
  *stdin = *fopen("/dev/tty", "r");

The "*" before stdin is necessary because of the way stdin is defined in
<stdio.h>.  Another method that SHOULD work is akin to the way I've seen
some shell code do it:

  close(0);
  dup2(0, open("/dev/tty", O_RDONLY, 0x700));

Of course, this doesn't check for a failed open() call.  Also, I guess:

  close(0);
  dup(open("/dev/tty", O_RDONLY, 0x700));

SHOULD work.  Note that any of read, write, or read/write should be OK, as
well as many different file modes (last arg to open()).



More information about the Comp.unix.wizards mailing list