Search code examples
linuxlinux-kernelposixchardev

Should Linux character devices terminate read() with newline


POSIX defines a text file as:

A file that contains characters organized into one or more lines.

POSIX defines a line as

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

Given this, should the read() function of a Linux character device driver append '\n' to the user buffer when it reaches EOF/has no more data?


Solution

  • Concept of char drivers is analogous to a stream. In this light, read just returns whatever happens to be available next. Now what is available will usually be part of definition of the device whose driver it is. If the device returns newline character then so should the driver. Note that this means the device will return newline on all platforms, not just Linux.

    In general, interpretating the bytes returned by read is a matter of higher level abstraction. In terms of policy vs mechanism, char driver can be thought of as providing mechanism, leaving policy to higher layers.