#include <string.h>
char *strerror(int errnum);
const char *strerrorname_np(int errnum);
const char *strerrordesc_np(int errnum);
int strerror_r(size_t size;
int errnum, char buf[size], size_t size);
/* XSI-compliant */
char *strerror_r(size_t size;
int errnum, char buf[size], size_t size);
/* GNU-specific */
char *strerror_l(int errnum, locale_t locale);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
strerrorname_np(), strerrordesc_np():
_GNU_SOURCE
strerror_r():
The XSI-compliant version is provided if:
(_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE
Otherwise, the GNU-specific version is provided.
Like strerror(), the strerrordesc_np() function returns a pointer to a string that describes the error code passed in the argument errnum, with the difference that the returned string is not translated according to the current locale.
The strerrorname_np() function returns a pointer to a string containing the name of the error code passed in the argument errnum. For example, given EPERM as an argument, this function returns a pointer to the string "EPERM". Given 0 as an argument, this function returns a pointer to the string "0".
The XSI-compliant strerror_r() is preferred for portable applications. It returns the error string in the user-supplied buffer buf of size size.
The GNU-specific strerror_r() returns a pointer to a string containing the error message. This may be either a pointer to a string that the function stores in buf, or a pointer to some (immutable) static string (in which case buf is unused). If the function stores a string in buf, then at most size bytes are stored (the string may be truncated if size is too small and errnum is unknown). The string always includes a terminating null byte ('\0').
On success, strerrorname_np() and strerrordesc_np() return the appropriate error description string. If errnum is an invalid error number, these functions return NULL.
The XSI-compliant strerror_r() function returns 0 on success. On error, a (positive) error number is returned (since glibc 2.13), or -1 is returned and errno is set to indicate the error (before glibc 2.13).
POSIX.1-2001 と POSIX.1-2008 では、 strerror() や strerror_l() が成功した場合は errno を変更せずに元のままにしなければならないとされている。関数のどの返り値もエラーを示すために予約されていないので、エラーをチェックしたいアプリケーションは呼び出しを行う前に errno を 0 に初期化し、呼び出しの後で errno をチェックすべき点に注意すること。
| インターフェース | 属性 | 値 |
| strerror() | Thread safety |
MT-Safe
|
| strerrorname_np(), strerrordesc_np() | Thread safety | MT-Safe |
| strerror_r(), strerror_l() | Thread safety | MT-Safe |
Before glibc 2.32, strerror() is not MT-Safe.
POSIX.1-2001 は、 strerror() がエラーに遭遇した場合に errno をセッ トすることを認めているが、エラー発生時に関数の結果として どんな値を返す べきかを規定してない。 あるシステムでは、 エラー番号が未知の場合、 strerror() は NULL を返す。 他のシステムでは、 エラー番号が未知の場 合、 strerror() は "Error nnn occurred" といった文字列を返し、 errno に EINVAL をセットする。 C99 と POSIX.1-2008 では、返り値が NULL 以外になることが求められている。