1TTYSLOT(3) Linux Programmer's Manual TTYSLOT(3)
2
3
4
6 ttyslot - find the slot of the current user's terminal in some file
7
9 #include <unistd.h> /* See NOTES */
10
11 int ttyslot(void);
12
13 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
14
15 ttyslot():
16 Since glibc 2.24:
17 _DEFAULT_SOURCE
18 From glibc 2.20 to 2.23:
19 _DEFAULT_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
20 Glibc 2.19 and earlier:
21 _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
22
24 The legacy function ttyslot() returns the index of the current user's
25 entry in some file.
26
27 Now "What file?" you ask. Well, let's first look at some history.
28
29 Ancient history
30 There used to be a file /etc/ttys in UNIX V6, that was read by the
31 init(1) program to find out what to do with each terminal line. Each
32 line consisted of three characters. The first character was either '0'
33 or '1', where '0' meant "ignore". The second character denoted the
34 terminal: '8' stood for "/dev/tty8". The third character was an argu‐
35 ment to getty(8) indicating the sequence of line speeds to try ('-'
36 was: start trying 110 baud). Thus a typical line was "18-". A hang on
37 some line was solved by changing the '1' to a '0', signaling init,
38 changing back again, and signaling init again.
39
40 In UNIX V7 the format was changed: here the second character was the
41 argument to getty(8) indicating the sequence of line speeds to try ('0'
42 was: cycle through 300-1200-150-110 baud; '4' was for the on-line con‐
43 sole DECwriter) while the rest of the line contained the name of the
44 tty. Thus a typical line was "14console".
45
46 Later systems have more elaborate syntax. System V-like systems have
47 /etc/inittab instead.
48
49 Ancient history (2)
50 On the other hand, there is the file /etc/utmp listing the people cur‐
51 rently logged in. It is maintained by login(1). It has a fixed size,
52 and the appropriate index in the file was determined by login(1) using
53 the ttyslot() call to find the number of the line in /etc/ttys (count‐
54 ing from 1).
55
56 The semantics of ttyslot
57 Thus, the function ttyslot() returns the index of the controlling ter‐
58 minal of the calling process in the file /etc/ttys, and that is (usu‐
59 ally) the same as the index of the entry for the current user in the
60 file /etc/utmp. BSD still has the /etc/ttys file, but System V-like
61 systems do not, and hence cannot refer to it. Thus, on such systems
62 the documentation says that ttyslot() returns the current user's index
63 in the user accounting data base.
64
66 If successful, this function returns the slot number. On error (e.g.,
67 if none of the file descriptors 0, 1, or 2 is associated with a termi‐
68 nal that occurs in this data base) it returns 0 on UNIX V6 and V7 and
69 BSD-like systems, but -1 on System V-like systems.
70
72 For an explanation of the terms used in this section, see at‐
73 tributes(7).
74
75 ┌──────────────────────────────────────────┬───────────────┬───────────┐
76 │Interface │ Attribute │ Value │
77 ├──────────────────────────────────────────┼───────────────┼───────────┤
78 │ttyslot() │ Thread safety │ MT-Unsafe │
79 └──────────────────────────────────────────┴───────────────┴───────────┘
80
82 SUSv1; marked as LEGACY in SUSv2; removed in POSIX.1-2001. SUSv2 re‐
83 quires -1 on error.
84
86 The utmp file is found in various places on various systems, such as
87 /etc/utmp, /var/adm/utmp, /var/run/utmp.
88
89 The glibc2 implementation of this function reads the file _PATH_TTYS,
90 defined in <ttyent.h> as "/etc/ttys". It returns 0 on error. Since
91 Linux systems do not usually have "/etc/ttys", it will always return 0.
92
93 On BSD-like systems and Linux, the declaration of ttyslot() is provided
94 by <unistd.h>. On System V-like systems, the declaration is provided
95 by <stdlib.h>. Since glibc 2.24, <stdlib.h> also provides the declara‐
96 tion with the following feature test macro definitions:
97
98 (_XOPEN_SOURCE >= 500 ||
99 (_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED))
100 && ! (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600)
101
102 Minix also has fttyslot(fd).
103
105 getttyent(3), ttyname(3), utmp(5)
106
108 This page is part of release 5.12 of the Linux man-pages project. A
109 description of the project, information about reporting bugs, and the
110 latest version of this page, can be found at
111 https://www.kernel.org/doc/man-pages/.
112
113
114
115GNU 2021-03-22 TTYSLOT(3)