C Programming/stdio.h/fopen

fopen, as implemented in glibc and musl will use the open system call.

Opening a file using fopen
A file is opened using, which returns an I/O stream attached to the specified file or other device from which reading and writing can be done. If the function fails, it returns a null pointer.

The related C library function  performs the same operation after first closing any open stream associated with its parameters.

They are defined as

The  function is essentially a slightly higher-level wrapper for the   system call of Unix operating systems. In the same way,  is often a thin wrapper for the Unix system call , and the C   structure itself often corresponds to a Unix file descriptor. In POSIX environments, the  function can be used to initialize a   structure from a file descriptor; however, file descriptors are a purely Unix concept not present in standard C.

The  parameter to   and   must be a string that begins with one of the following sequences:

The "b" stands for binary. The C standard provides for two kinds of files—text files and binary files—although operating systems are not required to distinguish between the two. A text file is a file consisting of text arranged in lines with some sort of distinguishing end-of-line character or sequence (in Unix, a bare line feed character; in Microsoft Windows, a carriage return followed by a line feed). When bytes are read in from a text file, an end-of-line sequence is usually mapped to a linefeed for ease in processing. When a text file is written to, a bare linefeed is mapped to the OS-specific end-of-line character sequence before writing. A binary file is a file where bytes are read in "raw", and delivered "raw", without any kind of mapping.

When a file is opened with update mode ( '+' as the second or third character in the mode argument), both input and output may be performed on the associated stream. However, writes cannot be followed by reads without an intervening call to  or to a file positioning function (,  , or  ), and reads cannot be followed by writes without an intervening call to a file positioning function.

Writing and appending modes will attempt to create a file of the given name, if no such file already exists. As mentioned above, if this operation fails,  will return.

Glibc/musl notes
Musl and Glibc support the e mode on Linux, which sets O_CLOEXEC on the new file descriptor. O_CLOEXEC is a Linux 2.6 feature, documented in the Linux man-pages project:

"Enable the close-on-exec flag for the new file descriptor. Specifying this flag permits a program to avoid additional fcntl(2) F_SETFD operations to set the FD_CLOEXEC flag. Additionally, use of this flag is essential in some multithreaded programs since using a separate fcntl(2) F_SETFD operation to set the FD_CLOEXEC flag does not suffice to avoid race conditions where one thread opens a file descriptor at the same time as another thread does a fork(2) plus execve(2)."

See http://danwalsh.livejournal.com/53603.html

See musl cgit for the Musl implementation.