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