| 1 |
'\" t |
| 2 |
.\" Copyright 2002, Andries E. Brouwer <[email protected]> |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 6 |
.\" |
| 7 |
.TH ttyslot 3 2026-02-08 "Linux man-pages 6.18" |
| 8 |
.SH NAME |
| 9 |
ttyslot \- find the slot of the current user's terminal in some file |
| 10 |
.SH LIBRARY |
| 11 |
Standard C library |
| 12 |
.RI ( libc ,\~ \-lc ) |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
.BR "#include <unistd.h>" " /* See NOTES */" |
| 16 |
.P |
| 17 |
.B "int ttyslot(void);" |
| 18 |
.fi |
| 19 |
.P |
| 20 |
.RS -4 |
| 21 |
Feature Test Macro Requirements for glibc (see |
| 22 |
.BR feature_test_macros (7)): |
| 23 |
.RE |
| 24 |
.P |
| 25 |
.BR ttyslot (): |
| 26 |
.nf |
| 27 |
Since glibc 2.24: |
| 28 |
_DEFAULT_SOURCE |
| 29 |
From glibc 2.20 to glibc 2.23: |
| 30 |
_DEFAULT_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500) |
| 31 |
glibc 2.19 and earlier: |
| 32 |
_BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500) |
| 33 |
.fi |
| 34 |
.SH DESCRIPTION |
| 35 |
The legacy function |
| 36 |
.BR ttyslot () |
| 37 |
returns the index of the current user's entry in some file. |
| 38 |
.P |
| 39 |
Now "What file?" you ask. |
| 40 |
Well, let's first look at some history. |
| 41 |
.SS Ancient history |
| 42 |
There used to be a file |
| 43 |
.I /etc/ttys |
| 44 |
in UNIX\ V6, that was read by the |
| 45 |
.BR init (1) |
| 46 |
program to find out what to do with each terminal line. |
| 47 |
Each line consisted of three characters. |
| 48 |
The first character was either \[aq]0\[aq] or \[aq]1\[aq], |
| 49 |
where \[aq]0\[aq] meant "ignore". |
| 50 |
The second character denoted the terminal: \[aq]8\[aq] stood for "/dev/tty8". |
| 51 |
The third character was an argument to |
| 52 |
.BR getty (8) |
| 53 |
indicating the sequence of line speeds to try (\[aq]\-\[aq] was: start trying |
| 54 |
110 baud). |
| 55 |
Thus a typical line was "18\-". |
| 56 |
A hang on some line was solved by changing the \[aq]1\[aq] to a \[aq]0\[aq], |
| 57 |
signaling init, changing back again, and signaling init again. |
| 58 |
.P |
| 59 |
In UNIX\ V7 the format was changed: here the second character |
| 60 |
was the argument to |
| 61 |
.BR getty (8) |
| 62 |
indicating the sequence of line speeds to try (\[aq]0\[aq] was: cycle through |
| 63 |
300-1200-150-110 baud; \[aq]4\[aq] was for the on-line console DECwriter) |
| 64 |
while the rest of the line contained the name of the tty. |
| 65 |
Thus a typical line was "14console". |
| 66 |
.P |
| 67 |
Later systems have more elaborate syntax. |
| 68 |
System V-like systems have |
| 69 |
.I /etc/inittab |
| 70 |
instead. |
| 71 |
.SS Ancient history (2) |
| 72 |
On the other hand, there is the file |
| 73 |
.I /etc/utmp |
| 74 |
listing the people currently logged in. |
| 75 |
It is maintained by |
| 76 |
.BR login (1). |
| 77 |
It has a fixed size, and the appropriate index in the file was |
| 78 |
determined by |
| 79 |
.BR login (1) |
| 80 |
using the |
| 81 |
.BR ttyslot () |
| 82 |
call to find the number of the line in |
| 83 |
.I /etc/ttys |
| 84 |
(counting from 1). |
| 85 |
.SS The semantics of ttyslot |
| 86 |
Thus, the function |
| 87 |
.BR ttyslot () |
| 88 |
returns the index of the controlling terminal of the calling process |
| 89 |
in the file |
| 90 |
.IR /etc/ttys , |
| 91 |
and that is (usually) the same as the index of the entry for the |
| 92 |
current user in the file |
| 93 |
.IR /etc/utmp . |
| 94 |
BSD still has the |
| 95 |
.I /etc/ttys |
| 96 |
file, but System V-like systems do not, and hence cannot refer to it. |
| 97 |
Thus, on such systems the documentation says that |
| 98 |
.BR ttyslot () |
| 99 |
returns the current user's index in the user accounting data base. |
| 100 |
.SH RETURN VALUE |
| 101 |
If successful, this function returns the slot number. |
| 102 |
On error (e.g., if none of the file descriptors 0, 1, or 2 is |
| 103 |
associated with a terminal that occurs in this data base) |
| 104 |
it returns 0 on UNIX\ V6 and V7 and BSD-like systems, |
| 105 |
but \-1 on System V-like systems. |
| 106 |
.SH ATTRIBUTES |
| 107 |
For an explanation of the terms used in this section, see |
| 108 |
.BR attributes (7). |
| 109 |
.TS |
| 110 |
allbox; |
| 111 |
lbx lb lb |
| 112 |
l l l. |
| 113 |
Interface Attribute Value |
| 114 |
T{ |
| 115 |
.na |
| 116 |
.nh |
| 117 |
.BR ttyslot () |
| 118 |
T} Thread safety MT-Unsafe |
| 119 |
.TE |
| 120 |
.SH VERSIONS |
| 121 |
The utmp file is found in various places on various systems, such as |
| 122 |
.IR /etc/utmp , |
| 123 |
.IR /var/adm/utmp , |
| 124 |
.IR /var/run/utmp . |
| 125 |
.SH STANDARDS |
| 126 |
None. |
| 127 |
.SH HISTORY |
| 128 |
SUSv1; marked as LEGACY in SUSv2; removed in POSIX.1-2001. |
| 129 |
SUSv2 requires \-1 on error. |
| 130 |
.P |
| 131 |
The glibc2 implementation of this function reads the file |
| 132 |
.BR _PATH_TTYS , |
| 133 |
defined in |
| 134 |
.I <ttyent.h> |
| 135 |
as "/etc/ttys". |
| 136 |
It returns 0 on error. |
| 137 |
Since Linux systems do not usually have "/etc/ttys", it will |
| 138 |
always return 0. |
| 139 |
.P |
| 140 |
On BSD-like systems and Linux, the declaration of |
| 141 |
.BR ttyslot () |
| 142 |
is provided by |
| 143 |
.IR <unistd.h> . |
| 144 |
On System V-like systems, the declaration is provided by |
| 145 |
.IR <stdlib.h> . |
| 146 |
Since glibc 2.24, |
| 147 |
.I <stdlib.h> |
| 148 |
also provides the declaration with the following |
| 149 |
feature test macro definitions: |
| 150 |
.P |
| 151 |
.in +4n |
| 152 |
.EX |
| 153 |
(_XOPEN_SOURCE >= 500 || |
| 154 |
(_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED)) |
| 155 |
&& ! (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) |
| 156 |
.EE |
| 157 |
.in |
| 158 |
.P |
| 159 |
Minix also has |
| 160 |
.IR fttyslot ( fd ). |
| 161 |
.\" .SH HISTORY |
| 162 |
.\" .BR ttyslot () |
| 163 |
.\" appeared in UNIX V7. |
| 164 |
.SH SEE ALSO |
| 165 |
.BR getttyent (3), |
| 166 |
.BR ttyname (3), |
| 167 |
.BR utmp (5) |