xref: /freebsd/lib/libc/gen/ttyname.3 (revision cbb3ec25236ba72f91cbdf23f8b78b9d1af0cedf)
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.\"     @(#)ttyname.3	8.1 (Berkeley) 6/4/93
29.\"
30.Dd July 18, 2014
31.Dt TTYNAME 3
32.Os
33.Sh NAME
34.Nm ttyname ,
35.Nm ttyname_r ,
36.Nm isatty
37.Nd get name of associated terminal (tty) from file descriptor
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In unistd.h
42.Ft char *
43.Fn ttyname "int fd"
44.Ft int
45.Fn ttyname_r "int fd" "char *buf" "size_t len"
46.Ft int
47.Fn isatty "int fd"
48.Sh DESCRIPTION
49These functions operate on file descriptors for terminal type devices.
50.Pp
51The
52.Fn isatty
53function
54determines if the file descriptor
55.Fa fd
56refers to a valid
57terminal type device.
58.Pp
59The
60.Fn ttyname
61function
62gets the related device name of
63a file descriptor for which
64.Fn isatty
65is true.
66.Pp
67The
68.Fn ttyname
69function
70returns the name stored in a static buffer which will be overwritten
71on subsequent calls.
72The
73.Fn ttyname_r
74function
75takes a buffer and length as arguments to avoid this problem.
76.Sh RETURN VALUES
77The
78.Fn isatty
79function returns 1 if
80.Fa fd
81refers to a terminal type device;
82otherwise, it returns 0 and may set
83.Va errno
84to indicate the error.
85The
86.Fn ttyname
87function
88returns the null terminated name if the device is found and
89.Fn isatty
90is true; otherwise
91a
92.Dv NULL
93pointer is returned.
94The
95.Fn ttyname_r
96function returns 0 if successful.
97Otherwise an error number is returned.
98.Sh ERRORS
99These functions may fail if:
100.Bl -tag -width Er
101.It Bq Er EBADF
102The
103.Fa fd
104argument
105is not a valid file descriptor.
106.It Bq Er ENOTTY
107The file associated with
108.Fa fd
109is not a terminal.
110.El
111.Pp
112Additionally,
113.Fn ttyname_r
114may fail if:
115.Bl -tag -width Er
116.It Bq Er ERANGE
117The
118.Fa bufsize
119argument
120is smaller than the length of the string to be returned.
121.El
122.Sh SEE ALSO
123.Xr fdevname 3 ,
124.Xr ptsname 3 ,
125.Xr tcgetattr 3 ,
126.Xr tty 4
127.Sh HISTORY
128The
129.Fn isatty
130and
131.Fn ttyname
132functions
133appeared in
134.At v7 .
135The
136.Fn ttyname_r
137function
138appeared in
139.Fx 6.0 .
140