errno(3) 線程局部變量

注意 NOTE

A common mistake is to do

一個常見的錯誤是像下面這樣

if (somecall() == -1) 
{
    printf("somecall() failed\n");
    if (errno == ...) { ... }
}

where errno no longer needs to have the value it had upon return from somecall() (i.e., it may have been changed by the printf(3)). If the value
of errno should be preserved across a library call, it must be saved:

errno 已經不再具有 somecall() 的返回值了,它的值已經被 printf(3) 改變。如果。要保留系統調用的錯誤碼,則它必須被保存:

if (somecall() == -1) {
        int errsv = errno;
        printf("somecall() failed\n");
        if (errsv == ...) { ... }
    }

名稱 NAME

errno - number of last error

語法 SYNOPSIS

/#include

描述 DESCRIPTION

The errno.h header file defines the integer variable errno, which is set by system calls and some library functions in the event of an error to
indicate what went wrong. Its value is significant only when the return value of the call indicated an error (i.e., -1 from most system calls; -1
or NULL from most library functions); a function that succeeds is allowed to change errno.

頭文件 errno.h 定義了整型變量出錯值,這些值在系統調用和一些庫函數出錯情況下被賦值,用來指出發生了哪種錯誤。

只有當調用的返回值指明一個錯誤時,它的值纔有意義。(例如,大多系統調用返回-1,大多庫函數返回NULL)

成功執行的函數也可以改變 erno 的值。

Valid error numbers are all nonzero; errno is never set to zero by any system call or library function.

有效的錯誤碼都是非零的;errno 不會被任何系統調用或庫函數設置爲0.

For some system calls and library functions (e.g., getpriority(2)), -1 is a valid return on success. In such cases, a successful return can be
distinguished from an error return by setting errno to zero before the call, and then, if the call returns a status that indicates that an error
may have occurred, checking to see if errno has a nonzero value.

對於有些系統調用和庫函數,-1 可以是執行成功的有效的返回值。這些情況中,成功的發返回和出錯的返回值可以通過以下方式解決:
在調用前將 errno 設置爲0,然後,如果調用返回狀態指出可能有錯誤發生,可以檢查 errno 是否爲非零值即可。

errno is defined by the ISO C standard to be a modifiable lvalue of type int, and must not be explicitly declared; errno may be a macro. errno is
thread-local; setting it in one thread does not affect its value in any other thread.

errno 由 ISO C 標準定義爲一個可變的整型的左值,並且不可以被明確聲明;errno 可能是宏。errno 是線程局部變量;在一個線程中修改它的值並不影響它在另一個線程中的值。

All the error names specified by POSIX.1 must have distinct values, with the exception of EAGAIN and EWOULDBLOCK, which may be the same.

POSIX.1 指定的錯誤名稱必須有唯一值,除了 EAGAIN 和 EWOULDBLOCK,這倆是相同的。

Below is a list of the symbolic error names that are defined on Linux. Some of these are marked POSIX.1, indicating that the name is defined by
POSIX.1-2001, or C99, indicating that the name is defined by C99.

以下是 Linux 下定義的符號錯誤名稱列表。有些被 POSIX.2標記,指明其是由 POSIX.1-2001 定義的,C99同理。

常量

名稱 含義
E2BIG Argument list too long (POSIX.1)
EACCES Permission denied (POSIX.1)
EADDRINUSE Address already in use (POSIX.1)
EADDRNOTAVAIL Address not available (POSIX.1)
自行查閱即可 自行查閱即可

注意 NOTES

It was common in traditional C to declare errno manually (i.e., extern int errno) instead of including

參考 SEE ALSO

   errno(1), err(3), error(3), perror(3), strerror(3)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章