Skip to main content

Posts

Showing posts with the label posix

Handling Terminal Output with Termios

As I explained in the previous article , Unix-like operating systems, for instance, OS X and Linux, use LF (line feed, 0x0A , \n ) as the newline character which moves the cursor to the beginning of the next line. However, the standard-defined behavior of LF only moves the cursor down to the next line, not to the beginning of the line. This difference is acceptable if files are always accessed through operating system-dependent applications. However, Unix-like systems have no distinction between files and input/output; this difference can be problematic when file and process input/output interact. To handle this difference, a terminal emulator post-processes the output appropriately. The c_oflag in the termios structure defined by the POSIX.1 standard controls this. The c_oflag is a flag for what post-processing the terminal should perform before displaying the received characters. The most important flag in c_oflag is OPOST . This flag determines whether or not to post-pro...

CR, LF, and CRLF

One of the confusing aspects for people working across multiple platforms is the newline character. Mac OS, Windows, and Linux all use different characters for newline. Even Mac OS behaves differently between older and newer versions. In this article, we will explore the reasons behind the different newline characters used across systems. According to the ISO 6429 standard, LF (line feed, \n) moves the cursor to the next line while maintaining the current column, and CR (carriage return, \r ) moves the cursor to the beginning of the current line. To achieve the newline function, both CR and LF should be used together. This distinction was made to mimic the behavior of early printers and typewriters that separated the line-changing action from the action of moving the cursor to the beginning. A B For instance, a string " A\nB " should not result in B directly below A , but rather B should appear diagonally below A , like in the above example. However, systems usin...