msfukuiの日記

おおまさのみみはそらのみみ。

POSIXの仕様では「テキストファイルの末尾は改行(newline)で終わる」のが正しい、を確認してみた。

唐突ですが、先日、エディタ領域のファイル末尾に [EOF] を表示するだけの VSCode extension を作ってみたのですが、そのもともとの動機が、テキストファイルの最後は改行で終わりたい→それエディタで見える様にしたい、という自分の好みから始まっていて、いろいろ調べていると、以下の様なお話があることを知りました。

POSIX のテキストファイルの仕様では、テキストファイルは行か行の集合であり、行はnewlineで終わる、と定義されている

だから Vim で末尾が改行コードでないファイルを開くと警告が出るんだなー、ということを今更ながら知ったのですが、以下のブログの記述を参考に、おおもとの定義はどうなっているのかな、と思って確認してみました。

yuu.nkjmlab.org

2004年時点の仕様

The Open Group Base Specifications Issue 6 IEEE Std 1003.1, 2004 Edition

http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap03.html

によると、

3.205 Line

A sequence of zero or more non- s plus a terminating .

となっていて、

3.392 Text File

A file that contains characters organized into one or more lines. The lines do not contain NUL characters and none can exceed {LINE_MAX} bytes in length, including the . Although IEEE Std 1003.1-2001 does not distinguish between text files and binary files (see the ISO C standard), many utilities only produce predictable or meaningful output when operating on text files. The standard utilities that have such restrictions always specify "text files" in their STDIN or INPUT FILES sections.

なので、line は改行で終わり、テキストファイルは1つ以上の行のまとまりなので、テキストファイルの末尾はで終わるということで納得。(えいごむずかしい。)

ところで今の最新の仕様

The Open Group Base Specifications Issue 7, 2018 edition IEEE Std 1003.1-2017 (Revision of IEEE Std 1003.1-2008)

The Open Group Base Specifications Issue 7, 2018 edition

を眺めてみると、

3.206 Line

A sequence of zero or more non- characters plus a terminating character.

...

3.403 Text File

A file that contains characters organized into zero or more lines. The lines do not contain NUL characters and none can exceed {LINE_MAX} bytes in length, including the character. Although POSIX.1-2017 does not distinguish between text files and binary files (see the ISO C standard), many utilities only produce predictable or meaningful output when operating on text files. The standard utilities that have such restrictions always specify "text files" in their STDIN or INPUT FILES sections.

となっていて、後者の表現が、0個以上の行のまとまり、と微妙に表現が変わっていて、で終わらないことを許容するようになったようにも読める。

ここ、この表現になったのはもうちょっと背景を含めて知りたいと思ったのでした。