# HG changeset patch # User David Demelier # Date 1329152767 -3600 # Node ID bb2694382675249a6c7bd90a282efe1d5497451a # Parent d3ccd7232e2d1ff95e75a46808dcb7b66c99909b Move err.[ch] to port/ directory diff -r d3ccd7232e2d -r bb2694382675 err.c --- a/err.c Mon Feb 13 14:49:50 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,114 +0,0 @@ -/* - * err.c -- formtted error messages (portable version) - * - * Copyright (c) 2011, 2012, David Demelier - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include -#include -#include -#include - -#include "err.h" - -/* - * These functions implements at least the same functions that can be found - * in the NetBSD err(3) man page without printing the programe name due to - * a portability issue. - */ - -void -err(int val, const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - verr(val, fmt, ap); - va_end(ap); -} - -void -verr(int val, const char *fmt, va_list ap) -{ - if (fmt) { - vfprintf(stderr, fmt, ap); - fprintf(stderr, ": "); - } - - fprintf(stderr, "%s\n", strerror(errno)); - exit(val); -} - -void -errx(int val, const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - verrx(val, fmt, ap); - va_end(ap); -} - -void -verrx(int val, const char *fmt, va_list ap) -{ - if (fmt) - vfprintf(stderr, fmt, ap); - - fprintf(stderr, "\n"); - - exit(val); -} - -void -warn(const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - vwarn(fmt, ap); - va_end(ap); -} - -void -vwarn(const char *fmt, va_list ap) -{ - if (fmt) { - vfprintf(stderr, fmt, ap); - fprintf(stderr, ": "); - } - - fprintf(stderr, "%s\n", strerror(errno)); -} - -void -warnx(const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - vwarnx(fmt, ap); - va_end(ap); -} - -void -vwarnx(const char *fmt, va_list ap) -{ - if (fmt) - vfprintf(stderr, fmt, ap); - - fprintf(stderr, "\n"); -} diff -r d3ccd7232e2d -r bb2694382675 err.h --- a/err.h Mon Feb 13 14:49:50 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,82 +0,0 @@ -/* - * err.h -- formtted error messages (portable version) - * - * Copyright (c) 2011, 2012, David Demelier - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef _ERR_H_ -#define _ERR_H_ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/* - * Attribute noreturn may helps. This may produce a warning with GCC 4.5: - * - * int a; - * - * if ((a = getstate() < 0) - * errx(1, "State failed"); - * - * Because compilator does not know that errx will call exit may produce - * a warning like `a may be used uninitialized'. - */ - -#if defined(__GNUC__) -# define __at_noreturn __attribute__ ((noreturn)) -#elif defined(_MSC_VER) -# define __at_noreturn __declspec(noreturn) -#endif - -/* - * err() functions append the format message to the stderr FILE pointer. They - * also append the system error using strerror(errno). Then the functions exit - * with the error code given as first argument. - */ - -void err(int, const char *, ...) __at_noreturn; -void verr(int, const char *, va_list) __at_noreturn; - -/* - * errx() functions are similar to err() except that they do not append the - * system error message. - */ - -void errx(int, const char *, ...) __at_noreturn; -void verrx(int, const char *, va_list) __at_noreturn; - -/* - * warn() functions are similar to err() but they do not call exit(). - */ - -void warn(const char *, ...); -void vwarn(const char *, va_list); - -/* - * warnx() functions are similar to warn() except that they do not append the - * system error message. - */ - -void warnx(const char *, ...); -void vwarnx(const char *, va_list); - -#ifdef __cplusplus -} -#endif - -#endif /* _ERR_H_ */ diff -r d3ccd7232e2d -r bb2694382675 port/err.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/port/err.c Mon Feb 13 18:06:07 2012 +0100 @@ -0,0 +1,114 @@ +/* + * err.c -- formtted error messages (portable version) + * + * Copyright (c) 2011, 2012, David Demelier + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include + +#include "err.h" + +/* + * These functions implements at least the same functions that can be found + * in the NetBSD err(3) man page without printing the programe name due to + * a portability issue. + */ + +void +err(int val, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + verr(val, fmt, ap); + va_end(ap); +} + +void +verr(int val, const char *fmt, va_list ap) +{ + if (fmt) { + vfprintf(stderr, fmt, ap); + fprintf(stderr, ": "); + } + + fprintf(stderr, "%s\n", strerror(errno)); + exit(val); +} + +void +errx(int val, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + verrx(val, fmt, ap); + va_end(ap); +} + +void +verrx(int val, const char *fmt, va_list ap) +{ + if (fmt) + vfprintf(stderr, fmt, ap); + + fprintf(stderr, "\n"); + + exit(val); +} + +void +warn(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vwarn(fmt, ap); + va_end(ap); +} + +void +vwarn(const char *fmt, va_list ap) +{ + if (fmt) { + vfprintf(stderr, fmt, ap); + fprintf(stderr, ": "); + } + + fprintf(stderr, "%s\n", strerror(errno)); +} + +void +warnx(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vwarnx(fmt, ap); + va_end(ap); +} + +void +vwarnx(const char *fmt, va_list ap) +{ + if (fmt) + vfprintf(stderr, fmt, ap); + + fprintf(stderr, "\n"); +} diff -r d3ccd7232e2d -r bb2694382675 port/err.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/port/err.h Mon Feb 13 18:06:07 2012 +0100 @@ -0,0 +1,82 @@ +/* + * err.h -- formtted error messages (portable version) + * + * Copyright (c) 2011, 2012, David Demelier + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef _ERR_H_ +#define _ERR_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Attribute noreturn may helps. This may produce a warning with GCC 4.5: + * + * int a; + * + * if ((a = getstate() < 0) + * errx(1, "State failed"); + * + * Because compilator does not know that errx will call exit may produce + * a warning like `a may be used uninitialized'. + */ + +#if defined(__GNUC__) +# define __at_noreturn __attribute__ ((noreturn)) +#elif defined(_MSC_VER) +# define __at_noreturn __declspec(noreturn) +#endif + +/* + * err() functions append the format message to the stderr FILE pointer. They + * also append the system error using strerror(errno). Then the functions exit + * with the error code given as first argument. + */ + +void err(int, const char *, ...) __at_noreturn; +void verr(int, const char *, va_list) __at_noreturn; + +/* + * errx() functions are similar to err() except that they do not append the + * system error message. + */ + +void errx(int, const char *, ...) __at_noreturn; +void verrx(int, const char *, va_list) __at_noreturn; + +/* + * warn() functions are similar to err() but they do not call exit(). + */ + +void warn(const char *, ...); +void vwarn(const char *, va_list); + +/* + * warnx() functions are similar to warn() except that they do not append the + * system error message. + */ + +void warnx(const char *, ...); +void vwarnx(const char *, va_list); + +#ifdef __cplusplus +} +#endif + +#endif /* _ERR_H_ */