a /dev and named
a /dev/tty Ns Em xx and for which an entry exists in the initialization file
a /etc/ttys (see .Xr ttys 5 ) , or for pseudo-terminal devices created in ptyfs and named
p The .Fn isatty function determines if the file descriptor .Fa fd refers to a valid terminal type device.
p The .Fn ttyname function gets the related device name of a file descriptor for which .Fn isatty is true. The .Fn ttyname_r is the reentrant version of the above, and it places the results in .Fa buf . If there is not enough space to place the results (indicated by .Fa len ) , then it returns an error.
p The .Fn ttyslot function fetches the current process' control terminal number from the .Xr ttys 5 file entry. If the terminal is a pseudo-terminal, and there is no special entry in the .Xr ttys 5 file for it, the slot number returned is 1 + (last slot number) + minor(tty). This will return a consistent and unique number for each pseudo-terminal device without requiring one to enumerate all of them in .Xr ttys 5 . .Sh IMPLEMENTATION NOTES As an optimisation, these functions attempt to obtain information about all devices from the
a /var/run/dev.cdb database, if it exists. If the database exists but is out of date, then these functions may produce incorrect results. The database should be updated using the .Xr dev_mkdb 8 command. .Sh RETURN VALUES The .Fn ttyname function returns the NUL-terminated name if the device is found and .Fn isatty is true; otherwise a .Dv NULL pointer is returned and .Va errno is set to indicate the error.
p The .Fn ttyname_r functions returns 0 on success and an error code on failure.
p The .Fn isatty function returns 1 if .Fa fd is associated with a terminal device; otherwise it returns 0 and .Va errno is set to indicate the error.
p The .Fn ttyslot function returns the unit number of the device file if found; otherwise the value zero is returned. .Sh FILES l -tag -width /etc/ttys -compact t Pa /dev/\(** t Pa /etc/ttys .El .Sh ERRORS The .Fn ttyname , .Fn ttyname_r , and .Fn isatty functions will fail if: l -tag -width Er t Bq Er EBADF The .Fa fd argument is not a valid file descriptor. t Bq Er ENOTTY The .Fa fd argument does not refer to a terminal device. .El
p
The
.Fn ttyname_r
function will also fail if:
l -tag -width Er t Bq Er ENOENT The terminal device is not found.
This can happen if the device node has been removed after it was opened.
t Bq Er ERANGE The buffer provided is not large enough to fit the result.
.El
.Sh SEE ALSO
.Xr ioctl 2 ,
.Xr ttys 5 ,
.Xr dev_mkdb 8
.Sh STANDARDS
The
.Fn ttyname
and
.Fn isatty
functions conform to
.St -p1003.1-90 .
.Sh HISTORY
.Fn isatty ,
.Fn ttyname ,
and
.Fn ttyslot
functions appeared in
.At v7 .
Use of the .Pa /var/run/dev.cdb file was added in netBSD 6.0.
.Sh BUGS
The
.Fn ttyname
function leaves its result in an internal static object and returns
a pointer to that object.
Subsequent calls to
.Fn ttyname
will modify the same object.