chiark / gitweb /
xfoo => mfoo, add comment
[inn-innduct.git] / lib / strerror.c
1 /*  $Id: strerror.c 6127 2003-01-18 22:25:37Z rra $
2 **
3 **  Replacement for a missing strerror.
4 **
5 **  Written by Russ Allbery <rra@stanford.edu>
6 **  This work is hereby placed in the public domain by its author.
7 **
8 **  Provides the same functionality as the standard library routine strerror
9 **  for those platforms that don't have it (e.g. Ultrix).  Assume that we
10 **  have sys_nerr and sys_errlist available to use instead.  Calling
11 **  strerror should be thread-safe unless it is called for an unknown errno.
12 */
13
14 #include "config.h"
15
16 /* Our declarations of sys_nerr and sys_errlist may conflict with the ones
17    provided by stdio.h from glibc.  This trick hides the declarations in the
18    system header from the compiler while we test.  (The conflicts are just
19    whether or not to const, so there are no negative effects from using our
20    declarations.) */
21 #if TESTING
22 # define sys_nerr       hidden_sys_nerr
23 # define sys_errlist    hidden_sys_errlist
24 #endif
25
26 #include <errno.h>
27 #include <stdio.h>
28
29 #if TESTING
30 # undef sys_nerr
31 # undef sys_errlist
32 #endif
33
34 extern const int sys_nerr;
35 extern const char *sys_errlist[];
36
37 /* If we're running the test suite, rename strerror to avoid conflicts with
38    the system version. */
39 #if TESTING
40 # define strerror test_strerror
41 const char *test_strerror(int);
42 int snprintf(char *, size_t, const char *, ...);
43 #endif
44
45 const char *
46 strerror(int error)
47 {
48     static char buff[32];
49     int oerrno;
50
51     if (error >= 0 && error < sys_nerr)
52         return sys_errlist[error];
53     oerrno = errno;
54     snprintf(buff, sizeof(buff), "Error code %d", error);
55     errno = oerrno;
56     return buff;
57 }