Change copyright notice from GPLv2+ to GPLv3+.
[gnulib.git] / 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 3 of the License, or
9    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
18
19 #include <config.h>
20
21 #if REPLACE_STRERROR
22
23 # include <string.h>
24 # include <stdio.h>
25
26 # undef strerror
27
28 char *rpl_strerror (int n)
29 {
30   static char const fmt[] = "Unknown error (%d)";
31   static char mesg[sizeof fmt + sizeof n * CHAR_BIT / 3];
32
33   char *result = strerror (n);
34
35   if (! result)
36     {
37       sprintf (mesg, fmt, n);
38       return mesg;
39     }
40   return result;
41 }
42
43 #elif !HAVE_STRERROR
44
45 #include <limits.h>
46
47 /* Don't include <stdio.h>, since it may or may not declare
48    sys_errlist and its declarations may collide with ours.  Just
49    declare the stuff that we need directly.  Standard hosted C89
50    implementations define strerror and they don't need this strerror
51    function, so take some liberties with the standard to cater to
52    ancient or limited freestanding implementations.  */
53 int sprintf (char *, char const *, ...);
54 extern int sys_nerr;
55 extern char *sys_errlist[];
56
57 char *
58 strerror (int n)
59 {
60   static char const fmt[] = "Unknown error (%d)";
61   static char mesg[sizeof fmt + sizeof n * CHAR_BIT / 3];
62
63   if (n < 0 || n >= sys_nerr)
64     {
65       sprintf (mesg, fmt, n);
66       return mesg;
67     }
68   else
69     return sys_errlist[n];
70 }
71
72 #else
73
74 /* This declaration is solely to ensure that after preprocessing
75    this file is never empty.  */
76 typedef int dummy;
77
78 #endif