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