ttyname(3)

ttyname3

Get name of associated terminal (tty) from file descriptor

Usage

#include <unistd.h>

char *

ttyname(int fd);

int

ttyname_r(int fd, char *buf, size_t len);

int

isatty(int fd);

Library

Standard C Library (libc, -lc)

Overview

These functions operate on file descriptors for terminal type devices.

The isatty() function determines if the file descriptor fd refers to a valid terminal type device.

The ttyname() function gets the related device name of a file descriptor for which isatty() is true.

The ttyname() function returns the name stored in a static buffer which will be overwritten on subsequent calls. The ttyname_r() function takes a buffer and length as arguments to avoid this problem.

Return values

The isatty() function returns 1 if fd refers to a terminal type device; otherwise, it returns 0 and may set errno to indicate the error. The ttyname() function returns the null terminated name if the device is found and isatty() is true; otherwise a NULL pointer is returned. The ttyname_r() function returns 0 if successful. Otherwise an error number is returned.

Errors

These functions may fail if:

[EBADF]

The fd argument is not a valid file descriptor.

[ENOTTY]

The file associated with fd is not a terminal.

Additionally, ttyname_r() may fail if:

[ERANGE]

The bufsize argument is smaller than the length of the string to be returned.

Releases

87 releases since FreeBSD 2.0.5-RELEASE; the text changed 42 times.

  1. 2.0.5–2.2.5
  2. 2.2.6–3.4
  3. 3.5–3.5.1
  4. 4.0
  5. 4.1–4.2
  6. 4.3–4.4
  7. 4.5–4.11
  8. 5.0
  9. 5.1–5.5
  10. 6.0–6.3
  11. 6.4
  12. 7.0
  13. 7.1
  14. 7.2
  15. 7.3
  16. 7.4
  17. 8.0
  18. 8.1
  19. 8.2
  20. 8.3
  21. 8.4
  22. 9.0
  23. 9.1
  24. 9.2
  25. 9.3
  26. 10.0
  27. 10.1
  28. 10.2
  29. 10.3
  30. 10.4
  31. 11.0
  32. 11.1
  33. 11.2
  34. 11.3
  35. 11.4
  36. 12.0
  37. 12.1
  38. 12.2
  39. 12.3
  40. 12.4
  41. 13.0–13.2
  42. 13.3–14.5
  43. 15.0–16.0

History

The isatty() and ttyname() functions appeared in Version 7 AT&T UNIX. The ttyname_r() function appeared in FreeBSD 6.0.

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.