ttyslot(3)
π ttyslot(3) β the vibe check on your terminal slot π
okay bestie, we're reading the ttyslot page from the Library Functions Manual, and ngl, this one is a whole archaeology dig. it's giving ancient Unix fossil, but it's also kind of iconic. let's go π¦
β¨ NAME β¨
ttyslot β find the slot of the current user's terminal in some file
π LIBRARY π
Standard C library (libc, -lc)
πΈ SYNOPSIS πΈ
#include <unistd.h> /* See HISTORY */
int ttyslot(void);
π Feature Test Macro Requirements for glibc π
(peep feature_test_macros(7) if you're lost, bestie)
ttyslot():
Since glibc 2.24:
_DEFAULT_SOURCE
From glibc 2.20 to glibc 2.23:
_DEFAULT_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
glibc 2.19 and earlier:
_BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
like... that's a lot of macros. but okay. we move. π
π DESCRIPTION π
so the legacy function ttyslot() returns the index of the current user's entry in some file.
and now you're like "bestie. which file." and honestly?? valid question. so let's do a little history lesson first, stay with me, it's gonna slay π§
ποΈ Ancient history ποΈ
back in the day, there was a file /etc/ttys in UNIX V6, and init(1) read it to figure out what to do with each terminal line. every line was exactly three characters:
- the first character was either
'0'or'1', where'0'meant "ignore" - the second character denoted the terminal:
'8'stood for/dev/tty8 - the third character was an argument to
getty(8)telling it the sequence of line speeds to try ('-'was: start trying 110 baud)
so a typical line was "18-". and if some line was hanging?? you fixed it by changing the '1' to a '0', signaling init, changing it back again, and signaling init again. truly manual girlbossing π
then in UNIX V7 the format changed: the second character was now the argument to getty(8) indicating the sequence of line speeds to try ('0' was: cycle through 300-1200-150-110 baud; '4' was for the on-line console DECwriter) while the rest of the line contained the name of the tty. so a typical line was "14console". glow up fr fr β¨
later systems got more elaborate syntax. and System V-like systems have /etc/inittab instead. okay periodt.
πΊ Ancient history (2) πΊ
on the other hand, there's the file /etc/utmp, which lists the people currently logged in. it's maintained by login(1). it has a fixed size, and the appropriate index in the file was determined by login(1) using the ttyslot() call to find the number of the line in /etc/ttys (counting from 1). no cap.
π¦ The semantics of ttyslot π¦
so here's the tea: ttyslot() returns the index of the controlling terminal of the calling process in the file /etc/ttys, and that is (usually) the same as the index of the entry for the current user in the file /etc/utmp.
BSD still has the /etc/ttys file, but System V-like systems do not, and hence cannot refer to it. so on those systems, the documentation says ttyslot() returns the current user's index in the user accounting data base. different file, same girl π
π«Ά RETURN VALUE π«Ά
if it goes well, this function returns the slot number. π
on error (e.g., if none of the file descriptors 0, 1, or 2 is associated with a terminal that occurs in this data base) it returns 0 on UNIX V6 and V7 and BSD-like systems, but -1 on System V-like systems.
β οΈ yeah bestie,0vs-1?? the return value literally depends on which flavor of Unix you're on. check twice, cry once.
β¨ ATTRIBUTES β¨
for an explanation of the terms used in this section, see attributes(7).
ttyslot()β Thread safety: MT-Unsafe π
(that's the whole table, she's small but she's honest)
π VERSIONS π
the utmp file is found in various places on various systems, such as /etc/utmp, /var/adm/utmp, /var/run/utmp. she moves around, okay, she's mysterious π
π STANDARDS π
None.
literally none. iconic behaviour honestly. π
π HISTORY π
SUSv1; marked as LEGACY in SUSv2; removed in POSIX.1-2001. SUSv2 requires -1 on error.
the glibc2 implementation of this function reads the file _PATH_TTYS, defined in <ttyent.h> as "/etc/ttys". it returns 0 on error. since Linux systems do not usually have "/etc/ttys", it will always return 0. π (yes, always. that's not me being dramatic, that's the manpage.)
on BSD-like systems and Linux, the declaration of ttyslot() is provided by <unistd.h>. on System V-like systems, the declaration is provided by <stdlib.h>. since glibc 2.24, <stdlib.h> also provides the declaration with the following feature test macro definitions:
(_XOPEN_SOURCE >= 500 ||
(_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED))
&& ! (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600)
also Minix has fttyslot(fd). shoutout Minix π«Ά
π₯ SEE ALSO π₯
and that's the whole page, bestie!! you ate that. go forth and never call ttyslot() on Linux expecting anything but 0 π
this lil masterpiece lives in Linux man-pages 6.20, dated 2026-08-04 β log off, hydrate, and read a manpage for fun sometime π¦β¨π
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- Copyright 2002, Andries E. Brouwer <aeb@cwi.nl>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms