LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   HOW TO get TTY id from a c programm (https://www.linuxquestions.org/questions/programming-9/how-to-get-tty-id-from-a-c-programm-728009/)

dinob 05-23-2009 09:55 AM

HOW TO get TTY id from a c programm
 
Hi experts,

I am new to linux/c programming and I am trying to get TTY id from my c program. Something like this:

int TTYID = get_ttyid();

How do I do that?

Regards,
_dino_

harry edwards 05-23-2009 02:10 PM

See man ttyname. Sample follows:

Code:

#include <stdio.h>
#include <sys/types.h>


static const char tty_usage[] = "tty\n"
#ifndef BB_FEATURE_TRIVIAL_HELP
        "\nPrint the file name of the terminal connected to standard input.\n\n"
        "Options:\n"
        "\t-s\tprint nothing, only return an exit status\n"
#endif
        ;

extern int tty_main(int argc, char **argv)
{
        char *tty;

        if (argc > 1) {
                if (argv[1][0] != '-' || argv[1][1] != 's')
                        usage(tty_usage);
        } else {
                tty = ttyname(0);
                if (tty)
                        puts(tty);
                else
                        puts("not a tty");
        }
        return(isatty(0) ? TRUE : FALSE);
}



All times are GMT -5. The time now is 04:17 PM.