gencon
1
Since the existence of /dev/tty is not guaranteed, what happens when an attempt is made to open /dev/tty and there's no controlling terminal?
Will it fail, or open /dev/null instead? Or do something else?
So is checking for NULL in the code below a safe way of checking whether opening /dev/tty was sucessful? If not how should it be checked?
FILE* fp = fopen("/dev/tty", "r");
if (fp == NULL)
OopsBetterDoSomething();
int c = fgetc(fp);
fclose(fp);
Many thanks all.
Checking to see that the stream pointer is NULL/not NULL is okay.
What happens in the case that stdin is redirected, which is, I think, what you may want.
try something like this:
File *in=NULL;
if( isatty(0) )
in=fopen("dev/tty", "r");
else
in=stdin;
if(in==NULL)
barf();
Your best bet is to refer to Steven's "Advanced Programming in the UNIX Environment"
gencon
3
Thanks Jim.
The code I posted is the code to handle 'what happens if stdin is redirected'. My call to isatty() is just above the snippet I posted.
Cheers.