git: 489af4764fa9 - stable/15 - pciconf: Minor cleanups in the config register methods

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Thu, 08 Oct 2026 20:43:45 UTC
The branch stable/15 has been updated by jhb:

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

commit 489af4764fa96fdca9e36f5ec873b77837a34482
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2026-06-23 15:51:43 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2026-10-08 20:42:31 +0000

    pciconf: Minor cleanups in the config register methods
    
    This is mostly to provide cleaner code for future changes to copy
    from.
    
    - Use NULL instead of casting 0 to pointer types.
    
    - Inline readone() in the sole caller now that it is just a single line.
    
    - Use a helper variable for the count of items on each line of output
      in readit().
    
    - Fix the double space in the middle of byte output to only trigger
      for width 1.  For other widths it would output spurious spaces at
      the end of the line which doesn't really hurt, but is buggy
      nonetheless.
    
    - Avoid using implicit booleans by explicitly comparing integer
      expressions against zero.
    
    - Don't compare the endptr returned from strtol() against NULL.
    
    Differential Revision:  https://reviews.freebsd.org/D57535
    
    (cherry picked from commit c6eb9f16cf0e19b54b382aa44f0c9a7bcec5fc40)
---
 usr.sbin/pciconf/pciconf.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)

diff --git a/usr.sbin/pciconf/pciconf.c b/usr.sbin/pciconf/pciconf.c
index b40c7dce4e40..acbd268c8be4 100644
--- a/usr.sbin/pciconf/pciconf.c
+++ b/usr.sbin/pciconf/pciconf.c
@@ -1290,13 +1290,6 @@ getsel(const char *str)
 		return (parsesel(str));
 }
 
-static void
-readone(int fd, struct pcisel *sel, long reg, int width)
-{
-
-	printf("%0*x", width*2, read_config(fd, sel, reg, width));
-}
-
 static void
 readit(const char *name, const char *reg, int width)
 {
@@ -1304,7 +1297,7 @@ readit(const char *name, const char *reg, int width)
 	long rend;
 	long r;
 	char *end;
-	int i;
+	int i, items_per_line;
 	int fd;
 	struct pcisel sel;
 
@@ -1313,18 +1306,22 @@ readit(const char *name, const char *reg, int width)
 		err(1, "%s", _PATH_DEVPCI);
 
 	rend = rstart = strtol(reg, &end, 0);
-	if (end && *end == ':') {
+	if (*end == ':') {
 		end++;
-		rend = strtol(end, (char **) 0, 0);
+		rend = strtol(end, NULL, 0);
 	}
 	sel = getsel(name);
+	items_per_line = 16 / width;
 	for (i = 1, r = rstart; r <= rend; i++, r += width) {
-		readone(fd, &sel, r, width);
-		if (i && !(i % 8))
+		printf("%0*x", width * 2, read_config(fd, &sel, r, width));
+
+		/* Use a double space in the middle when outputting bytes. */
+		if (width == 1 && i % 16 == 8)
 			putchar(' ');
-		putchar(i % (16/width) ? ' ' : '\n');
+
+		putchar(i % items_per_line == 0 ? '\n' : ' ');
 	}
-	if (i % (16/width) != 1)
+	if (i % items_per_line != 1)
 		putchar('\n');
 	close(fd);
 }
@@ -1336,9 +1333,9 @@ writeit(const char *name, const char *reg, const char *data, int width)
 	struct pci_io pi;
 
 	pi.pi_sel = getsel(name);
-	pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */
+	pi.pi_reg = strtoul(reg, NULL, 0); /* XXX error check */
 	pi.pi_width = width;
-	pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */
+	pi.pi_data = strtoul(data, NULL, 0); /* XXX error check */
 
 	fd = open(_PATH_DEVPCI, O_RDWR, 0);
 	if (fd < 0)