libc/misc/err.c:add err.c to libc

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
This commit is contained in:
anjiahao 2021-11-25 16:44:06 +08:00 committed by David Sidrane
parent c39ef4420e
commit f9570810c0
4 changed files with 223 additions and 0 deletions

57
include/err.h Normal file
View File

@ -0,0 +1,57 @@
/****************************************************************************
* include/err.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __INCLUDE_ERR_H
#define __INCLUDE_ERR_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdarg.h>
#include <nuttx/compiler.h>
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef CONFIG_LIBC_ERR
/* Print "pid: ", FORMAT, ": ", the standard error string for errno,
* and a newline, on stderr.
*/
void warn(FAR const char *fmt, ...);
void vwarn(FAR const char *fmt, va_list ap);
/* Likewise, but without ": " and the standard error string. */
void warnx(FAR const char *fmt, ...);
void vwarnx(FAR const char *fmt, va_list ap);
/* Likewise, and then exit with STATUS. */
void err(int status, FAR const char *fmt, ...);
void verr(int status, FAR const char *fmt, va_list ap);
void errx(int status, FAR const char *fmt, ...);
void verrx(int status, FAR const char *, va_list ap);
#endif /* CONFIG_LIBC_ERR */
#endif /* __INCLUDE_ERR_H */

View File

@ -61,3 +61,9 @@ config LIBC_ENVPATH
---help---
Use the contents of the common environment variable to locate executable
or library files. Default: n
config LIBC_ERR
bool "Support err() verr() verrx() warn() vwarn() vwarnx()"
default y
---help---
Support err() verr() verrx() warn() vwarn() vwarnx(). Default: y

View File

@ -56,6 +56,10 @@ ifeq ($(CONFIG_LIBC_ENVPATH),y)
CSRCS += lib_envpath.c
endif
ifeq ($(CONFIG_LIBC_ERR),y)
CSRCS += lib_err.c
endif
# To ensure uname information is newest,
# add lib_utsname.o to phony target for force rebuild

156
libs/libc/misc/lib_err.c Normal file
View File

@ -0,0 +1,156 @@
/****************************************************************************
* libs/libc/misc/lib_err.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define VA(call) \
do \
{ \
va_list ap; \
va_start(ap, fmt); \
call; \
va_end(ap); \
} while(0)
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: vwarn
****************************************************************************/
void vwarn(FAR const char *fmt, va_list ap)
{
int error = errno;
struct va_format vaf;
#ifdef va_copy
va_list copy;
va_copy(copy, ap);
vaf.fmt = fmt;
vaf.va = &copy;
#else
vaf.fmt = fmt;
vaf.va = &ap;
#endif
#ifdef CONFIG_FILE_STREAM
fprintf(stderr, "%d: %pV: %s\n", getpid(), &vaf, strerror(error));
#else
dprintf(STDERR_FILENO, "%d: %pV: %s\n", getpid(), &vaf, strerror(error));
#endif
}
/****************************************************************************
* Name: vwarnx
****************************************************************************/
void vwarnx(FAR const char *fmt, va_list ap)
{
struct va_format vaf;
#ifdef va_copy
va_list copy;
va_copy(copy, ap);
vaf.fmt = fmt;
vaf.va = &copy;
#else
vaf.fmt = fmt;
vaf.va = &ap;
#endif
#ifdef CONFIG_FILE_STREAM
fprintf(stderr, "%d: %pV\n", getpid(), &vaf);
#else
dprintf(STDERR_FILENO, "%d: %pV\n", getpid(), &vaf);
#endif
}
/****************************************************************************
* Name: warn
****************************************************************************/
void warn(FAR const char *fmt, ...)
{
VA(vwarn(fmt, ap));
}
/****************************************************************************
* Name: warnx
****************************************************************************/
void warnx(FAR const char *fmt, ...)
{
VA(vwarnx(fmt, ap));
}
/****************************************************************************
* Name: verr
****************************************************************************/
void verr(int status, FAR const char *fmt, va_list ap)
{
vwarn(fmt, ap);
exit(status);
}
/****************************************************************************
* Name: verrx
****************************************************************************/
void verrx(int status, FAR const char *fmt, va_list ap)
{
vwarnx(fmt, ap);
exit(status);
}
/****************************************************************************
* Name: err
****************************************************************************/
void err(int status, FAR const char *fmt, ...)
{
VA(verr(status, fmt, ap));
}
/****************************************************************************
* Name: errx
****************************************************************************/
void errx(int status, FAR const char *fmt, ...)
{
VA(verrx(status, fmt, ap));
}