xref: /netbsd-src/external/gpl2/xcvs/dist/lib/strerror.c (revision 5a6c14c844c4c665da5632061aebde7bb2cb5766)
1 /* strerror.c --- ANSI C compatible system error routine
2 
3    Copyright (C) 1986, 1988, 1989, 1991, 2002, 2003 Free Software
4    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 #include <sys/cdefs.h>
20 __RCSID("$NetBSD: strerror.c,v 1.2 2016/05/17 14:00:09 christos Exp $");
21 
22 
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26 
27 #include <limits.h>
28 
29 /* Don't include <stdio.h>, since it may or may not declare
30    sys_errlist and its declarations may collide with ours.  Just
31    declare the stuff that we need directly.  Standard hosted C89
32    implementations define strerror and they don't need this strerror
33    function, so take some liberties with the standard to cater to
34    ancient or limited freestanding implementations.  */
35 int sprintf (char *, char const *, ...);
36 extern int sys_nerr;
37 extern char *sys_errlist[];
38 
39 char *
strerror(int n)40 strerror (int n)
41 {
42   static char const fmt[] = "Unknown error (%d)";
43   static char mesg[sizeof fmt + sizeof n * CHAR_BIT / 3];
44 
45   if (n < 0 || n >= sys_nerr)
46     {
47       sprintf (mesg, fmt, n);
48       return mesg;
49     }
50   else
51     return sys_errlist[n];
52 }
53