diff options
author | Eric Blake <eblake@redhat.com> | 2011-05-25 18:41:10 +0000 |
---|---|---|
committer | Eric Blake <eblake@redhat.com> | 2011-05-25 18:41:10 +0000 |
commit | 4805b60ccfeee32a4ac3547e680c917f9e5c1d39 (patch) | |
tree | 7035ae20768510551b6abb65549b1e51acd18e09 /newlib/libc/string/strerror.c | |
parent | 6215837523703c2c0b58200341ae3861d8b7a28d (diff) | |
download | cygnal-4805b60ccfeee32a4ac3547e680c917f9e5c1d39.tar.gz cygnal-4805b60ccfeee32a4ac3547e680c917f9e5c1d39.tar.bz2 cygnal-4805b60ccfeee32a4ac3547e680c917f9e5c1d39.zip |
strerror: allow user hook to comply with POSIX rules
* libc/string/strerror.c (strerror): Split body into...
(_strerror_r): ...new reentrant function.
* libc/string/u_strerr.c (_user_strerror): Update signature.
* libc/include/stdio.h (_strerror_r): New prototype.
* libc/posix/collate.c (__collate_err): Adjust callers.
* libc/stdio/perror.c (_perror_r): Likewise.
* libc/string/strerror_r.c (strerror_r): Likewise.
* libc/string/xpg_strerror_r.c (__xpg_strerror_r): Likewise.
Diffstat (limited to 'newlib/libc/string/strerror.c')
-rw-r--r-- | newlib/libc/string/strerror.c | 61 |
1 files changed, 43 insertions, 18 deletions
diff --git a/newlib/libc/string/strerror.c b/newlib/libc/string/strerror.c index 61e40ab7a..fd6edd9e2 100644 --- a/newlib/libc/string/strerror.c +++ b/newlib/libc/string/strerror.c @@ -15,6 +15,8 @@ INDEX ANSI_SYNOPSIS #include <string.h> char *strerror(int <[errnum]>); + char *_strerror_r(struct _reent <[ptr]>, int <[errnum]>, + int <[internal]>, int *<[error]>); TRAD_SYNOPSIS #include <string.h> @@ -288,6 +290,8 @@ Strings pipe error o- +<<_strerror_r>> is a reentrant version of the above. + RETURNS This function returns a pointer to a string. Your application must not modify that string. @@ -296,10 +300,10 @@ PORTABILITY ANSI C requires <<strerror>>, but does not specify the strings used for each error number. -Although this implementation of <<strerror>> is reentrant, ANSI C -declares that subsequent calls to <<strerror>> may overwrite the -result string; therefore portable code cannot depend on the reentrancy -of this subroutine. +Although this implementation of <<strerror>> is reentrant (depending +on <<_user_strerror>>), ANSI C declares that subsequent calls to +<<strerror>> may overwrite the result string; therefore portable +code cannot depend on the reentrancy of this subroutine. Although this implementation of <<strerror>> guarantees a non-null result with a NUL-terminator, some implementations return <<NULL>> @@ -317,15 +321,24 @@ extensibility. <<errno.h>> defines <[__ELASTERROR]>, which can be used as a base for user-defined error values. If the user supplies a routine named <<_user_strerror>>, and <[errnum]> passed to <<strerror>> does not match any of the supported values, -<<_user_strerror>> is called with <[errnum]> as its argument. - -<<_user_strerror>> takes one argument of type <[int]>, and returns a -character pointer. If <[errnum]> is unknown to <<_user_strerror>>, -<<_user_strerror>> returns <[NULL]>. The default <<_user_strerror>> -returns <[NULL]> for all input values. - -Note that <<_user_sterror>> must be thread-safe and not alter <<errno>> -if <<strerror_r>> is to comply with POSIX. +<<_user_strerror>> is called with three arguments. The first is of +type <[int]>, and is the <[errnum]> value unknown to <<strerror>>. +The second is of type <[int]>, and matches the <[internal]> argument +of <<_strerror_r>>; this should be zero if called from <<strerror>> +and non-zero if called from any other function; <<_user_strerror>> can +use this information to satisfy the POSIX rule that no other +standardized function can overwrite a static buffer reused by +<<strerror>>. The third is of type <[int *]>, and matches the +<[error]> argument of <<_strerror_r>>; if a non-zero value is stored +into that location (usually <[EINVAL]>), then <<strerror>> will set +<<errno>> to that value, and the XPG variant of <<strerror_r>> will +return that value instead of zero or <[ERANGE]>. <<_user_strerror>> +returns a <[char *]> value; returning <[NULL]> implies that the user +function did not choose to handle <[errnum]>. The default +<<_user_strerror>> returns <[NULL]> for all input values. Note that +<<_user_sterror>> must be thread-safe, and only denote errors via the +third argument rather than modifying <<errno>>, if <<strerror>> and +<<strerror_r>> are are to comply with POSIX. <<strerror>> requires no supporting OS subroutines. @@ -337,11 +350,14 @@ QUICKREF #include <string.h> char * -_DEFUN (strerror, (errnum), - int errnum) +_DEFUN (_strerror_r, (ptr, errnum, internal, errptr), + struct _reent *ptr _AND + int errnum _AND + int internal _AND + int *errptr) { char *error; - extern char *_user_strerror _PARAMS ((int)); + extern char *_user_strerror _PARAMS ((int, int, int *)); switch (errnum) { @@ -798,10 +814,19 @@ _DEFUN (strerror, (errnum), break; #endif default: - if ((error = _user_strerror (errnum)) == 0) - error = ""; + if (!errptr) + errptr = &ptr->_errno; + if ((error = _user_strerror (errnum, internal, errptr)) == 0) + error = ""; break; } return error; } + +char * +_DEFUN(strerror, (int), + int errnum) +{ + return _strerror_r (_REENT, errnum, 0, NULL); +} |