NetBSD-Bugs archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: lib/48521: err.h / err(3): add missing errc(3)/verrc(3)/warnc(3)/vwarnc(3) family
The following reply was made to PR lib/48521; it has been noted by GNATS.
From: Steffen (Daode) Nurpmeso <sdaoden%gmail.com@localhost>
To: gnats-bugs%NetBSD.org@localhost
Cc:
Subject: Re: lib/48521: err.h / err(3): add missing
errc(3)/verrc(3)/warnc(3)/vwarnc(3) family
Date: Thu, 16 Jan 2014 16:51:05 +0100
This is a multi-part message in MIME format.
--=_01389887465=-q9urn8VTFbMsFCeDxKaE9KRQKYOGzT=_
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
I'll append a version that i deem to be better, since it ensures
the message is written as a unity even in multithreaded+ programs.
It's not in NetBSD style, though.
It would be nice if anything would go forward in here.
Thanks and ciao,
--steffen
--- /dev/null 2014-01-16 12:45:53.000000000 +0100
+++ err.h 2014-01-16 12:14:10.000000000 +0100
@@ -0,0 +1,39 @@
+
+#ifndef _ERR_H
+#define _ERR_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdarg.h>
+
+#ifndef _Noreturn
+# define __ERR_H_DEFINED_NORETURN
+# define _Noreturn
+#endif
+
+/* Program exit and warning calls */
+_Noreturn void err(int, const char *, ...);
+_Noreturn void verr(int, const char *, va_list);
+_Noreturn void errc(int, int, const char *, ...);
+_Noreturn void verrc(int, int, const char *, va_list);
+_Noreturn void errx(int, const char *, ...);
+_Noreturn void verrx(int, const char *, va_list);
+
+void warn(const char *, ...);
+void vwarn(const char *, va_list);
+void warnc(int, const char *, ...);
+void vwarnc(int, const char *, va_list);
+void warnx(const char *, ...);
+void vwarnx(const char *, va_list);
+
+#ifdef __ERR_H_DEFINED_NORETURN
+# undef _Noreturn
+# undef __ERR_H_DEFINED_NORETURN
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* _ERR_H */
--- /dev/null 2014-01-16 12:45:57.000000000 +0100
+++ err.c 2014-01-16 12:44:52.000000000 +0100
@@ -0,0 +1,174 @@
+
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+
+/* Thread-safe locked with multiple I/Os, or a single output?
+ * Is used as the stack buffer size if defined */
+/*#define __PREPARE_STACK_BUFFER 1024*/
+
+/* Get the program name as a const char* */
+#define __PROGRAM_NAME() getprogname()
+
+/* The shared workhorse */
+static void _errwarn(FILE *fp, int doerr, int code, const char *fmt,
+ va_list args);
+
+static void
+_errwarn(FILE *fp, int doerr, int code, const char *fmt, va_list args)
+{
+#ifdef __PREPARE_STACK_BUFFER
+ char buffer[__PREPARE_STACK_BUFFER];
+ size_t len;
+ int i;
+
+ i = snprintf(buffer, sizeof buffer, "%s: ", __PROGRAM_NAME());
+ if (i < 0)
+ goto jleave;
+ len = i;
+ if (len >= sizeof buffer)
+ goto jprint;
+
+ if (fmt != NULL) {
+ i = vsnprintf(buffer + len, sizeof(buffer) - len, fmt, args);
+ if (i < 0)
+ goto jleave;
+ len += i;
+ if (len >= sizeof buffer)
+ goto jprint;
+ }
+
+ if (doerr) {
+ i = snprintf(buffer + len, sizeof(buffer) - len, ": %s",
+ strerror(code));
+ if (i < 0)
+ goto jleave;
+ len += i;
+ }
+jprint:
+ if (len >= sizeof(buffer) - 1)
+ len = sizeof(buffer) - 2;
+ buffer[len++] = '\n';
+ buffer[len] = '\0';
+ fputs(buffer, fp);
+ fflush(fp);
+jleave:
+ ;
+
+#else /* __PREPARE_STACK_BUFFER */
+ flockfile(fp);
+ fprintf(fp, "%s: ", __PROGRAM_NAME());
+ if (fmt != NULL)
+ vfprintf(fp, fmt, args);
+ if (doerr)
+ fprintf(fp, ": %s", strerror(code));
+ fputc('\n', fp);
+ fflush(fp);
+ funlockfile(fp);
+#endif
+}
+
+void
+err(int status, const char *fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ _errwarn(stderr, 1, errno, fmt, args);
+ va_end(args);
+ exit(status);
+}
+
+void
+verr(int status, const char *fmt, va_list args)
+{
+ _errwarn(stderr, 1, errno, fmt, args);
+ exit(status);
+}
+
+void
+errc(int status, int code, const char *fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ _errwarn(stderr, 1, code, fmt, args);
+ va_end(args);
+ exit(status);
+}
+
+void
+verrc(int status, int code, const char *fmt, va_list args)
+{
+ _errwarn(stderr, 1, code, fmt, args);
+ exit(status);
+}
+
+void
+errx(int status, const char *fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ _errwarn(stderr, 0, 0, fmt, args);
+ va_end(args);
+ exit(status);
+}
+
+void
+verrx(int status, const char *fmt, va_list args)
+{
+ _errwarn(stderr, 0, 0, fmt, args);
+ exit(status);
+}
+
+void
+warn(const char *fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ _errwarn(stderr, 1, errno, fmt, args);
+ va_end(args);
+}
+
+void
+vwarn(const char *fmt, va_list args)
+{
+ _errwarn(stderr, 1, errno, fmt, args);
+}
+
+void
+warnc(int code, const char *fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ _errwarn(stderr, 1, code, fmt, args);
+ va_end(args);
+}
+
+void
+vwarnc(int code, const char *fmt, va_list args)
+{
+ _errwarn(stderr, 1, code, fmt, args);
+}
+
+void
+warnx(const char *fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ _errwarn(stderr, 0, 0, fmt, args);
+ va_end(args);
+}
+
+void
+vwarnx(const char *fmt, va_list args)
+{
+ _errwarn(stderr, 0, 0, fmt, args);
+}
--=_01389887465=-q9urn8VTFbMsFCeDxKaE9KRQKYOGzT=_
Content-Type: message/rfc822
Content-Disposition: inline
Content-Description: Original message content
Delivered-To: sdaoden%gmail.com@localhost
Received: by 10.42.112.10 with SMTP id w10csp239559icp; Tue, 14 Jan 2014
05:45:03 -0800 (PST)
X-Received: by 10.236.155.100 with SMTP id i64mr32997658yhk.42.1389707103076;
Tue, 14 Jan 2014 05:45:03 -0800 (PST)
Return-Path: <gnats%netbsd.org@localhost>
Received: from mollari.NetBSD.org (mollari.NetBSD.org.
[2001:4f8:3:7:230:48ff:fed3:af12]) by mx.google.com with ESMTPS id
q69si891705yhd.70.2014.01.14.05.45.02 for <sdaoden%gmail.com@localhost>
(version=TLSv1
cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 14 Jan 2014 05:45:02 -0800 (PST)
Received-SPF: pass (google.com: best guess record for domain of
gnats%netbsd.org@localhost designates 2001:4f8:3:7:230:48ff:fed3:af12 as
permitted
sender) client-ip=2001:4f8:3:7:230:48ff:fed3:af12;
Authentication-Results: mx.google.com; spf=pass (google.com: best guess record
for domain of gnats%netbsd.org@localhost designates
2001:4f8:3:7:230:48ff:fed3:af12 as
permitted sender) smtp.mail=gnats%netbsd.org@localhost
Received: by mollari.NetBSD.org (Postfix, from userid 31008) id 13A62A646D;
Tue, 14 Jan 2014 13:45:01 +0000 (UTC)
From: gnats-admin%netbsd.org@localhost
To: sdaoden%gmail.com@localhost
Reply-To: gnats-bugs%NetBSD.org@localhost
Subject: Re: lib/48521: err.h / err(3): add missing
errc(3)/verrc(3)/warnc(3)/vwarnc(3) family
References: <pr-lib-48521%gnats.netbsd.org@localhost>
<20140113183144./Vt1NahZJgHkEFo7WinnlfLC@dietcurd.local>
In-Reply-To: <20140113183144./Vt1NahZJgHkEFo7WinnlfLC@dietcurd.local>
Message-Id: <20140114134501.13A62A646D%mollari.NetBSD.org@localhost>
Date: Tue, 14 Jan 2014 13:45:01 +0000 (UTC)
Status: RO
Thank you very much for your problem report.
It has the internal identification `lib/48521'.
The individual assigned to look at your
report is: lib-bug-people.
>Category: lib
>Responsible: lib-bug-people
>Synopsis: See subject
>Arrival-Date: Tue Jan 14 13:45:00 +0000 2014
--=_01389887465=-q9urn8VTFbMsFCeDxKaE9KRQKYOGzT=_--
Home |
Main Index |
Thread Index |
Old Index