3 ms·
> The Standard C Library > Or how errno came to existence? This is interesting. According to this book, errno was created because they wanted system calls to
by matheusmoreira 7y ago
> The Standard C Library
> Or how errno came to existence?
This is interesting. According to this book, errno was created because they wanted system calls to work like ordinary C functions:
> Each implementation of UNIX adopts a simple method for indicating erroneous system calls.
> Writing in assembly language, you typically test the carry indicator in the condition code.
> That scheme is great for assembly language. It is less great for programs you write in C.
> You can write a library of C-callable functions, one for each distinct system call.
> You'd like each function return value to be the answer you request when making that particular system call.
This turned out to be unnecessary. For example, many Linux system calls return a signed size_t type with either the result or the negated errno constant. When an error occurs, the C library stub function simply negates that value and assigns it to errno which is likely a thread-local variable.
The C standard library provides many examples of bad design. All the hidden global data, the broken locale handling, the duplication found in so many str* and mem* functions... Freestanding C lacks most of the standard headers and is a better language for it. Understanding the historical context that contributed to these designs is very useful since it allows new languages to avoid repeating these mistakes.