Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

12
  • 2
    This doesn’t quite show what tac writes though — it shows what the C library’s buffered output routines write. That’s why F\n shows up as a separate call to write (which could give the impression that tac still treats newlines specially; it doesn’t, but the C library does). Commented Jul 23 at 11:43
  • @StephenKitt Agreed and thanks for clarifying that ... These are actually system calls being traced and don't necessarily reflect how tac exactly writes i.e. might be a single printf() for example. Commented Jul 23 at 12:12
  • @Raffa: Indeed, yes, glibc fputs surprisingly flushes separately after each newline in fputs("foo\nbar\n", stdout). I didn't expect that; seems extra slow for no reason, but I just tested it. Commented Jul 24 at 18:16
  • @PeterCordes I guess that’s typical when a stream is line buffered which stdout likely is in this case … stderr however might not be buffered and you might see a different outcome writing to that one .., Please see man7.org/linux/man-pages/man3/setbuf.3.html Commented Jul 24 at 20:04
  • Yes, stdout was line buffered since it was connected to a terminal. I just don't see why it doesn't remember the fact that there was a newline in the buffer and write once for the whole fputs. (Which is ironically what we get for unbuffered fputs("foo\nbar\n", stderr), more efficient handling with only one write system call.) It seems like taking the wording in setbuf too literally, of saving characters "until a newline is output"; could easily apply the as-if rule there. Commented Jul 24 at 20:49