git: c8d2ac71e8cf - stable/15 - ngctl: Modernize code somewhat

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Tue, 17 Feb 2026 23:57:58 UTC
The branch stable/15 has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=c8d2ac71e8cf2bc6a5ac8fdf212178a165e1a564

commit c8d2ac71e8cf2bc6a5ac8fdf212178a165e1a564
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-02-13 15:57:40 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-02-17 23:11:48 +0000

    ngctl: Modernize code somewhat
    
    * Replace fgets(3) with getline(3)
    
    * Replace select(2) with poll(2)
    
    * Avoid needlessly copying text around
    
    * Correct use of getopt(3)
    
    * Pick some style and whitespace nits
    
    MFC after:      1 week
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D55257
    
    (cherry picked from commit 3cbdcabf714d5187e22d8ff1cbfbc261dc8622eb)
---
 usr.sbin/ngctl/config.c   |   9 +-
 usr.sbin/ngctl/connect.c  |   6 +-
 usr.sbin/ngctl/debug.c    |   6 +-
 usr.sbin/ngctl/dot.c      |   7 +-
 usr.sbin/ngctl/list.c     |  14 ++-
 usr.sbin/ngctl/main.c     | 243 ++++++++++++++++++++++------------------------
 usr.sbin/ngctl/mkpeer.c   |   6 +-
 usr.sbin/ngctl/msg.c      |  39 +++-----
 usr.sbin/ngctl/name.c     |   6 +-
 usr.sbin/ngctl/rmhook.c   |   6 +-
 usr.sbin/ngctl/show.c     |   9 +-
 usr.sbin/ngctl/shutdown.c |   7 +-
 usr.sbin/ngctl/status.c   |  10 +-
 usr.sbin/ngctl/types.c    |   6 +-
 usr.sbin/ngctl/write.c    |  10 +-
 15 files changed, 168 insertions(+), 216 deletions(-)

diff --git a/usr.sbin/ngctl/config.c b/usr.sbin/ngctl/config.c
index 261bc950f570..25cd841494d1 100644
--- a/usr.sbin/ngctl/config.c
+++ b/usr.sbin/ngctl/config.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1996-1999 Whistle Communications, Inc.
  * All rights reserved.
- * 
+ *
  * Subject to the following obligations and disclaimer of warranty, use and
  * redistribution of this software, in source or object code forms, with or
  * without modifications are expressly permitted by Whistle Communications;
@@ -14,7 +14,7 @@
  *    Communications, Inc. trademarks, including the mark "WHISTLE
  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
  *    such appears in the above copyright notice or in the software.
- * 
+ *
  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
@@ -94,8 +94,8 @@ ConfigCmd(int ac, char **av)
 			return (CMDRTN_ERROR);
 		}
 	} else {
-		if (NgRecvMsg(csock, resp, sizeof(sbuf), NULL) < 0
-		    || (resp->header.flags & NGF_RESP) == 0)
+		if (NgRecvMsg(csock, resp, sizeof(sbuf), NULL) < 0 ||
+		    (resp->header.flags & NGF_RESP) == 0)
 			nostat = 1;
 	}
 
@@ -106,4 +106,3 @@ ConfigCmd(int ac, char **av)
 		printf("Config for \"%s\":\n%s\n", path, status);
 	return (CMDRTN_OK);
 }
