2

I have a C++ program and I want to determine whether the parent process is a terminal or not. Because if it is a terminal, I can use escape codes to highlight the output, else this will be displayed with unreadable characters. The solution need to run on any posix system.

If anyone knows the solution for Java I would be interested in it as well.

msrd0
  • 7,816
  • 9
  • 47
  • 82
  • I think there's something in POSIX that can do this, but I can't recall the specifics. Possibly `fcntl()`? I didn't see anything relevant in the man entry though, so maybe it's something else. – celticminstrel Jul 18 '15 at 17:15
  • @celticminstrel according to the man page `fcntl` stands for `file control` and doesn't seem to be what I want – msrd0 Jul 18 '15 at 17:22
  • I'm sorry, but *parent process is a terminal* is just non sense. Do you mean *standard output is a terminal*, *parent process is an interactive shell* or anything else ? – Serge Ballesta Jul 18 '15 at 17:27

1 Answers1

6

If I understand your problem correctly, you're looking at it from the wrong angle. Ask not what your parent process is, but rather what the capabilities of stdout are. And to do that in a POSIX environment, use isatty().

For stdout, isatty(STDOUT_FILENO) should return 1.

In Java, System.console() will apparently return a Console object if both stdin and stdout are a terminal, and null otherwise. See How can I check if a Java program's input/output streams are connected to a terminal? for more details.

user3553031
  • 5,990
  • 1
  • 20
  • 40