POSIX specific implementation details

This document details implementation details specific to fully POSIX compliant platforms, including Linux distributions and macOS, OS X.

Note

POSIX compliant platforms are identified using the presence of the __unix macro, except Apple’s OS X platform, which is identified separately since it doesn’t define the aforementioned macro.

Serial device management

Serial device management is mostly standardized on POSIX compliant platforms, except for serial device paths and advanced operations such as DTR/RTS management.

Serial device detection on POSIX compliant platforms

As described in SUSv4TC2 section 10, devices should be present in /dev. Therefore, we attempt at finding devices with the following patterns in the filesystem, expressed using regular expressions:

/dev/ttyUSB[0-9]+

USB serial devices on Linux distributions; see USB serial on Linux for more information.

/dev/cua[dn][0-9]+

Call-out ports on FreeBSD; see Serial communications on FreeBSD for more information.

/dev/cu\..+

Call-out ports on macOS, OS X.

/dev/dty[0-9]+

Call-out ports on NetBSD; see tty(4) on NetBSD for more information.

File implementation

When creating or opening a file on POSIX compliant systems, cahute_create_posix_file() and cahute_open_posix_file() use open(2). Then, depending on the situation:

  • On creation, we want to set the file size to the provided one.

    In order to do this, we call ftruncate(2).

  • On reading, we want to get the current file size.

    In order to do this, we call lseek(2) to seek 0 bytes from SEEK_END, which returns the current file size, then use the same function to seek 0 bytes from SEEK_SET.

In order to open the standard output, we just use file descriptor 1.

Once a file or stdout is opened, we have a file descriptor we can then use for the following operations: