1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3 * (C) 2001 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
5 */
6
7 /* This would live in safestr.c, but it requires thread-private storage support
8 * from mpiimpl.h and friends. safestr.c is meant to be able to be used in
9 * different software packages, perhaps someday by moving it to MPL. */
10 #include "mpiimpl.h"
11
12 #if defined(HAVE_STRERROR_R) && defined(NEEDS_STRERROR_R_DECL)
13 #if defined(STRERROR_R_CHAR_P)
14 char *strerror_r(int errnum, char *strerrbuf, size_t buflen);
15 #else
16 int strerror_r(int errnum, char *strerrbuf, size_t buflen);
17 #endif
18 #endif
19
20 /* ideally, provides a thread-safe version of strerror */
MPIU_Strerror(int errnum)21 const char *MPIU_Strerror(int errnum)
22 {
23 #if defined(HAVE_STRERROR_R)
24 char *buf;
25 MPIU_THREADPRIV_DECL;
26 MPIU_THREADPRIV_GET;
27 buf = MPIU_THREADPRIV_FIELD(strerrbuf);
28 # if defined(STRERROR_R_CHAR_P)
29 /* strerror_r returns char ptr (old GNU-flavor). Static strings for known
30 * errnums are in returned buf, unknown errnums put a message in buf and
31 * return buf */
32 buf = strerror_r(errnum, buf, MPIU_STRERROR_BUF_SIZE);
33 # else
34 /* strerror_r returns an int */
35 strerror_r(errnum, buf, MPIU_STRERROR_BUF_SIZE);
36 # endif
37 return buf;
38
39 #elif defined(HAVE_STRERROR)
40 /* MT - not guaranteed to be thread-safe, but on may platforms it will be
41 * anyway for the most common cases (looking up an error string in a table
42 * of constants).
43 *
44 * Using a mutex here would be an option, but then you need a version
45 * without the mutex to call when interpreting errors from mutex functions
46 * themselves. */
47 return strerror(errnum);
48
49 #else
50 /* nowadays this case is most likely to happen because of a configure or
51 * internal header file inclusion bug rather than an actually missing
52 * strerror routine */
53 return "(strerror() unavailable on this platform)"
54
55 #endif
56 }
57
58