xref: /freebsd/lib/libc/gen/ttyname.3 (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
1.\" Copyright (c) 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.Dd July 18, 2014
29.Dt TTYNAME 3
30.Os
31.Sh NAME
32.Nm ttyname ,
33.Nm ttyname_r ,
34.Nm isatty
35.Nd get name of associated terminal (tty) from file descriptor
36.Sh LIBRARY
37.Lb libc
38.Sh SYNOPSIS
39.In unistd.h
40.Ft char *
41.Fn ttyname "int fd"
42.Ft int
43.Fn ttyname_r "int fd" "char *buf" "size_t len"
44.Ft int
45.Fn isatty "int fd"
46.Sh DESCRIPTION
47These functions operate on file descriptors for terminal type devices.
48.Pp
49The
50.Fn isatty
51function
52determines if the file descriptor
53.Fa fd
54refers to a valid
55terminal type device.
56.Pp
57The
58.Fn ttyname
59function
60gets the related device name of
61a file descriptor for which
62.Fn isatty
63is true.
64.Pp
65The
66.Fn ttyname
67function
68returns the name stored in a static buffer which will be overwritten
69on subsequent calls.
70The
71.Fn ttyname_r
72function
73takes a buffer and length as arguments to avoid this problem.
74.Sh RETURN VALUES
75The
76.Fn isatty
77function returns 1 if
78.Fa fd
79refers to a terminal type device;
80otherwise, it returns 0 and may set
81.Va errno
82to indicate the error.
83The
84.Fn ttyname
85function
86returns the null terminated name if the device is found and
87.Fn isatty
88is true; otherwise
89a
90.Dv NULL
91pointer is returned.
92The
93.Fn ttyname_r
94function returns 0 if successful.
95Otherwise an error number is returned.
96.Sh ERRORS
97These functions may fail if:
98.Bl -tag -width Er
99.It Bq Er EBADF
100The
101.Fa fd
102argument
103is not a valid file descriptor.
104.It Bq Er ENOTTY
105The file associated with
106.Fa fd
107is not a terminal.
108.El
109.Pp
110Additionally,
111.Fn ttyname_r
112may fail if:
113.Bl -tag -width Er
114.It Bq Er ERANGE
115The
116.Fa bufsize
117argument
118is smaller than the length of the string to be returned.
119.El
120.Sh SEE ALSO
121.Xr fdevname 3 ,
122.Xr ptsname 3 ,
123.Xr tcgetattr 3 ,
124.Xr tty 4
125.Sh HISTORY
126The
127.Fn isatty
128and
129.Fn ttyname
130functions
131appeared in
132.At v7 .
133The
134.Fn ttyname_r
135function
136appeared in
137.Fx 6.0 .
138