3

I'm looking for how to get the name of the terminal emulator (« aterm », « xterm », « konsole », etc.) with C Programming langage (GNU/Linux). I have done several researches, but I didn't find anything.

md5
  • 23,373
  • 3
  • 44
  • 93
  • 1
    Deleted my answer because it didn't turn out to be as reliable as I thought. Why do you need to know the terminal emulator? – Fred Foo May 09 '12 at 16:19
  • @FredFoo you should undelete the answer, and leave it for historical purposes. – MD XF Mar 10 '17 at 04:47

5 Answers5

4

I doubt there's a reliable way to check this.

As @larsmans suggested, you can check the TERM env variable, but most emulators use the same terminal setting..

I would check the parent process id (getppid) and its parent ( linux: programmatically get parent pid of another process? ) and so on till you find a process with a name that looks like a terminal emulator...

Or, if you have pstree simply process its output:

# pstree -As 12652
init---screen---bash---pstree

(sorry, I don't have X terminal so I cannot show a proper example)

Anyway, none of these are totally reliable but probably will work.

Community
  • 1
  • 1
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
4

The name of the terminal is given by the TERM environment variable.

char *termtype = getenv("TERM");
printf("The terminal type is %s\n", termtype);
LeoNerd
  • 8,344
  • 1
  • 29
  • 36
1

See if this works. I tested it from Emacs, and in urxvt, xterm, and rxvt.

#!/bin/zsh

terminal_emulator_parents=`pstree -As $$`
tep_list=`echo $terminal_emulator_parents | tr -s "-" | tr "-" " " \
          | tac -s' ' | tr '\n' ' '`

found="false"
for process in `echo $tep_list`; do
    if [[ $process =~ ("urxvt"|"xterm"|"rxvt") ]]; then # here: add all
        found="true"                                    # terminal emulators
        break                                           # you can think of
    fi
done

if [[ $found == "true" ]]; then
    echo "Terminal emulator: $process"
else
    echo "Couldn't determine the terminal emulator."
fi
Emanuel Berg
  • 499
  • 4
  • 14
0

Building off the prior two answers with pstree, I created an environment variable to identify the terminal emulator type in my .bashrc, then in my script, I can access that information.

I use this to set my guake tab title to the current virtualenvironment using postactivate and clear it in postdeactivate, but I don't want guake to be invoked if I happen to enter a virtualenvironment when I'm using a different terminal program.

Here's my code:

in .bashrc

export TERM_TYPE=`pstree -As $$ | awk -F "---" '{print $2}'`

in postdeactivate or postactivate (replace "Terminal" with the desired tab name)

#!/bin/bash
# This hook is run after every virtualenv is deactivated.

if [ "$TERM_TYPE" == "guake" ]; then
  guake -r Terminal
fi
Dannid
  • 1,507
  • 1
  • 20
  • 17
0

There are the functions: ctermid() and gettyname()

ctermid returns the name in the specified string

getttyname works on the selected file device