[comp.lang.c] stdin and UNIX pipes

nr3m@unix.cis.pitt.edu (Matthew A Henry) (02/18/91)

Hi,

I am writing a C program in a UNIX environment that will receive input
from other programs through a pipe, causing the shell to change the
default assignment of stdin.  At some point in the program I would like
to request input from the original stdin (keyboard).  Any thoughts on
how I could do this, e-mailed or posted, would be greatly appreciated.

Thanks,
Matt Henry

nr3m@unix.cis.pitt.edu

rjohnson@shell.com (Roy Johnson) (02/20/91)

In article <92993@unix.cis.pitt.edu> nr3m@unix.cis.pitt.edu (Matthew A Henry) writes:
>I am writing a C program in a UNIX environment that will receive input
>from other programs through a pipe, causing the shell to change the
>default assignment of stdin.  At some point in the program I would like
>to request input from the original stdin (keyboard).  Any thoughts on
>how I could do this, e-mailed or posted, would be greatly appreciated.

The file "/dev/tty" should be your keyboard for input (it is on my Sun).
I think you want something like this:

#include <stdio.h>

main()
{
  int i;

  if (freopen("newfile", "r", stdin) != stdin) {
    perror("freopen");
    exit(1);
  }
  /* Get 50 chars from "newfile" */
  for(i=0; i<50; ++i)
    putchar(getchar());

/* Change back to stdin */
  if (freopen("/dev/tty", "r", stdin) != stdin) {
    perror("freopen");
    exit(1);
  }
  /* Get 50 chars from keyboard */
  for(i=0; i<50; ++i)
    putchar(getchar());
}
--
======= !{sun,psuvax1,bcm,rice,decwrl,cs.utexas.edu}!shell!rjohnson =======
Feel free to correct me, but don't preface your correction with "BZZT!"
Roy Johnson, Shell Development Company

gwyn@smoke.brl.mil (Doug Gwyn) (02/21/91)

In article <1991Feb20.210406.21117@csrd.uiuc.edu> bliss@sp64.csrd.uiuc.edu (Brian Bliss) writes:
>saying
>  fopen ("/dev/tty", "r");
>always opens the screen for input, regardless of the state of stdin.

Wrong, although close for most UNIX implementations.

>obvoulsy, somewhere (in libc.a?) there is code to check for the
>string "/dev/tty", and open /dev/tty/tty?? instead of trying
>to open the directory /dev/tty.

Wrong again.  Usually on UNIX "/dev/tty" is the name of a "character
special file" that has no obvious connection to the other terminal
device names nor the kernel support for them.  The kernel knows how
to handle /dev/tty and does so.  This should be documented in your
UNIX Programmer's (or Administrator's) Reference Manual under TTY(4)
or TTY(7).

pfalstad@phoenix.Princeton.EDU (Paul Falstad) (02/21/91)

bliss@sp64.csrd.uiuc.edu (Brian Bliss) wrote:
>saying
>
>  fopen ("/dev/tty", "r");
>
>always opens the screen for input, regardless of the state of stdin.
>obvoulsy, somewhere (in libc.a?) there is code to check for the
>string "/dev/tty", and open /dev/tty/tty?? instead of trying
>to open the directory /dev/tty.

Not quite.

1. it opens the _tty_ for input, _if_ your process has a controlling
terminal.
2. there's no such directory /dev/tty.  At least not on my system.
It might have been a good idea, but a tad unportable since /dev/tty
already exists.
3. the /dev/tty -> /dev/tty?? is done in the kernel by checking
minor device numbers, not strings.  It is not in libc; typing
open("/dev/tty",2) opens a tty without touching any libraries
(except for the actual trap code, of course).

I'd redirect followups to a more appropriate group, like
comp.unix.programmer, but I don't want them there either.  :-)

--
  Paul Falstad, pfalstad@phoenix.princeton.edu PLink:HYPNOS GEnie:P.FALSTAD
  I think there should be more race prejudice.  <slap> LESS race prejudice.
     Princeton University apologizes for the content of this article.