Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1 |
both |
#define _POSIX_SOURCE
#include <unistd.h>
char *ttyname(int fildes);
Returns a string containing the path name of the terminal associated with the given file descriptor, fildes. Subsequent calls to ttyname() may overwrite this string, because the pointer returned may point to static data.
If successful, ttyname() returns a string containing a path name.
If unsuccessful because fildes is not a terminal, or the path name cannot be determined, ttyname() returns a NULL pointer.
⁄* CELEBT16
This example provides the pathname of the terminal
associated with stdin.
*⁄
#define _POSIX_SOURCE
#include <unistd.h>
#include <stdio.h>
main() {
char *ret, tty[40];
if ((ret = ttyname(STDIN_FILENO)) == NULL)
perror("ttyname() error");
else {
strcpy(tty, ret);
printf("The ttyname associated with my stdin is %s\n", tty);
}
}
The ttyname associated with my stdin is /dev/ttyp0000