Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/games/hunt/huntd Reduce ifdefs by making a common function t...
details: https://anonhg.NetBSD.org/src/rev/a15e1a0e4d39
branches: trunk
changeset: 328257:a15e1a0e4d39
user: dholland <dholland%NetBSD.org@localhost>
date: Sat Mar 29 22:29:55 2014 +0000
description:
Reduce ifdefs by making a common function to talk to either syslogd or
stderr.
diffstat:
games/hunt/huntd/Makefile | 4 +-
games/hunt/huntd/answer.c | 10 ++-----
games/hunt/huntd/driver.c | 58 ++++++++------------------------------------
games/hunt/huntd/hunt.h | 8 +++---
games/hunt/huntd/support.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 80 insertions(+), 60 deletions(-)
diffs (269 lines):
diff -r ac66b5ad0bc8 -r a15e1a0e4d39 games/hunt/huntd/Makefile
--- a/games/hunt/huntd/Makefile Sat Mar 29 22:15:26 2014 +0000
+++ b/games/hunt/huntd/Makefile Sat Mar 29 22:29:55 2014 +0000
@@ -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
diff -r ac66b5ad0bc8 -r a15e1a0e4d39 games/hunt/huntd/answer.c
--- a/games/hunt/huntd/answer.c Sat Mar 29 22:15:26 2014 +0000
+++ b/games/hunt/huntd/answer.c Sat Mar 29 22:29:55 2014 +0000
@@ -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 @@
{
if (errno == EINTR)
return false;
-#ifdef LOG
- syslog(LOG_ERR, "accept: %m");
-#else
- perror("accept");
-#endif
+ complain(LOG_ERR, "accept");
cleanup(1);
}
diff -r ac66b5ad0bc8 -r a15e1a0e4d39 games/hunt/huntd/driver.c
--- a/games/hunt/huntd/driver.c Sat Mar 29 22:15:26 2014 +0000
+++ b/games/hunt/huntd/driver.c Sat Mar 29 22:29:55 2014 +0000
@@ -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 @@
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 @@
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 @@
#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 @@
#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 @@
#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 @@
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 @@
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;
}
diff -r ac66b5ad0bc8 -r a15e1a0e4d39 games/hunt/huntd/hunt.h
--- a/games/hunt/huntd/hunt.h Sat Mar 29 22:15:26 2014 +0000
+++ b/games/hunt/huntd/hunt.h Sat Mar 29 22:29:55 2014 +0000
@@ -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 @@
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);
diff -r ac66b5ad0bc8 -r a15e1a0e4d39 games/hunt/huntd/support.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/games/hunt/huntd/support.c Sat Mar 29 22:29:55 2014 +0000
@@ -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);
+}
Home |
Main Index |
Thread Index |
Old Index