-
diff --git a/usr.sbin/ngctl/connect.c b/usr.sbin/ngctl/connect.c
index 7f538e0a442d..c3e7d0b5a1b4 100644
--- a/usr.sbin/ngctl/connect.c
+++ b/usr.sbin/ngctl/connect.c
@@ -1,10 +1,9 @@
-
 /*
  * connect.c
  *
  * Copyright (c) 1996-1999 Whistle Communications, Inc.
  * All rights reserved.
- * 
+ *
  * Subject to the following obligations and disclaimer of warranty, use and
  * redistribution of this software, in source or object code forms, with or
  * without modifications are expressly permitted by Whistle Communications;
@@ -15,7 +14,7 @@
  *    Communications, Inc. trademarks, including the mark "WHISTLE
  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
  *    such appears in the above copyright notice or in the software.
- * 
+ *
  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
@@ -85,4 +84,3 @@ ConnectCmd(int ac, char **av)
 	}
 	return (CMDRTN_OK);
 }
-
diff --git a/usr.sbin/ngctl/debug.c b/usr.sbin/ngctl/debug.c
index bbc329acdcd2..1f64dfd466e9 100644
--- a/usr.sbin/ngctl/debug.c
+++ b/usr.sbin/ngctl/debug.c
@@ -1,10 +1,9 @@
-
 /*
  * debug.c
  *
  * Copyright (c) 1996-1999 Whistle Communications, Inc.
  * All rights reserved.
- * 
+ *
  * Subject to the following obligations and disclaimer of warranty, use and
  * redistribution of this software, in source or object code forms, with or
  * without modifications are expressly permitted by Whistle Communications;
@@ -15,7 +14,7 @@
  *    Communications, Inc. trademarks, including the mark "WHISTLE
  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
  *    such appears in the above copyright notice or in the software.
- * 
+ *
  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
@@ -79,4 +78,3 @@ DebugCmd(int ac, char **av)
 	}
 	return (CMDRTN_OK);
 }
-
diff --git a/usr.sbin/ngctl/dot.c b/usr.sbin/ngctl/dot.c
index 95132a5d0785..9893dc27f845 100644
--- a/usr.sbin/ngctl/dot.c
+++ b/usr.sbin/ngctl/dot.c
@@ -1,4 +1,3 @@
-
 /*
  * dot.c
  *
@@ -6,7 +5,7 @@
  * Copyright (c) 2004 Brian Fundakowski Feldman
  * Copyright (c) 1996-1999 Whistle Communications, Inc.
  * All rights reserved.
- * 
+ *
  * Subject to the following obligations and disclaimer of warranty, use and
  * redistribution of this software, in source or object code forms, with or
  * without modifications are expressly permitted by Whistle Communications;
@@ -17,7 +16,7 @@
  *    Communications, Inc. trademarks, including the mark "WHISTLE
  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
  *    such appears in the above copyright notice or in the software.
- * 
+ *
  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
@@ -71,13 +70,13 @@ DotCmd(int ac, char **av)
 	u_int i;
 
 	/* Get options */
