1. 9

  2. 2

    I always associated the stdio functions more with the portable subset part of C, rather than Unix specifically. For Unix-specific I/O, I’d think of open, read, write, and one of our many friends, creat.

    1. 3

      The C APIs are more friendly in some ways. For example, stdin and stdout are buffered, so an fread may be satisfied without a system call (and, when it does a system call, will ready a large chunk of data in a single call, typically all of the available input for interactive things), whereas a read on STDIN_FILENO is a system call (POSIX doesn’t require it to be one, but it is practically everywhere). In addition, a read is allowed to spuriously return early. If your process receives a signal, then a blocking read will return EINTR if it doesn’t read all of the requested data, or simply the amount that it did read otherwise. In contrast, fread will hide that from you.

      Most of the times I’ve seen people use the raw POSIX APIs, they’ve failed to correctly handle spurious failure. If I remember correctly, OpenBSD (and OpenSSH) has some lightweight wrappers that do the right thing.