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