Operating System - HP-UX
1843977 Members
1622 Online
110226 Solutions
New Discussion

Re: How to get terminal name from C program?

 
SOLVED
Go to solution
Subbu Cherukuwada
Occasional Contributor

How to get terminal name from C program?

Hi,
I want to know the terminal name where a C program is running, within the same program i.e. something like the output of tty command?
1 REPLY 1
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to get terminal name from C program?

Hi,

No problem, use ttyname. Man ttyname for details.

Example:

#include

#ifndef STDIN
#define STDIN 0
#endif

char *tty_device = NULL;

tty_device = ttyname(STDIN);

You should also use isatty() (man isatty) to make sure that stdin has not be redirected.

if (isatty(STDIN))
{
(void) printf("I'm a terminal\");
}
else
{
(void) printf("I ain't no stinkin terminal");
}

This should do it, Clay

If it ain't broke, I can fix that.