Hacker News new | ask | show | jobs
by saagarjha 2648 days ago
There are programs that violate POSIX and are picky about it.
2 comments

POSIX definition of what is text file actually requires the file to end with \n, so such programs are technically correct.

The reason why this is (and also why the lines should not be longer than LINE_MAX or contain \0) is that when you simply call fgets() in a loop with LINE_MAX sized buffer, all of these things cause perfectly logical, but wrong/surprising behavior. This is the reason why almost any modern small unix tool contains something called myfgets(), getline() or whatever which wraps fgets() in loop with realloc() and correctly distinguishes eof from other errors.

> POSIX definition of what is text file actually requires the file to actually end with \n, so such programs are technically correct.

No, they’re not correct, since they make files without the newline.

Which ones?