Module Name:    src
Committed By:   dholland
Date:           Sat Mar 29 22:29:55 UTC 2014

Modified Files:
        src/games/hunt/huntd: Makefile answer.c driver.c hunt.h
Added Files:
        src/games/hunt/huntd: support.c

Log Message:
Reduce ifdefs by making a common function to talk to either syslogd or
stderr.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/games/hunt/huntd/Makefile
cvs rdiff -u -r1.17 -r1.18 src/games/hunt/huntd/answer.c
cvs rdiff -u -r1.31 -r1.32 src/games/hunt/huntd/driver.c
cvs rdiff -u -r1.26 -r1.27 src/games/hunt/huntd/hunt.h
cvs rdiff -u -r0 -r1.1 src/games/hunt/huntd/support.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/games/hunt/huntd/Makefile
diff -u src/games/hunt/huntd/Makefile:1.5 src/games/hunt/huntd/Makefile:1.6
--- src/games/hunt/huntd/Makefile:1.5	Sat Mar 29 21:27:40 2014
+++ src/games/hunt/huntd/Makefile	Sat Mar 29 22:29:55 2014
@@ -1,8 +1,8 @@
-#	$NetBSD: Makefile,v 1.5 2014/03/29 21:27:40 dholland Exp $
+#	$NetBSD: Makefile,v 1.6 2014/03/29 22:29:55 dholland Exp $
 
 PROG=	huntd
 SRCS=	answer.c draw.c driver.c execute.c expl.c \
-	extern.c makemaze.c shots.c terminal.c
+	extern.c makemaze.c shots.c support.c terminal.c
 MAN=	huntd.6
 
 CPPFLAGS+=-I${.CURDIR}/../include

Index: src/games/hunt/huntd/answer.c
diff -u src/games/hunt/huntd/answer.c:1.17 src/games/hunt/huntd/answer.c:1.18
--- src/games/hunt/huntd/answer.c:1.17	Sat Mar 29 19:41:10 2014
+++ src/games/hunt/huntd/answer.c	Sat Mar 29 22:29:55 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: answer.c,v 1.17 2014/03/29 19:41:10 dholland Exp $	*/
+/*	$NetBSD: answer.c,v 1.18 2014/03/29 22:29:55 dholland Exp $	*/
 /*
  * Copyright (c) 1983-2003, Regents of the University of California.
  * All rights reserved.
@@ -32,7 +32,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: answer.c,v 1.17 2014/03/29 19:41:10 dholland Exp $");
+__RCSID("$NetBSD: answer.c,v 1.18 2014/03/29 22:29:55 dholland Exp $");
 #endif /* not lint */
 
 #include <ctype.h>
@@ -79,11 +79,7 @@ answer(void)
 	{
 		if (errno == EINTR)
 			return false;
-#ifdef LOG
-		syslog(LOG_ERR, "accept: %m");
-#else
-		perror("accept");
-#endif
+		complain(LOG_ERR, "accept");
 		cleanup(1);
 	}
 

Index: src/games/hunt/huntd/driver.c
diff -u src/games/hunt/huntd/driver.c:1.31 src/games/hunt/huntd/driver.c:1.32
--- src/games/hunt/huntd/driver.c:1.31	Sat Mar 29 22:11:19 2014
+++ src/games/hunt/huntd/driver.c	Sat Mar 29 22:29:55 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: driver.c,v 1.31 2014/03/29 22:11:19 dholland Exp $	*/
+/*	$NetBSD: driver.c,v 1.32 2014/03/29 22:29:55 dholland Exp $	*/
 /*
  * Copyright (c) 1983-2003, Regents of the University of California.
  * All rights reserved.
@@ -32,7 +32,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: driver.c,v 1.31 2014/03/29 22:11:19 dholland Exp $");
+__RCSID("$NetBSD: driver.c,v 1.32 2014/03/29 22:29:55 dholland Exp $");
 #endif /* not lint */
 
 #include <sys/ioctl.h>
@@ -130,11 +130,7 @@ again:
 		while (poll(fdset, 3+MAXPL+MAXMON, INFTIM) < 0)
 		{
 			if (errno != EINTR)
-#ifdef LOG
-				syslog(LOG_WARNING, "poll: %m");
-#else
-				warn("poll");
-#endif
+				complain(LOG_WARNING, "poll");
 			errno = 0;
 		}
 #ifdef INTERNET
@@ -294,11 +290,7 @@ init(void)
 		if (errno == EADDRINUSE)
 			exit(0);
 		else {
-#ifdef LOG
-			syslog(LOG_ERR, "bind: %m");
-#else
-			warn("bind");
-#endif
+			complain(LOG_ERR, "bind");
 			cleanup(1);
 		}
 	}
@@ -307,11 +299,7 @@ init(void)
 #ifdef INTERNET
 	len = sizeof (SOCKET);
 	if (getsockname(Status, (struct sockaddr *) &Daemon, &len) < 0)  {
-#ifdef LOG
-		syslog(LOG_ERR, "getsockname: %m");
-#else
-		warn("getsockname");
-#endif
+		complain(LOG_ERR, "getsockname");
 		exit(1);
 	}
 	stat_port = ntohs(Daemon.sin_port);
