Back to index

tetex-bin  3.0
strerror.c
Go to the documentation of this file.
00001 /* strerror.c --- ANSI C compatible system error routine
00002 
00003    Copyright (C) 1986, 1988, 1989, 1991, 2002, 2003 Free Software
00004    Foundation, Inc.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2, or (at your option)
00009    any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software Foundation,
00018    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00019 
00020 #if HAVE_CONFIG_H
00021 # include <config.h>
00022 #endif
00023 
00024 #include <limits.h>
00025 
00026 /* Don't include <stdio.h>, since it may or may not declare
00027    sys_errlist and its declarations may collide with ours.  Just
00028    declare the stuff that we need directly.  Standard hosted C89
00029    implementations define strerror and they don't need this strerror
00030    function, so take some liberties with the standard to cater to
00031    ancient or limited freestanding implementations.  */
00032 int sprintf (char *, char const *, ...);
00033 extern int sys_nerr;
00034 extern char *sys_errlist[];
00035 
00036 char *
00037 strerror (int n)
00038 {
00039   static char const fmt[] = "Unknown error (%d)";
00040   static char mesg[sizeof fmt + sizeof n * CHAR_BIT / 3];
00041 
00042   if (n < 0 || n >= sys_nerr)
00043     {
00044       sprintf (mesg, fmt, n);
00045       return mesg;
00046     }
00047   else
00048     return sys_errlist[n];
00049 }