Go to main content

man pages section 3: Basic Library Functions

Exit Print View

Updated: Wednesday, July 27, 2022



ttyname, ttyname_r - find pathname of a terminal


#include <unistd.h> 

char *ttyname(int fildes);
int ttyname_r(int fildes, char *name, size_t namesize);
POSIX.1c Draft 6
  cc [ flag...] file ... –D__USE_DRAFT6_PROTOTYPES__ [ library ... ]
char *ttyname_r(int fildes, char *name, int namelen);


The ttyname() function returns a pointer to a string containing the null-terminated path name of the terminal device associated with file descriptor fildes. The return value points to thread–specific data whose content is overwritten by each call from the same thread.

The ttyname_r() function has the same functionality as ttyname() except that the caller must supply a buffer name with size namesize to store the result; this buffer must be at least _POSIX_PATH_MAX in size (defined in <limits.h>).

Return Values

Upon successful completion, ttyname() returns a pointer to a string. Otherwise, a null pointer is returned and errno is set to indicate the error.

The ttyname_r() function returns 0 if successful or the error number upon failure; it does not set errno.


The ttyname() and ttyname_r() functions may fail if:


The fildes argument is not a valid file descriptor. This condition is reported.


The fildes argument does not refer to a terminal device. This condition is reported.

The ttyname_r() function may fail if:


The value of namesize is smaller than the length of the string to be returned including the terminating null character.


See attributes(7) for descriptions of the following attributes:

Interface Stability

See Also

gettext(3C), setlocale(3C), ttysrch(5), attributes(7), standards(7)


Messages printed from this function are in the native language specified by the LC_MESSAGES locale category. See setlocale(3C).

The return value of ttyname() points to thread–specific data whose content is overwritten by each call from the same thread. This function is safe to use in multithreaded applications, but its use is discouraged. The ttyname_r() function should used instead.

Prior to Oracle Solaris 11.4, the default compilation environment provided a definition of the ttyname_r() function as specified in POSIX.1c Draft 6. The final POSIX.1c standard changed the interface for ttyname_r(). To allow applications that were written to use the obsolete Draft-6 interfaces to continue to be compiled and run, the __USE_DRAFT6_PROTOTYPES__ macro must be defined:


Support for the Draft-6 interfaces is provided for source compatibility only and might not be supported in future releases. Old applications should be converted to use the standard definitions.