# strerror

## PROLOG



           This manual page is part of the POSIX Programmer's Manual.  The
           Linux implementation of this interface may differ (consult the
           corresponding Linux manual page for details of Linux behavior), or
           the interface may not be implemented on Linux.
    


## NAME



           strerror, strerror_l, strerror_r — get error message string
    


## SYNOPSIS



           #include <string.h>
    
           char *strerror(int _errnum_);
           char *strerror_l(int _errnum_ , locale_t _locale_);
           int strerror_r(int _errnum_ , char *_strerrbuf_ , size_t _buflen_);
    


## DESCRIPTION



           For _strerror_(): The functionality described on this reference page
           is aligned with the ISO C standard. Any conflict between the
           requirements described here and the ISO C standard is
           unintentional. This volume of POSIX.1‐2017 defers to the ISO C
           standard.
    
           The _strerror_() function shall map the error number in _errnum_ to a
           locale-dependent error message string and shall return a pointer
           to it. Typically, the values for _errnum_ come from _errno_ , but
           _strerror_() shall map any value of type **int** to a message.
    
           The application shall not modify the string returned.  The
           returned string pointer might be invalidated or the string content
           might be overwritten by a subsequent call to _strerror_(), or by a
           subsequent call to _strerror_l_() in the same thread. The returned
           pointer and the string content might also be invalidated if the
           calling thread is terminated.
    
           The string may be overwritten by a subsequent call to _strerror_l_()
           in the same thread.
    
           The contents of the error message strings returned by _strerror_()
           should be determined by the setting of the _LC_MESSAGES_ category in
           the current locale.
    
           The implementation shall behave as if no function defined in this
           volume of POSIX.1‐2017 calls _strerror_().
    
           The _strerror_() and _strerror_l_() functions shall not change the
           setting of _errno_ if successful.
    
           Since no return value is reserved to indicate an error of
           _strerror_(), an application wishing to check for error situations
           should set _errno_ to 0, then call _strerror_(), then check _errno_.
           Similarly, since _strerror_l_() is required to return a string for
           some errors, an application wishing to check for all error
           situations should set _errno_ to 0, then call _strerror_l_(), then
           check _errno_.
    
           The _strerror_() function need not be thread-safe.
    
           The _strerror_l_() function shall map the error number in _errnum_ to
           a locale-dependent error message string in the locale represented
           by _locale_ and shall return a pointer to it.
    
           The _strerror_r_() function shall map the error number in _errnum_ to
           a locale-dependent error message string and shall return the
           string in the buffer pointed to by _strerrbuf_ , with length _buflen_.
    
           If the value of _errnum_ is a valid error number, the message string
           shall indicate what error occurred; if the value of _errnum_ is
           zero, the message string shall either be an empty string or
           indicate that no error occurred; otherwise, if these functions
           complete successfully, the message string shall indicate that an
           unknown error occurred.
    
           The behavior is undefined if the _locale_ argument to _strerror_l_()
           is the special locale object LC_GLOBAL_LOCALE or is not a valid
           locale object handle.
    


## RETURN VALUE



           Upon completion, whether successful or not, _strerror_() shall
           return a pointer to the generated message string.  On error _errno_
           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. If _errnum_ is not a valid error
           number, _errno_ may be set to **[EINVAL]** , but a pointer to a message
           string shall still be returned. If any other error 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 the
           error.
    


## ERRORS



           These functions may fail if:
    
           **EINVAL** The value of _errnum_ is neither a valid error number nor
                  zero.
    
           The _strerror_r_() function may fail if:
    
           **ERANGE** Insufficient storage was supplied via _strerrbuf_ and _buflen_
                  to contain the generated message string.
    
           _The following sections are informative._
    


## EXAMPLES



           None.
    


## APPLICATION USAGE



           Historically in some implementations, calls to _perror_() would
           overwrite the string that the pointer returned by _strerror_()
           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



           The _strerror_l_() function is required to be thread-safe, thereby
           eliminating the need for an equivalent to the _strerror_r_()
           function.
    
           Earlier versions of this standard did not explicitly require that
           the error message strings returned by _strerror_() and _strerror_r_()
           provide any information about the error. This version of the
           standard requires a meaningful message for any successful
           completion.
    
           Since no return value is reserved to indicate a _strerror_() error,
           but all calls (whether successful or not) must return a pointer to
           a message string, on error _strerror_() can return a pointer to an
           empty string or a pointer to a meaningful string that can be
           printed.
    
           Note that the **[EINVAL]** error condition is a may fail error. If an
           invalid error number is supplied as the value of _errnum_ ,
           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"**
               (where _xxx_ is the value of _errnum_).
    
            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"**(where _xxx_ is the value of _errnum_).  Since
               applications frequently use the return value of _strerror_() as
               an argument to functions like _fprintf_() (without checking the
               return value) and since applications have no way to parse an
               error message string to determine whether _errnum_ represents a
               valid error number, implementations are encouraged to
               implement #3. Similarly, implementations are encouraged to
               have _strerror_r_() return **[EINVAL]** and put a string like
               **"unknown** error" or **"error** number**xxx"** in the buffer pointed to by
               _strerrbuf_ when the value of _errnum_ is not a valid error
               number.
    
           Some applications rely on being able to set _errno_ to 0 before
           calling a function with no reserved value to indicate an error,
           then call _strerror_(_errno_) afterwards to detect whether an error
           occurred (because _errno_ changed) or to indicate success (because
           _errno_ remained zero). This usage pattern requires that **strerror**(0)
           succeed with useful results. Previous versions of the standard did
           not specify the behavior when _errnum_ is zero.
    


## FUTURE DIRECTIONS



           None.
    


* * *