Why errno when the POSIX function indicates an error status, returning -1 or NULL

If an error occurs in one of the UNIX system functions, a negative value is often returned, and the integer is errnoset to a value that provides additional information. - Advanced UNIX Programming, Section 1.7

This seems odd:

  • It introduces the relationship between the different compilation units of the standard library - error conditions are not defined in the modules that call them.
  • It introduces implementation complexity because it errnoneeds to be streaming local.
  • It is difficult to use because the user needs to check that the original system call is in error, and then check errnowhich is also another function call.

Why not code the error states in the return values?

+4
source share
2 answers

For historical reasons, mostly.

, errno ( 1980- ). (C99, C11...) - , , __errno() ( C , errno , . n1570 ยง7.5); .

errno , , , ,

, #include <errno.h> errno , - , , .

. C, . musl-libc errno/__errno_location.c

  int *__errno_location(void)
  {
     return &__pthread_self()->errno_val;
  }

include/errno.h :

 int *__errno_location(void);
 #define errno (*__errno_location())

GNU libc

, (, mmap), POSIX errno, . dlopen (. dlerror). , .

+7

Linus Torvalds .

"errno" - UNIX. Linux . , , , "" .

[...]

"errno" - , . UNIX, .

[...]

. -, . , , , .

+3

Source: https://habr.com/ru/post/1626562/


All Articles