Movatterモバイル変換


[0]ホーム

URL:


<<<PreviousHomeNext >>>


NAME

strerror, strerror_l, strerror_r - get error message string

SYNOPSIS

#include <string.h>

char *strerror(int
errnum);

[CX][Option Start] char *strerror_l(interrnum, locale_tlocale);
int strerror_r(int
errnum, char *strerrbuf, size_tbuflen);[Option End]

DESCRIPTION

Forstrerror():[CX][Option Start]  The functionality described on this reference page is aligned with the ISO C standard. Anyconflict between the requirements described here and the ISO C standard is unintentional. This volume of POSIX.1-2017 defersto the ISO C standard.[Option End]

Thestrerror() function shall map the error number inerrnum to a locale-dependent error message string and shallreturn a pointer to it. Typically, the values forerrnum come fromerrno, butstrerror() shall map any valueof typeint to a message.

The application shall not modify the string returned.[CX][Option Start]  The returned string pointer might be invalidated or[Option End]  the string content might be overwritten by a subsequent call tostrerror(),[CX][Option Start]  or by a subsequent call tostrerror_l() in the same thread. The returned pointer and the string contentmight also be invalidated if the calling thread is terminated.[Option End]

[CX][Option Start] Thestring may be overwritten by a subsequent call tostrerror_l() in the same thread.

The contents of the error message strings returned bystrerror() should be determined by the setting of theLC_MESSAGES category in the current locale.[Option End]

The implementation shall behave as if no function defined in this volume of POSIX.1-2017 callsstrerror().

[CX][Option Start] Thestrerror() andstrerror_l() functions shall not change the setting oferrno if successful.

Since no return value is reserved to indicate an error ofstrerror(), an application wishing to check for errorsituations should seterrno to 0, then callstrerror(), then checkerrno. Similarly, sincestrerror_l()is required to return a string for some errors, an application wishing to check for all error situations should seterrno to0, then callstrerror_l(), then checkerrno.

Thestrerror() function need not be thread-safe.

Thestrerror_l() function shall map the error number inerrnum to a locale-dependent error message string in thelocale represented bylocale and shall return a pointer to it.

Thestrerror_r() function shall map the error number inerrnum to a locale-dependent error message string andshall return the string in the buffer pointed to bystrerrbuf, with lengthbuflen.[Option End]

[CX][Option Start] Ifthe value oferrnum is a valid error number, the message string shall indicate what error occurred; if the value oferrnum is zero, the message string shall either be an empty string or indicate that no error occurred; otherwise, if thesefunctions complete successfully, the message string shall indicate that an unknown error occurred.[Option End]

[CX][Option Start] Thebehavior is undefined if thelocale argument tostrerror_l() is the special locale object LC_GLOBAL_LOCALE or is nota valid locale object handle.[Option End]

RETURN VALUE

Upon completion, whether successful or not,strerror() shall return a pointer to the generated message string.[CX][Option Start]  On errorerrno may be set, but no return value is reserved to indicate an error.

Upon successful completion,strerror_l() shall return a pointer to the generated message string. Iferrnum is nota valid error number,errno may be set to [EINVAL], but a pointer to a message string shall still be returned. If any othererror occurs,errno shall be set to indicate the error and a null pointer shall be returned.

Upon successful completion,strerror_r() shall return 0. Otherwise, an error number shall be returned to indicate theerror.[Option End]

ERRORS

These functions may fail if:

[EINVAL]
[CX][Option Start]The value oferrnum is neither a valid error number nor zero.[Option End]

Thestrerror_r() function may fail if:

[ERANGE]
[CX][Option Start]Insufficient storage was supplied viastrerrbuf andbuflen to contain the generated message string.[Option End]

The following sections are informative.

EXAMPLES

None.

APPLICATION USAGE

Historically in some implementations, calls toperror() would overwrite the stringthat the pointer returned bystrerror() points to. Such implementations did not conform to the ISO C standard; however,application developers should be aware of this behavior if they wish their applications to be portable to such implementations.

RATIONALE

Thestrerror_l() function is required to be thread-safe, thereby eliminating the need for an equivalent to thestrerror_r() function.

Earlier versions of this standard did not explicitly require that the error message strings returned bystrerror() andstrerror_r() provide any information about the error. This version of the standard requires a meaningful message for anysuccessful completion.

Since no return value is reserved to indicate astrerror() error, but all calls (whether successful or not) must return apointer to a message string, on errorstrerror() can return a pointer to an empty string or a pointer to a meaningful stringthat can be printed.

Note that the [EINVAL] error condition is a may fail error. If an invalid error number is supplied as the value oferrnum, applications should be prepared to handle any of the following:

  1. Error (with no meaningful message):errno is set to [EINVAL], the return value is a pointer to an empty string.

  2. Successful completion:errno is unchanged and the return value points to a string like"unknown error" or"error number xxx" (wherexxx is the value oferrnum).

  3. Combination of #1 and #2:errno is set to [EINVAL] and the return value points to a string like"unknown error"or"error number xxx" (wherexxx is the value oferrnum). Since applications frequently use the return valueofstrerror() as an argument to functions likefprintf() (without checkingthe return value) and since applications have no way to parse an error message string to determine whethererrnum representsa valid error number, implementations are encouraged to implement #3. Similarly, implementations are encouraged to havestrerror_r() return [EINVAL] and put a string like"unknown error" or"error number xxx" in the bufferpointed to bystrerrbuf when the value oferrnum is not a valid error number.

Some applications rely on being able to seterrno to 0 before calling a function with no reserved value to indicate anerror, then callstrerror(errno) afterwards to detect whether an error occurred (becauseerrno changed) or toindicate success (becauseerrno remained zero). This usage pattern requires thatstrerror(0) succeed with usefulresults. Previous versions of the standard did not specify the behavior whenerrnum is zero.

FUTURE DIRECTIONS

None.

SEE ALSO

perror

XBD<string.h>

CHANGE HISTORY

First released in Issue 3.

Issue 5

The DESCRIPTION is updated to indicate thaterrno is not changed if the function is successful.

A note indicating that thestrerror() function need not be reentrant is added to the DESCRIPTION.

Issue 6

Extensions beyond the ISO C standard are marked.

The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:

The normative text is updated to avoid use of the term "must" for application requirements.

Thestrerror_r() function is added in response to IEEE PASC Interpretation 1003.1c #39.

Thestrerror_r() function is marked as part of the Thread-Safe Functions option.

Issue 7

Austin Group Interpretation 1003.1-2001 #072 is applied, updating the ERRORS section.

Austin Group Interpretation 1003.1-2001 #156 is applied.

Austin Group Interpretation 1003.1-2001 #187 is applied, clarifying the behavior when the generated error message is an emptystring.

SD5-XSH-ERN-191 is applied, updating the APPLICATION USAGE section.

Thestrerror_l() function is added from The Open Group Technical Standard, 2006, Extended API Set Part 4.

Thestrerror_r() function is moved from the Thread-Safe Functions option to the Base.

POSIX.1-2008, Technical Corrigendum 1, XSH/TC1-2008/0595 [75], XSH/TC1-2008/0596 [447], XSH/TC1-2008/0597 [382,428],XSH/TC1-2008/0598 [283], XSH/TC1-2008/0599 [382,428], XSH/TC1-2008/0600 [283], and XSH/TC1-2008/0601 [382,428] are applied.

POSIX.1-2008, Technical Corrigendum 2, XSH/TC2-2008/0339 [656] is applied.

End of informative text.

 

return to top of page


<<<PreviousHomeNext >>>


[8]ページ先頭

©2009-2026 Movatter.jp