You can use the ctermid
function to get a file name that you can
use to open the controlling terminal. In the GNU library, it returns
the same string all the time: "/dev/tty"
. That is a special
“magic” file name that refers to the controlling terminal of the
current process (if it has one). To find the name of the specific
terminal device, use ttyname
; see Is It a Terminal.
The function ctermid
is declared in the header file
stdio.h.
The
ctermid
function returns a string containing the file name of the controlling terminal for the current process. If string is not a null pointer, it should be an array that can hold at leastL_ctermid
characters; the string is returned in this array. Otherwise, a pointer to a string in a static area is returned, which might get overwritten on subsequent calls to this function.An empty string is returned if the file name cannot be determined for any reason. Even if a file name is returned, access to the file it represents is not guaranteed.
The value of this macro is an integer constant expression that represents the size of a string large enough to hold the file name returned by
ctermid
.
See also the isatty
and ttyname
functions, in
Is It a Terminal.