+	optreset = 1;
 	optind = 1;
 	while ((ch = getopt(ac, av, "c")) != -1) {
 		switch (ch) {
 		case 'c':
 			compact = 1;
 			break;
-		case '?':
 		default:
 			return (CMDRTN_USAGE);
 			break;
diff --git a/usr.sbin/ngctl/list.c b/usr.sbin/ngctl/list.c
index d13de1c4c57f..77fc7a4bbcf5 100644
--- a/usr.sbin/ngctl/list.c
+++ b/usr.sbin/ngctl/list.c
@@ -1,10 +1,9 @@
-
 /*
  * list.c
  *
  * Copyright (c) 1996-1999 Whistle Communications, Inc.
  * All rights reserved.
- * 
+ *
  * Subject to the following obligations and disclaimer of warranty, use and
  * redistribution of this software, in source or object code forms, with or
  * without modifications are expressly permitted by Whistle Communications;
@@ -15,7 +14,7 @@
  *    Communications, Inc. trademarks, including the mark "WHISTLE
  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
  *    such appears in the above copyright notice or in the software.
- * 
+ *
  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
@@ -70,6 +69,7 @@ ListCmd(int ac, char **av)
 	int ch, rtn = CMDRTN_OK;
 
 	/* Get options */
+	optreset = 1;
 	optind = 1;
 	while ((ch = getopt(ac, av, "ln")) != -1) {
 		switch (ch) {
@@ -79,7 +79,6 @@ ListCmd(int ac, char **av)
 		case 'n':
 			named_only = 1;
 			break;
-		case '?':
 		default:
 			return (CMDRTN_USAGE);
 			break;
@@ -114,11 +113,11 @@ ListCmd(int ac, char **av)
 	ninfo = nlist->nodeinfo;
 	if (list_hooks) {
 		char	path[NG_PATHSIZ];
-		char	*argv[2] = { "show", path };
+		char	*argv[] = { "show", path, NULL };
 
 		while (nlist->numnames > 0) {
-			snprintf(path, sizeof(path),
-			    "[%lx]:", (u_long)ninfo->id);
+			snprintf(path, sizeof(path), "[%lx]:",
+			    (unsigned long)ninfo->id);
 			if ((rtn = (*show_cmd.func)(2, argv)) != CMDRTN_OK)
 				break;
 			ninfo++;
@@ -143,4 +142,3 @@ ListCmd(int ac, char **av)
 	free(resp);
 	return (rtn);
 }
-
diff --git a/usr.sbin/ngctl/main.c b/usr.sbin/ngctl/main.c
index b86f1ca27e71..b58de1e042b3 100644
--- a/usr.sbin/ngctl/main.c
+++ b/usr.sbin/ngctl/main.c
@@ -1,10 +1,9 @@
-
 /*
  * main.c
  *
  * Copyright (c) 1996-1999 Whistle Communications, Inc.
  * All rights reserved.
- * 
+ *
  * Subject to the following obligations and disclaimer of warranty, use and
  * redistribution of this software, in source or object code forms, with or
  * without modifications are expressly permitted by Whistle Communications;
@@ -15,7 +14,7 @@
  *    Communications, Inc. trademarks, including the mark "WHISTLE
  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
  *    such appears in the above copyright notice or in the software.
- * 
+ *
  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
@@ -39,12 +38,12 @@
 
 #include <sys/param.h>
 #include <sys/socket.h>
-#include <sys/select.h>
 
 #include <ctype.h>
 #include <err.h>
 #include <errno.h>
 #include <limits.h>
+#include <poll.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -71,7 +70,8 @@
 
 /* Internal functions */
 static int	ReadFile(FILE *fp);
-static void	ReadSockets(fd_set *);
+static void	ReadCtrlSocket(void);
+static void	ReadDataSocket(void);
 static int	DoParseCommand(const char *line);
 static int	DoCommand(int ac, char **av);
 static int	DoInteractive(void);
@@ -146,6 +146,7 @@ main(int ac, char *av[])
 	FILE		*fp = NULL;
 #ifdef JAIL
 	const char	*jail_name = NULL;
+	int		jid;
 #endif
 	int		ch, rtn = 0;
 
@@ -174,7 +175,6 @@ main(int ac, char *av[])
 		case 'n':
 			snprintf(name, sizeof(name), "%s", optarg);
 			break;
-		case '?':
 		default:
 			Usage((char *)NULL);
 			break;
@@ -185,8 +185,6 @@ main(int ac, char *av[])
 
 #ifdef JAIL
 	if (jail_name != NULL) {
-		int jid;
-
 		if (jail_name[0] == '\0')
 			Usage("invalid jail name");
 
@@ -239,18 +237,25 @@ main(int ac, char *av[])
 static int
 ReadFile(FILE *fp)
 {
-	char line[LINE_MAX];
-	int num, rtn;
-
-	for (num = 1; fgets(line, sizeof(line), fp) != NULL; num++) {
+	char *line = NULL;
+	ssize_t len;
+	size_t sz = 0;
+	unsigned int lineno = 0;
+	int rtn = CMDRTN_OK;
+
+	while ((len = getline(&line, &sz, fp)) > 0) {
+		lineno++;
 		if (*line == '#')
 			continue;
-		if ((rtn = DoParseCommand(line)) != 0) {
-			warnx("line %d: error in file", num);
-			return (rtn);
+		if ((rtn = DoParseCommand(line)) != CMDRTN_OK) {
+			warnx("line %d: error in file", lineno);
+			break;
 		}
 	}
-	return (CMDRTN_OK);
+	if (len < 0)
+		rtn = CMDRTN_ERROR;
+	free(line);
+	return (rtn);
 }
 
 #ifdef EDITLINE
@@ -258,7 +263,6 @@ ReadFile(FILE *fp)
 static void
 Unblock(int signal __unused)
 {
-
 	unblock = 1;
 }
 
@@ -269,8 +273,11 @@ Unblock(int signal __unused)
 static void *
 Monitor(void *v __unused)
 {
+	struct pollfd pfds[2] = {
+		{ .fd = csock, .events = POLLIN },
+		{ .fd = dsock, .events = POLLIN },
+	};
 	struct sigaction act;
-	const int maxfd = MAX(csock, dsock) + 1;
 
 	act.sa_handler = Unblock;
 	sigemptyset(&act.sa_mask);
@@ -279,22 +286,19 @@ Monitor(void *v __unused)
 
 	pthread_mutex_lock(&mutex);
 	for (;;) {
-		fd_set rfds;
-
-		/* See if any data or control messages are arriving. */
-		FD_ZERO(&rfds);
-		FD_SET(csock, &rfds);
-		FD_SET(dsock, &rfds);
 		unblock = 0;
-		if (select(maxfd, &rfds, NULL, NULL, NULL) <= 0) {
+		if (poll(pfds, 2, INFTIM) <= 0) {
 			if (errno == EINTR) {
 				if (unblock == 1)
 					pthread_cond_wait(&cond, &mutex);
 				continue;
 			}
-			err(EX_OSERR, "select");
+			err(EX_OSERR, "poll");
 		}
-		ReadSockets(&rfds);
+		if (pfds[0].revents != 0)
+			ReadCtrlSocket();
+		if (pfds[1].revents != 0)
+			ReadDataSocket();
 	}
 
 	return (NULL);
@@ -303,7 +307,6 @@ Monitor(void *v __unused)
 static char *
 Prompt(EditLine *el __unused)
 {
-
 	return (PROMPT);
 }
 
@@ -376,49 +379,48 @@ DoInteractive(void)
 static int
 DoInteractive(void)
 {
-	const int maxfd = MAX(csock, dsock) + 1;
+	struct pollfd pfds[3] = {
+		{ .fd = csock, .events = POLLIN },
+		{ .fd = dsock, .events = POLLIN },
+		{ .fd = STDIN_FILENO, .events = POLLIN },
+	};
+	char *line = NULL;
+	ssize_t len;
+	size_t sz = 0;
 
 	(*help_cmd.func)(0, NULL);
-	while (1) {
-		struct timeval tv;
-		fd_set rfds;
-
+	for (;;) {
 		/* See if any data or control messages are arriving */
-		FD_ZERO(&rfds);
-		FD_SET(csock, &rfds);
-		FD_SET(dsock, &rfds);
-		memset(&tv, 0, sizeof(tv));
-		if (select(maxfd, &rfds, NULL, NULL, &tv) <= 0) {
-
+		if (poll(pfds, 2, 0) <= 0) {
 			/* Issue prompt and wait for anything to happen */
 			printf("%s", PROMPT);
 			fflush(stdout);
-			FD_ZERO(&rfds);
-			FD_SET(0, &rfds);
-			FD_SET(csock, &rfds);
-			FD_SET(dsock, &rfds);
-			if (select(maxfd, &rfds, NULL, NULL, NULL) < 0)
-				err(EX_OSERR, "select");
-
-			/* If not user input, print a newline first */
-			if (!FD_ISSET(0, &rfds))
-				printf("\n");
+			if (poll(pfds, 3, INFTIM) < 0 && errno != EINTR)
+				err(EX_OSERR, "poll");
+		} else {
+			pfds[2].revents = 0;
 		}
 
-		ReadSockets(&rfds);
+		/* If not user input, print a newline first */
+		if (pfds[2].revents == 0)
+			printf("\n");
+
+		if (pfds[0].revents != 0)
+			ReadCtrlSocket();
+		if (pfds[1].revents != 0)
+			ReadDataSocket();
 
 		/* Get any user input */
-		if (FD_ISSET(0, &rfds)) {
-			char buf[LINE_MAX];
-
-			if (fgets(buf, sizeof(buf), stdin) == NULL) {
+		if (pfds[2].revents != 0) {
+			if ((len = getline(&line, &sz, stdin)) <= 0) {
 				printf("\n");
 				break;
 			}
-			if (DoParseCommand(buf) == CMDRTN_QUIT)
+			if (DoParseCommand(line) == CMDRTN_QUIT)
 				break;
 		}
 	}
+	free(line);
 	return (CMDRTN_QUIT);
 }
 #endif /* !EDITLINE */
@@ -427,29 +429,28 @@ DoInteractive(void)
  * Read and process data on netgraph control and data sockets.
  */
 static void
-ReadSockets(fd_set *rfds)
+ReadCtrlSocket(void)
 {
-	/* Display any incoming control message. */
-	if (FD_ISSET(csock, rfds))
-		MsgRead();
-
-	/* Display any incoming data packet. */
-	if (FD_ISSET(dsock, rfds)) {
-		char hook[NG_HOOKSIZ];
-		u_char *buf;
-		int rl;
-
-		/* Read packet from socket. */
-		if ((rl = NgAllocRecvData(dsock, &buf, hook)) < 0)
-			err(EX_OSERR, "reading hook \"%s\"", hook);
-		if (rl == 0)
-			errx(EX_OSERR, "EOF from hook \"%s\"?", hook);
-
-		/* Write packet to stdout. */
-		printf("Rec'd data packet on hook \"%s\":\n", hook);
-		DumpAscii(buf, rl);
-		free(buf);
-	}
+	MsgRead();
+}
+
+static void
+ReadDataSocket(void)
+{
+	char hook[NG_HOOKSIZ];
+	u_char *buf;
+	int rl;
+
+	/* Read packet from socket. */
+	if ((rl = NgAllocRecvData(dsock, &buf, hook)) < 0)
+		err(EX_OSERR, "reading hook \"%s\"", hook);
+	if (rl == 0)
+		errx(EX_OSERR, "EOF from hook \"%s\"?", hook);
+
+	/* Write packet to stdout. */
+	printf("Rec'd data packet on hook \"%s\":\n", hook);
+	DumpAscii(buf, rl);
+	free(buf);
 }
 
 /*
@@ -561,6 +562,8 @@ ReadCmd(int ac, char **av)
 
 	/* Process it */
 	rtn = ReadFile(fp);
+	if (ferror(fp))
+		warn("%s", av[1]);
 	fclose(fp);
 	return (rtn);
 }
@@ -572,7 +575,9 @@ static int
 HelpCmd(int ac, char **av)
 {
 	const struct ngcmd *cmd;
-	int k;
+	const char *s;
+	const int maxcol = 63;
+	int a, k, len;
 
 	switch (ac) {
 	case 0:
@@ -580,13 +585,11 @@ HelpCmd(int ac, char **av)
 		/* Show all commands */
 		printf("Available commands:\n");
 		for (k = 0; cmds[k] != NULL; k++) {
-			char *s, buf[100];
-
 			cmd = cmds[k];
-			snprintf(buf, sizeof(buf), "%s", cmd->cmd);
-			for (s = buf; *s != '\0' && !isspace(*s); s++);
-			*s = '\0';
-			printf("  %-10s %s\n", buf, cmd->desc);
+			for (s = cmd->cmd; *s != '\0' && !isspace(*s); s++)
+				/* nothing */;
+			printf("  %.*s%*s %s\n", (int)(s - cmd->cmd), cmd->cmd,
+			    (int)(10 - (s - cmd->cmd)), "", cmd->desc);
 		}
 		return (CMDRTN_OK);
 	default:
@@ -594,40 +597,29 @@ HelpCmd(int ac, char **av)
 		if ((cmd = FindCommand(av[1])) != NULL) {
 			printf("usage:    %s\n", cmd->cmd);
 			if (cmd->aliases[0] != NULL) {
-				int a = 0;
-
 				printf("Aliases:  ");
-				while (1) {
-					printf("%s", cmd->aliases[a++]);
-					if (a == MAX_CMD_ALIAS
-					    || cmd->aliases[a] == NULL) {
-						printf("\n");
-						break;
-					}
-					printf(", ");
+				for (a = 0; a < MAX_CMD_ALIAS &&
+				    cmd->aliases[a] != NULL; a++) {
+					if (a > 0)
+						printf(", ");
+					printf("%s", cmd->aliases[a]);
 				}
+				printf("\n");
 			}
 			printf("Summary:  %s\n", cmd->desc);
-			if (cmd->help != NULL) {
-				const char *s;
-				char buf[65];
-				int tot, len, done;
-
-				printf("Description:\n");
-				for (s = cmd->help; *s != '\0'; s += len) {
-					while (isspace(*s))
-						s++;
-					tot = snprintf(buf,
-					    sizeof(buf), "%s", s);
-					len = strlen(buf);
-					done = len == tot;
-					if (!done) {
-						while (len > 0
-						    && !isspace(buf[len-1]))
-							buf[--len] = '\0';
-					}
-					printf("  %s\n", buf);
-				}
+			if (cmd->help == NULL)
+				break;
+			printf("Description:\n");
+			for (s = cmd->help; *s != '\0'; s += len) {
+				while (isspace(*s))
+					s++;
+				/* advance to the column limit */
+				for (len = 0; s[len] && len < maxcol; len++)
+					/* nothing */;
+				/* back up to previous interword space */
+				while (len > 0 && s[len] && !isblank(s[len]))
+					len--;
+				printf("  %.*s\n", len, s);
 			}
 		}
 	}
@@ -649,34 +641,27 @@ QuitCmd(int ac __unused, char **av __unused)
 void
 DumpAscii(const u_char *buf, int len)
 {
-	char ch, sbuf[100];
 	int k, count;
 
 	for (count = 0; count < len; count += DUMP_BYTES_PER_LINE) {
-		snprintf(sbuf, sizeof(sbuf), "%04x:  ", count);
+		printf("%04x:  ", count);
 		for (k = 0; k < DUMP_BYTES_PER_LINE; k++) {
 			if (count + k < len) {
-				snprintf(sbuf + strlen(sbuf),
-				    sizeof(sbuf) - strlen(sbuf),
-				    "%02x ", buf[count + k]);
+				printf("%02x ", buf[count + k]);
 			} else {
-				snprintf(sbuf + strlen(sbuf),
-				    sizeof(sbuf) - strlen(sbuf), "   ");
+				printf("   ");
 			}
 		}
-		snprintf(sbuf + strlen(sbuf), sizeof(sbuf) - strlen(sbuf), " ");
+		printf(" ");
 		for (k = 0; k < DUMP_BYTES_PER_LINE; k++) {
 			if (count + k < len) {
-				ch = isprint(buf[count + k]) ?
-				    buf[count + k] : '.';
-				snprintf(sbuf + strlen(sbuf),
-				    sizeof(sbuf) - strlen(sbuf), "%c", ch);
+				printf("%c", isprint(buf[count + k]) ?
+				    buf[count + k] : '.');
 			} else {
-				snprintf(sbuf + strlen(sbuf),
-				    sizeof(sbuf) - strlen(sbuf), " ");
+				printf(" ");
 			}
 		}
-		printf("%s\n", sbuf);
+		printf("\n");
 	}
 }
 
diff --git a/usr.sbin/ngctl/mkpeer.c b/usr.sbin/ngctl/mkpeer.c
index b16260a7cce1..5884c898a165 100644
--- a/usr.sbin/ngctl/mkpeer.c
+++ b/usr.sbin/ngctl/mkpeer.c
@@ -1,10 +1,9 @@
-
 /*
  * mkpeer.c
  *
  * Copyright (c) 1996-1999 Whistle Communications, Inc.
  * All rights reserved.
- * 
+ *
  * Subject to the following obligations and disclaimer of warranty, use and
  * redistribution of this software, in source or object code forms, with or
  * without modifications are expressly permitted by Whistle Communications;
@@ -15,7 +14,7 @@
  *    Communications, Inc. trademarks, including the mark "WHISTLE
  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
  *    such appears in the above copyright notice or in the software.
- * 
+ *
  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
@@ -85,4 +84,3 @@ MkPeerCmd(int ac, char **av)
 	}
 	return (CMDRTN_OK);
 }
-
diff --git a/usr.sbin/ngctl/msg.c b/usr.sbin/ngctl/msg.c
index 2cc733408380..3be10293e82b 100644
--- a/usr.sbin/ngctl/msg.c
+++ b/usr.sbin/ngctl/msg.c
@@ -1,10 +1,9 @@
-
 /*
  * msg.c
  *
  * Copyright (c) 1999 Whistle Communications, Inc.
  * All rights reserved.
- * 
+ *
  * Subject to the following obligations and disclaimer of warranty, use and
  * redistribution of this software, in source or object code forms, with or
  * without modifications are expressly permitted by Whistle Communications;
@@ -15,7 +14,7 @@
  *    Communications, Inc. trademarks, including the mark "WHISTLE
  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
  *    such appears in the above copyright notice or in the software.
- * 
+ *
  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
@@ -37,9 +36,9 @@
  * $Whistle: msg.c,v 1.2 1999/11/29 23:38:35 archie Exp $
  */
 
-#include <sys/cdefs.h>
 #include <err.h>
 #include <netgraph.h>
+#include <poll.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -64,6 +63,9 @@ const struct ngcmd msg_cmd = {
 static int
 MsgCmd(int ac, char **av)
 {
+	struct pollfd pfds[1] = {
+		{ .fd = csock, .events = POLLIN },
+	};
 	char *buf;
 	char *path, *cmdstr;
 	int i, len;
@@ -95,22 +97,14 @@ MsgCmd(int ac, char **av)
 	free(buf);
 
 	/* See if a synchronous reply awaits */
-	{
-		struct timeval tv;
-		fd_set rfds;
-
-		FD_ZERO(&rfds);
-		FD_SET(csock, &rfds);
-		memset(&tv, 0, sizeof(tv));
-		switch (select(csock + 1, &rfds, NULL, NULL, &tv)) {
-		case -1:
-			err(EX_OSERR, "select");
-		case 0:
-			break;
-		default:
-			MsgRead();
-			break;
-		}
+	switch (poll(pfds, 1, 0)) {
+	case -1:
+		err(EX_OSERR, "poll");
+	case 0:
+		break;
+	default:
+		MsgRead();
+		break;
 	}
 
 	/* Done */
@@ -135,8 +129,8 @@ MsgRead(void)
 
 	/* Ask originating node to convert message to ASCII */
 	if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
-	      NGM_BINARY2ASCII, m, sizeof(*m) + m->header.arglen) < 0
-	    || NgAllocRecvMsg(csock, &m2, NULL) < 0) {
+	      NGM_BINARY2ASCII, m, sizeof(*m) + m->header.arglen) < 0 ||
+	    NgAllocRecvMsg(csock, &m2, NULL) < 0) {
 		printf("Rec'd %s %d from \"%s\":\n",
 		    (m->header.flags & NGF_RESP) != 0 ? "response" : "command",
 		    m->header.cmd, path);
@@ -160,4 +154,3 @@ MsgRead(void)
 		printf("No arguments\n");
 	free(m2);
 }
-
diff --git a/usr.sbin/ngctl/name.c b/usr.sbin/ngctl/name.c
index 79ddcad2f0c7..56c2040c3961 100644
--- a/usr.sbin/ngctl/name.c
+++ b/usr.sbin/ngctl/name.c
@@ -1,10 +1,9 @@
-
 /*
  * name.c
  *
  * Copyright (c) 1996-1999 Whistle Communications, Inc.
  * All rights reserved.
- * 
+ *
  * Subject to the following obligations and disclaimer of warranty, use and
  * redistribution of this software, in source or object code forms, with or
  * without modifications are expressly permitted by Whistle Communications;
@@ -15,7 +14,7 @@
  *    Communications, Inc. trademarks, including the mark "WHISTLE
  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
  *    such appears in the above copyright notice or in the software.
- * 
+ *
  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
@@ -76,4 +75,3 @@ NameCmd(int ac, char **av)
 	}
 	return (CMDRTN_OK);
 }
-
diff --git a/usr.sbin/ngctl/rmhook.c b/usr.sbin/ngctl/rmhook.c
index 5591d39cb078..d8fbbec18579 100644
--- a/usr.sbin/ngctl/rmhook.c
+++ b/usr.sbin/ngctl/rmhook.c
@@ -1,10 +1,9 @@
-
 /*
  * rmhook.c
  *
  * Copyright (c) 1996-1999 Whistle Communications, Inc.
  * All rights reserved.
- * 
+ *
  * Subject to the following obligations and disclaimer of warranty, use and
  * redistribution of this software, in source or object code forms, with or
  * without modifications are expressly permitted by Whistle Communications;
@@ -15,7 +14,7 @@
  *    Communications, Inc. trademarks, including the mark "WHISTLE
  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
  *    such appears in the above copyright notice or in the software.
- * 
+ *
  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
@@ -81,4 +80,3 @@ RmHookCmd(int ac, char **av)
 	}
 	return (CMDRTN_OK);
 }
-
diff --git a/usr.sbin/ngctl/show.c b/usr.sbin/ngctl/show.c
index 35d1aa2e5192..3536a5c39ae4 100644
--- a/usr.sbin/ngctl/show.c
+++ b/usr.sbin/ngctl/show.c
@@ -1,10 +1,9 @@
-
 /*
  * show.c
  *
  * Copyright (c) 1996-1999 Whistle Communications, Inc.
  * All rights reserved.
- * 
+ *
  * Subject to the following obligations and disclaimer of warranty, use and
  * redistribution of this software, in source or object code forms, with or
  * without modifications are expressly permitted by Whistle Communications;
@@ -15,7 +14,7 @@
  *    Communications, Inc. trademarks, including the mark "WHISTLE
  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
  *    such appears in the above copyright notice or in the software.
- * 
+ *
  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
@@ -67,13 +66,13 @@ ShowCmd(int ac, char **av)
 	int ch, no_hooks = 0;
 
 	/* Get options */
+	optreset = 1;
 	optind = 1;
 	while ((ch = getopt(ac, av, "n")) != -1) {
 		switch (ch) {
 		case 'n':
 			no_hooks = 1;
 			break;
-		case '?':
 		default:
 			return (CMDRTN_USAGE);
 			break;
@@ -133,5 +132,3 @@ ShowCmd(int ac, char **av)
 	free(resp);
 	return (CMDRTN_OK);
 }
-
-
diff --git a/usr.sbin/ngctl/shutdown.c b/usr.sbin/ngctl/shutdown.c
index fd38b9dc152b..9e221d1b0ba5 100644
--- a/usr.sbin/ngctl/shutdown.c
+++ b/usr.sbin/ngctl/shutdown.c
@@ -1,10 +1,9 @@
-
 /*
  * shutdown.c
  *
  * Copyright (c) 1996-1999 Whistle Communications, Inc.
  * All rights reserved.
- * 
+ *
  * Subject to the following obligations and disclaimer of warranty, use and
  * redistribution of this software, in source or object code forms, with or
  * without modifications are expressly permitted by Whistle Communications;
@@ -15,7 +14,7 @@
  *    Communications, Inc. trademarks, including the mark "WHISTLE
  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
  *    such appears in the above copyright notice or in the software.
- * 
+ *
  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
@@ -73,5 +72,3 @@ ShutdownCmd(int ac, char **av)
 	}
 	return (CMDRTN_OK);
 }
-
-
diff --git a/usr.sbin/ngctl/status.c b/usr.sbin/ngctl/status.c
index 10007255d551..3af258f00a7d 100644
--- a/usr.sbin/ngctl/status.c
+++ b/usr.sbin/ngctl/status.c
@@ -1,10 +1,9 @@
-
 /*
  * status.c
  *
  * Copyright (c) 1996-1999 Whistle Communications, Inc.
  * All rights reserved.
- * 
+ *
  * Subject to the following obligations and disclaimer of warranty, use and
  * redistribution of this software, in source or object code forms, with or
  * without modifications are expressly permitted by Whistle Communications;
@@ -15,7 +14,7 @@
  *    Communications, Inc. trademarks, including the mark "WHISTLE
  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
  *    such appears in the above copyright notice or in the software.
- * 
+ *
  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
@@ -84,8 +83,8 @@ StatusCmd(int ac, char **av)
 			return (CMDRTN_ERROR);
 		}
 	} else {
-		if (NgRecvMsg(csock, resp, sizeof(sbuf), NULL) < 0
-		    || (resp->header.flags & NGF_RESP) == 0)
+		if (NgRecvMsg(csock, resp, sizeof(sbuf), NULL) < 0 ||
+		    (resp->header.flags & NGF_RESP) == 0)
 			nostat = 1;
 	}
 
@@ -96,4 +95,3 @@ StatusCmd(int ac, char **av)
 		printf("Status for \"%s\":\n%s\n", path, status);
 	return (CMDRTN_OK);
 }
-
diff --git a/usr.sbin/ngctl/types.c b/usr.sbin/ngctl/types.c
index 555960a59c02..dbbb8c60c84d 100644
--- a/usr.sbin/ngctl/types.c
+++ b/usr.sbin/ngctl/types.c
@@ -1,10 +1,9 @@
-
*** 64 LINES SKIPPED ***