ttyslot

Section: C Library Functions (3)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

ttyslot - カレントユーザーの端末のスロットをファイルから探す  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#include <unistd.h>       /* See NOTES */

int ttyslot(void);

glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):

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)
 

説明

レガシーな関数 ttyslot() は、あるファイルに書かれているカレントユーザーのエントリーのインデックスを返す。

「どのファイルなのか?」という質問があるだろう。 では、まず最初にその歴史を見てみよう。  

大昔の歴史

There used to be a file /etc/ttys in UNIX V6, that was read by the init(1) program to find out what to do with each terminal line. Each line consisted of 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) indicating the sequence of line speeds to try ('-' was: start trying 110 baud). Thus a typical line was "18-". A hang on some line was solved by changing the '1' to a '0', signaling init, changing back again, and signaling init again.

In UNIX V7 the format was changed: here the second character was 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. Thus a typical line was "14console".

後者のシステムの書式は、より精巧である。 System V 系のシステムでは、代わりに /etc/inittab がある。  

大昔の歴史 (2)

一方、現在ログインしている人をリストするファイル /etc/utmp がある。 これは login(1) によって管理されている。 このファイルは固定されたサイズであり、ファイル内の適切なインデックスは、 login(1) によって決定される。 この際に ttyslot() が呼ばれて、 /etc/ttys における行番号を見つける (行番号は 1 から数える)。  

ttyslot の意味

よって、関数 ttyslot() は、ファイル /etc/ttys における呼び出し元のプロセスの制御端末のインデックスを返す。 これは /etc/utmp におけるカレントユーザーのエントリーのインデックスと (通常は) 等しい。 BSD には未だに /etc/ttys ファイルがあるが、System V 系のシステムにはないので、 このファイルを参照することはできない。 よって、そのようなシステムでは 「ttyslot() はカレントユーザーのユーザーアカウントデータベースにおけるインデックスを返す」 とドキュメントに書かれている。  

返り値

If successful, 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.  

属性

この節で使用されている用語の説明については、 attributes(7) を参照。
インターフェース属性値
ttyslot() Thread safetyMT-Unsafe
 

バージョン

utmp ファイルは様々なシステムで /etc/utmp, /var/adm/utmp, /var/run/utmp のようにいろいろな場所にある。  

標準規格

なし。  

歴史

SUSv1。SUSv2 では「過去の名残 (LEGACY)」と位置付けられている。 POSIX.1-2001 で削除された。 SUSv2 ではエラー時に -1 を返すことが要求されている。

この関数の glibc2 における実装では、ファイル _PATH_TTYS を読み込む。 これは <ttyent.h> において "/etc/ttys" と定義されている。 エラーの場合、0 を返す。 Linux システムには通常 "/etc/ttys" がないので、常に 0 を返す。

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)

Minix には fttyslot(fd) もある。  

関連項目

getttyent(3), ttyname(3), utmp(5)


 

Index

名前
ライブラリ
書式
説明
大昔の歴史
大昔の歴史 (2)
ttyslot の意味
返り値
属性
バージョン
標準規格
歴史
関連項目

This document was created by man2html, using the manual pages.