stderr.
-# $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
-/* $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.
#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>
{
if (errno == EINTR)
return false;
-#ifdef LOG
- syslog(LOG_ERR, "accept: %m");
-#else
- perror("accept");
-#endif
+ complain(LOG_ERR, "accept");
cleanup(1);
}
-/* $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.
#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>
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
if (errno == EADDRINUSE)
exit(0);
else {
-#ifdef LOG
- syslog(LOG_ERR, "bind: %m");
-#else
- warn("bind");
-#endif
+ complain(LOG_ERR, "bind");
cleanup(1);
}
}
#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);
#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);
}
}
#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);
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);
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;
}
-/* $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.
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
-
-#ifdef LOG
#include <syslog.h>
-#endif
#include <sys/ioctl.h>
#include <sys/types.h>
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);
--- /dev/null
+/* $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);
+}