@@ -333,21 +321,13 @@ init(void)
 #if defined(INTERNET)
 	msg = 1;
 	if (setsockopt(Socket, SOL_SOCKET, SO_USELOOPBACK, &msg, sizeof msg)<0)
-#ifdef LOG
-		syslog(LOG_WARNING, "setsockopt loopback %m");
-#else
-		warn("setsockopt loopback");
-#endif
+		complain(LOG_WARNING, "setsockopt loopback");
 #endif
 	if (bind(Socket, (struct sockaddr *) &Daemon, DAEMON_SIZE) < 0) {
 		if (errno == EADDRINUSE)
 			exit(0);
 		else {
-#ifdef LOG
-			syslog(LOG_ERR, "bind: %m");
-#else
-			warn("bind");
-#endif
+			complain(LOG_ERR, "bind");
 			cleanup(1);
 		}
 	}
@@ -356,11 +336,7 @@ init(void)
 #ifdef INTERNET
 	len = sizeof (SOCKET);
 	if (getsockname(Socket, (struct sockaddr *) &Daemon, &len) < 0)  {
-#ifdef LOG
-		syslog(LOG_ERR, "getsockname: %m");
-#else
-		warn("getsockname");
-#endif
+		complain(LOG_ERR, "getsockname");
 		exit(1);
 	}
 	sock_port = ntohs(Daemon.sin_port);
@@ -391,11 +367,7 @@ init(void)
 		Test_socket = socket(SOCK_FAMILY, SOCK_DGRAM, 0);
 		if (bind(Test_socket, (struct sockaddr *) &test_port,
 		    DAEMON_SIZE) < 0) {
-#ifdef LOG
-			syslog(LOG_ERR, "bind: %m");
-#else
-			warn("bind");
-#endif
+			complain(LOG_ERR, "bind");
 			exit(1);
 		}
 		(void) listen(Test_socket, 5);
@@ -891,20 +863,12 @@ send_stats(void)
 	if (s < 0) {
 		if (errno == EINTR)
 			return;
-#ifdef LOG
-		syslog(LOG_WARNING, "accept: %m");
-#else
-		warn("accept");
-#endif
+		complain(LOG_WARNING, "accept");
 		return;
 	}
 	fp = fdopen(s, "w");
 	if (fp == NULL) {
-#ifdef LOG
-		syslog(LOG_WARNING, "fdopen: %m");
-#else
-		warn("fdopen");
-#endif
+		complain(LOG_WARNING, "fdopen");
 		(void) close(s);
 		return;
 	}

Index: src/games/hunt/huntd/hunt.h
diff -u src/games/hunt/huntd/hunt.h:1.26 src/games/hunt/huntd/hunt.h:1.27
--- src/games/hunt/huntd/hunt.h:1.26	Sat Mar 29 21:55:59 2014
+++ src/games/hunt/huntd/hunt.h	Sat Mar 29 22:29:55 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: hunt.h,v 1.26 2014/03/29 21:55:59 dholland Exp $	*/
+/*	$NetBSD: hunt.h,v 1.27 2014/03/29 22:29:55 dholland Exp $	*/
 
 /*
  * Copyright (c) 1983-2003, Regents of the University of California.
@@ -34,10 +34,7 @@
 #include <stdbool.h>
 #include <stdio.h>
 #include <string.h>
-
-#ifdef LOG
 #include <syslog.h>
-#endif
 
 #include <sys/ioctl.h>
 #include <sys/types.h>
@@ -209,6 +206,9 @@ bool opposite(int, char);
 BULLET *is_bullet(int, int);
 void fixshots(int, int, char);
 
+/* in support.c */
+__printflike(2, 3) void complain(int level, const char *fmt, ...);
+
 /* in terminal.c */
 void cgoto(PLAYER *, int, int);
 void outch(PLAYER *, int);

Added files:

Index: src/games/hunt/huntd/support.c
diff -u /dev/null src/games/hunt/huntd/support.c:1.1
--- /dev/null	Sat Mar 29 22:29:55 2014
+++ src/games/hunt/huntd/support.c	Sat Mar 29 22:29:55 2014
@@ -0,0 +1,60 @@
+/*	$NetBSD: support.c,v 1.1 2014/03/29 22:29:55 dholland Exp $	*/
+/*-
+ * Copyright (c) 2014 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by David A. Holland.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+ */
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <syslog.h>
+#include <errno.h>
+#include <err.h>
+
+#include "hunt.h"
+
+__RCSID("$NetBSD: support.c,v 1.1 2014/03/29 22:29:55 dholland Exp $");
+
+void
+complain(int level, const char *fmt, ...)
+{
+	va_list ap;
+#ifdef LOG
+	char buf[4096];
+	int serrno = errno;
+#endif
+
+	va_start(ap, fmt);
+#ifdef LOG
+	vsnprintf(buf, sizeof(buf), fmt, ap);
+	snprintf(buf, sizeof(buf) - strlen(buf), ": %s", strerror(serrno));
+	syslog(level, "%s", buf);
+#else
+	(void)level;
+	vwarn("accept");
+#endif
+	va_end(ap);
+}

Reply via email to