Add perror()

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5061 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo
2012-08-28 19:01:14 +00:00
parent da30d409a0
commit 94a048002b
9 changed files with 382 additions and 17 deletions
+42 -2
View File
@@ -23,7 +23,7 @@ config NUNGET_CHARS
---help---
Number of characters that can be buffered by ungetc() (Only if NFILE_STREAMS > 0)
config CONFIG_LIB_HOMEDIR
config LIB_HOMEDIR
string "Home directory"
default "/"
depends on !DISABLE_ENVIRON
@@ -51,6 +51,46 @@ config LIBC_FLOATINGPOINT
By default, floating point
support in printf, sscanf, etc. is disabled.
config LIBC_STRERROR
bool "Enable strerror"
default n
---help---
strerror() is useful because it decodes 'errno' values into a human readable
strings. But it can also require a lot of memory. If this option is selected,
strerror() will still exist in the build but it will not decode error values.
This option should be used by other logic to decide if it should use strerror()
or not. For example, the NSH application will not use strerror() if this
option is not selected; perror() will not use strerror() is this option is not
selected (see also NSH_STRERROR).
config LIBC_STRERROR_SHORT
bool "Use short error descriptions in strerror()"
default n
depends on LIBC_STRERROR
---help---
If this option is selected, then strerror() will use a shortened string when
it decodes the error. Specifically, strerror() is simply use the string that
is the common name for the error. For example, the 'errno' value of 2 will
produce the string "No such file or directory" is LIBC_STRERROR_SHORT
is not defined but the string "ENOENT" is LIBC_STRERROR_SHORT is defined.
config LIBC_PERROR_STDOUT
bool "perror() to stdout"
default n
---help---
POSIX requires that perror() provide its output on stderr. This option may
be defined, however, to provide perror() output that is serialized with
other stdout messages.
config LIBC_PERROR_DEVNAME
string "perror() to device"
default "/dev/console"
depends on !LIBC_PERROR_STDOUT
---help---
Another non-standard option is to provide perror() output to a logging device
or file. LIBC_PERROR_DEVNAME may be defined to be any write-able,
character device (or file).
config ARCH_LOWPUTC
bool "Low-level console output"
default "y"
@@ -68,7 +108,7 @@ config ENABLE_ARCH_OPTIMIZED_FUN
The architecture may provide custom versions of certain
standard header files:
config ARCH_MATH_H, CONFIG_ARCH_STDBOOL_H, CONFIG_ARCH_STDINT_H
config ARCH_MATH_H, ARCH_STDBOOL_H, ARCH_STDINT_H
if ENABLE_ARCH_OPTIMIZED_FUN
config ARCH_MEMCPY
+130
View File
@@ -0,0 +1,130 @@
/****************************************************************************
* lib/stdio/lib_perror.c
*
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <errno.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* POSIX requires that perror provide its output on stderr. This option may
* be defined, however, to provide perror output that is serialized with
* other stdout messages.
*/
#ifdef CONFIG_LIBC_PERROR_STDOUT
# define PERROR_STREAM stdout
# undef CONFIG_LIBC_PERROR_DEVNAME
#endif
/* Another non-standard option is to provide perror output to a logging
* device or file. CONFIG_LIBC_PERROR_DEVNAME may be defined to be any write-
* able, character device (or file).
*/
#ifndef CONFIG_LIBC_PERROR_DEVNAME
# define PERROR_STREAM stderr
#else
# define PERROR_STREAM perror_stream;
#endif
/****************************************************************************
* Private Type Declarations
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
#ifndef CONFIG_LIBC_PERROR_DEVNAME
static FILE *perror_stream;
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: perror
****************************************************************************/
void perror(FAR const char *s)
{
/* If we are using a custom output device (something other than
* /dev/console), then make sure that the device has been opened.
*/
#ifdef CONFIG_LIBC_PERROR_DEVNAME
if (!perror_stream)
{
/* Not yet.. open it now */
perror_stream = fopen(CONFIG_LIBC_PERROR_DEVNAME, "w");
if (!perror_stream)
{
/* Oops... we couldn't open the device */
return;
}
}
#endif
/* If strerror() is not enabled, then just print the error number */
#ifdef CONFIG_LIBC_STRERROR
(void)fprintf(PERROR_STREAM, "%s: %s\n", s, strerror(errno));
#else
(void)fprintf(PERROR_STREAM, "%s: Error %d\n", s, errno);
#endif
}
+141 -3
View File
@@ -1,8 +1,8 @@
/************************************************************************
* lib/string/lib_strerror.c
*
* Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* Copyright (C) 2007, 2009, 2011-2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -61,6 +61,8 @@ struct errno_strmap_s
* Private Data
************************************************************************/
#ifdef CONFIG_LIBC_STRERROR
/* This table maps all error numbers to descriptive strings.
* The only assumption that the code makes with regard to this
* this table is that it is ordered by error number.
@@ -70,6 +72,8 @@ struct errno_strmap_s
* strings.
*/
#ifndef CONFIG_LIBC_STRERROR_SHORT
static const struct errno_strmap_s g_errnomap[] =
{
{ EPERM, EPERM_STR },
@@ -196,8 +200,140 @@ static const struct errno_strmap_s g_errnomap[] =
{ EMEDIUMTYPE, EMEDIUMTYPE_STR }
};
#else /* CONFIG_LIBC_STRERROR_SHORT */
static const struct errno_strmap_s g_errnomap[] =
{
{ EPERM, "EPERM" },
{ ENOENT, "ENOENT" },
{ ESRCH, "ESRCH" },
{ EINTR, "EINTR" },
{ EIO, "EIO" },
{ ENXIO, "ENXIO" },
{ E2BIG, "E2BIG" },
{ ENOEXEC, "ENOEXEC" },
{ EBADF, "EBADF" },
{ ECHILD, "ECHILD" },
{ EAGAIN, "EAGAIN" },
{ ENOMEM, "ENOMEM" },
{ EACCES, "EACCES" },
{ EFAULT, "EFAULT" },
{ ENOTBLK, "ENOTBLK" },
{ EBUSY, "EBUSY" },
{ EEXIST, "EEXIST" },
{ EXDEV, "EXDEV" },
{ ENODEV, "ENODEV" },
{ ENOTDIR, "ENOTDIR" },
{ EISDIR, "EISDIR" },
{ EINVAL, "EINVAL" },
{ ENFILE, "ENFILE" },
{ EMFILE, "EMFILE" },
{ ENOTTY, "ENOTTY" },
{ ETXTBSY, "ETXTBSY" },
{ EFBIG, "EFBIG" },
{ ENOSPC, "ENOSPC" },
{ ESPIPE, "ESPIPE" },
{ EROFS, "EROFS" },
{ EMLINK, "EMLINK" },
{ EPIPE, "EPIPE" },
{ EDOM, "EDOM" },
{ ERANGE, "ERANGE" },
{ EDEADLK, "EDEADLK" },
{ ENAMETOOLONG, "ENAMETOOLONG" },
{ ENOLCK, "ENOLCK" },
{ ENOSYS, "ENOSYS" },
{ ENOTEMPTY, "ENOTEMPTY" },
{ ELOOP, "ELOOP" },
{ ENOMSG, "ENOMSG" },
{ EIDRM, "EIDRM" },
{ ECHRNG, "ECHRNG" },
{ EL2NSYNC, "EL2NSYNC" },
{ EL3HLT, "EL3HLT" },
{ EL3RST, "EL3RST" },
{ EL3RST, "EL3RST" },
{ EUNATCH, "EUNATCH" },
{ ENOCSI, "ENOCSI" },
{ EL2HLT, "EL2HLT" },
{ EBADE, "EBADE" },
{ EBADR, "EBADR" },
{ EXFULL, "EXFULL" },
{ ENOANO, "ENOANO" },
{ EBADRQC, "EBADRQC" },
{ EBADSLT, "EBADSLT" },
{ EBFONT, "EBFONT" },
{ ENOSTR, "ENOSTR" },
{ ENODATA, "ENODATA" },
{ ETIME, "ETIME" },
{ ENOSR, "ENOSR" },
{ ENONET, "ENONET" },
{ ENOPKG, "ENOPKG" },
{ EREMOTE, "EREMOTE" },
{ ENOLINK, "ENOLINK" },
{ EADV, "EADV" },
{ ESRMNT, "ESRMNT" },
{ ECOMM, "ECOMM" },
{ EPROTO, "EPROTO" },
{ EMULTIHOP, "EMULTIHOP" },
{ EDOTDOT, "EDOTDOT" },
{ EBADMSG, "EBADMSG" },
{ EOVERFLOW, "EOVERFLOW" },
{ ENOTUNIQ, "ENOTUNIQ" },
{ EBADFD, "EBADFD" },
{ EREMCHG, "EREMCHG" },
{ ELIBACC, "ELIBACC" },
{ ELIBBAD, "ELIBBAD" },
{ ELIBSCN, "ELIBSCN" },
{ ELIBMAX, "ELIBMAX" },
{ ELIBEXEC, "ELIBEXEC" },
{ EILSEQ, "EILSEQ" },
{ ERESTART, "ERESTART" },
{ ESTRPIPE, "ESTRPIPE" },
{ EUSERS, "EUSERS" },
{ ENOTSOCK, "ENOTSOCK" },
{ EDESTADDRREQ, "EDESTADDRREQ" },
{ EMSGSIZE, "EMSGSIZE" },
{ EPROTOTYPE, "EPROTOTYPE" },
{ ENOPROTOOPT, "ENOPROTOOPT" },
{ EPROTONOSUPPORT, "EPROTONOSUPPORT" },
{ ESOCKTNOSUPPORT, "ESOCKTNOSUPPORT" },
{ EOPNOTSUPP, "EOPNOTSUPP" },
{ EPFNOSUPPORT, "EPFNOSUPPORT" },
{ EAFNOSUPPORT, "EAFNOSUPPORT" },
{ EADDRINUSE, "EADDRINUSE" },
{ EADDRNOTAVAIL, "EADDRNOTAVAIL" },
{ ENETDOWN, "ENETDOWN" },
{ ENETUNREACH, "ENETUNREACH" },
{ ENETRESET, "ENETRESET" },
{ ECONNABORTED, "ECONNABORTED" },
{ ECONNRESET, "ECONNRESET" },
{ ENOBUFS, "ENOBUFS" },
{ EISCONN, "EISCONN" },
{ ENOTCONN, "ENOTCONN" },
{ ESHUTDOWN, "ESHUTDOWN" },
{ ETOOMANYREFS, "ETOOMANYREFS" },
{ ETIMEDOUT, "ETIMEDOUT" },
{ ECONNREFUSED, "ECONNREFUSED" },
{ EHOSTDOWN, "EHOSTDOWN" },
{ EHOSTUNREACH, "EHOSTUNREACH" },
{ EALREADY, "EALREADY" },
{ EINPROGRESS, "EINPROGRESS" },
{ ESTALE, "ESTALE" },
{ EUCLEAN, "EUCLEAN" },
{ ENOTNAM, "ENOTNAM" },
{ ENAVAIL, "ENAVAIL" },
{ EISNAM, "EISNAM" },
{ EREMOTEIO, "EREMOTEIO" },
{ EDQUOT, "EDQUOT" },
{ ENOMEDIUM, "ENOMEDIUM" },
{ EMEDIUMTYPE, "EMEDIUMTYPE" }
};
#endif /* CONFIG_LIBC_STRERROR_SHORT */
#define NERRNO_STRS (sizeof(g_errnomap) / sizeof(struct errno_strmap_s))
#endif /* CONFIG_LIBC_STRERROR */
/************************************************************************
* Private Functions
************************************************************************/
@@ -210,8 +346,9 @@ static const struct errno_strmap_s g_errnomap[] =
* Name: strerror
************************************************************************/
const char *strerror(int errnum)
FAR const char *strerror(int errnum)
{
#ifdef CONFIG_LIBC_STRERROR
int ndxlow = 0;
int ndxhi = NERRNO_STRS - 1;
int ndxmid;
@@ -233,5 +370,6 @@ const char *strerror(int errnum)
}
}
while (ndxlow <= ndxhi);
#endif
return "Unknown error";
}