Fix strerror on Interix.
[pspp] / lib / strerror.c
1 /* strerror.c --- ANSI C compatible system error routine
2
3    Copyright (C) 1986, 1988, 1989, 1991, 2002, 2003, 2006, 2007 Free
4    Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 #include <config.h>
21
22 #if REPLACE_STRERROR
23
24 # include <string.h>
25 # include <stdio.h>
26
27 # undef strerror
28
29 char *rpl_strerror (int n)
30 {
31   static char const fmt[] = "Unknown error (%d)";
32   static char mesg[sizeof fmt + sizeof n * CHAR_BIT / 3];
33
34   char *result = strerror (n);
35
36   if (! result)
37     {
38       sprintf (mesg, fmt, n);
39       return mesg;
40     }
41   return result;
42 }
43
44 #elif !HAVE_STRERROR
45
46 #include <limits.h>
47
48 /* Don't include <stdio.h>, since it may or may not declare
49    sys_errlist and its declarations may collide with ours.  Just
50    declare the stuff that we need directly.  Standard hosted C89
51    implementations define strerror and they don't need this strerror
52    function, so take some liberties with the standard to cater to
53    ancient or limited freestanding implementations.  */
54 int sprintf (char *, char const *, ...);
55 extern int sys_nerr;
56 extern char *sys_errlist[];
57
58 char *
59 strerror (int n)
60 {
61   static char const fmt[] = "Unknown error (%d)";
62   static char mesg[sizeof fmt + sizeof n * CHAR_BIT / 3];
63
64   if (n < 0 || n >= sys_nerr)
65     {
66       sprintf (mesg, fmt, n);
67       return mesg;
68     }
69   else
70     return sys_errlist[n];
71 }
72
73 #else
74
75 /* This declaration is solely to ensure that after preprocessing
76    this file is never empty.  */
77 typedef int dummy;
78
79 #endif