xref: /openbsd-src/gnu/usr.bin/texinfo/lib/strerror.c (revision a1acfa9b69ad64eb720639240c8438f11107dc85)
1*a1acfa9bSespie /* strerror.c --- ANSI C compatible system error routine
2840175f0Skstailey 
3*a1acfa9bSespie    Copyright (C) 1986, 1988, 1989, 1991, 2002, 2003 Free Software
4*a1acfa9bSespie    Foundation, Inc.
5840175f0Skstailey 
6*a1acfa9bSespie    This program is free software; you can redistribute it and/or modify
7*a1acfa9bSespie    it under the terms of the GNU General Public License as published by
8*a1acfa9bSespie    the Free Software Foundation; either version 2, or (at your option)
9*a1acfa9bSespie    any later version.
10*a1acfa9bSespie 
11*a1acfa9bSespie    This program is distributed in the hope that it will be useful,
12*a1acfa9bSespie    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*a1acfa9bSespie    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*a1acfa9bSespie    GNU General Public License for more details.
15*a1acfa9bSespie 
16*a1acfa9bSespie    You should have received a copy of the GNU General Public License
17*a1acfa9bSespie    along with this program; if not, write to the Free Software Foundation,
18*a1acfa9bSespie    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19*a1acfa9bSespie 
20*a1acfa9bSespie #if HAVE_CONFIG_H
21*a1acfa9bSespie # include <config.h>
22840175f0Skstailey #endif
23840175f0Skstailey 
24*a1acfa9bSespie #include <limits.h>
25*a1acfa9bSespie 
26*a1acfa9bSespie /* Don't include <stdio.h>, since it may or may not declare
27*a1acfa9bSespie    sys_errlist and its declarations may collide with ours.  Just
28*a1acfa9bSespie    declare the stuff that we need directly.  Standard hosted C89
29*a1acfa9bSespie    implementations define strerror and they don't need this strerror
30*a1acfa9bSespie    function, so take some liberties with the standard to cater to
31*a1acfa9bSespie    ancient or limited freestanding implementations.  */
32*a1acfa9bSespie int sprintf (char *, char const *, ...);
33840175f0Skstailey extern int sys_nerr;
34840175f0Skstailey extern char *sys_errlist[];
35840175f0Skstailey 
36840175f0Skstailey char *
strerror(int n)37*a1acfa9bSespie strerror (int n)
38840175f0Skstailey {
39*a1acfa9bSespie   static char const fmt[] = "Unknown error (%d)";
40*a1acfa9bSespie   static char mesg[sizeof fmt + sizeof n * CHAR_BIT / 3];
41840175f0Skstailey 
42*a1acfa9bSespie   if (n < 0 || n >= sys_nerr)
43*a1acfa9bSespie     {
44*a1acfa9bSespie       sprintf (mesg, fmt, n);
45840175f0Skstailey       return mesg;
46*a1acfa9bSespie     }
47*a1acfa9bSespie   else
48840175f0Skstailey     return sys_errlist[n];
49840175f0Skstailey }
50