| 1 |
'\" t |
| 2 |
.\" Copyright 1995, James R. Van Zandt <[email protected]> |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
.\" |
| 7 |
.TH ttyname 3 2026-02-08 "Linux man-pages 6.18" |
| 8 |
.SH NAME |
| 9 |
ttyname, ttyname_r \- return name of a terminal |
| 10 |
.SH LIBRARY |
| 11 |
Standard C library |
| 12 |
.RI ( libc ,\~ \-lc ) |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
.B #include <unistd.h> |
| 16 |
.P |
| 17 |
.BI "char *ttyname(int " fd ); |
| 18 |
.BR "int ttyname_r(" "size_t size;" |
| 19 |
.BI " int " fd ", char " buf [ size "], size_t " size ); |
| 20 |
.fi |
| 21 |
.SH DESCRIPTION |
| 22 |
The function |
| 23 |
.BR ttyname () |
| 24 |
returns a pointer to the null-terminated pathname of the terminal device |
| 25 |
that is open on the file descriptor |
| 26 |
.IR fd , |
| 27 |
or NULL on error |
| 28 |
(for example, |
| 29 |
if |
| 30 |
.I fd |
| 31 |
is not connected to a terminal). |
| 32 |
The return value may point to static data, possibly overwritten by the |
| 33 |
next call. |
| 34 |
The function |
| 35 |
.BR ttyname_r () |
| 36 |
stores this pathname in the buffer |
| 37 |
.I buf |
| 38 |
of size |
| 39 |
.IR size . |
| 40 |
.SH RETURN VALUE |
| 41 |
The function |
| 42 |
.BR ttyname () |
| 43 |
returns a pointer to a pathname on success. |
| 44 |
On error, NULL is returned, and |
| 45 |
.I errno |
| 46 |
is set to indicate the error. |
| 47 |
The function |
| 48 |
.BR ttyname_r () |
| 49 |
returns 0 on success, and an error number upon error. |
| 50 |
.SH ERRORS |
| 51 |
.TP |
| 52 |
.B EBADF |
| 53 |
Bad file descriptor. |
| 54 |
.TP |
| 55 |
.\" glibc commit 15e9a4f378c8607c2ae1aa465436af4321db0e23 |
| 56 |
.B ENODEV |
| 57 |
.I fd |
| 58 |
refers to a slave pseudoterminal device |
| 59 |
but the corresponding pathname could not be found (see NOTES). |
| 60 |
.TP |
| 61 |
.B ENOTTY |
| 62 |
.I fd |
| 63 |
does not refer to a terminal device. |
| 64 |
.TP |
| 65 |
.B ERANGE |
| 66 |
.RB ( ttyname_r ()) |
| 67 |
.I size |
| 68 |
was too small to allow storing the pathname. |
| 69 |
.SH ATTRIBUTES |
| 70 |
For an explanation of the terms used in this section, see |
| 71 |
.BR attributes (7). |
| 72 |
.TS |
| 73 |
allbox; |
| 74 |
lbx lb lb |
| 75 |
l l l. |
| 76 |
Interface Attribute Value |
| 77 |
T{ |
| 78 |
.na |
| 79 |
.nh |
| 80 |
.BR ttyname () |
| 81 |
T} Thread safety MT-Unsafe race:ttyname |
| 82 |
T{ |
| 83 |
.na |
| 84 |
.nh |
| 85 |
.BR ttyname_r () |
| 86 |
T} Thread safety MT-Safe |
| 87 |
.TE |
| 88 |
.SH STANDARDS |
| 89 |
POSIX.1-2008. |
| 90 |
.SH HISTORY |
| 91 |
POSIX.1-2001, 4.2BSD. |
| 92 |
.SH NOTES |
| 93 |
A process that keeps a file descriptor that refers to a |
| 94 |
.BR pts (4) |
| 95 |
device open when switching to another mount namespace that uses a different |
| 96 |
.I /dev/ptmx |
| 97 |
instance may still accidentally find that a device path of the same name |
| 98 |
for that file descriptor exists. |
| 99 |
However, this device path refers to a different device and thus |
| 100 |
can't be used to access the device that the file descriptor refers to. |
| 101 |
Calling |
| 102 |
.BR ttyname () |
| 103 |
or |
| 104 |
.BR ttyname_r () |
| 105 |
on the file descriptor in the new mount namespace will cause these |
| 106 |
functions to return NULL and set |
| 107 |
.I errno |
| 108 |
to |
| 109 |
.BR ENODEV . |
| 110 |
.SH SEE ALSO |
| 111 |
.BR tty (1), |
| 112 |
.BR fstat (2), |
| 113 |
.BR ctermid (3), |
| 114 |
.BR isatty (3), |
| 115 |
.BR pts (4) |