Unfortunately there's no simple (internal) way to find out the name of the controlling
terminal because ctermid
always returns "/dev/tty" (in my experience.) This isn't a symlink to the "real" terminal, either.
Instead, you pretty much have to settle for determining the terminal name of standard input. This isn't really of use if you setsid
your program but start it from a terminal because you will show the name of a terminal that the process is mutually out of control of.
If you want the real name of the controlling terminal, you're best off calling ps
fprintf(stderr, "%s\n", ttyname(STDIN_FILENO));
Sorry, I think I misunderstood your question.