diff options
| author | John Baldwin <jhb@FreeBSD.org> | 2026-06-23 15:51:43 +0000 |
|---|---|---|
| committer | John Baldwin <jhb@FreeBSD.org> | 2026-06-23 15:51:43 +0000 |
| commit | c6eb9f16cf0e19b54b382aa44f0c9a7bcec5fc40 (patch) | |
| tree | 9e7b82db4f4531636c7a2cfb6c5fcb30b561ce5d | |
| parent | b9f616d031566e94e6967a2c533b8aa7f36123b9 (diff) | |
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
| -rw-r--r-- | usr.sbin/pciconf/pciconf.c | 29 |
1 files changed, 13 insertions, 16 deletions
diff --git a/usr.sbin/pciconf/pciconf.c b/usr.sbin/pciconf/pciconf.c index 7da8aeadae93..e2a3beeba6f7 100644 --- a/usr.sbin/pciconf/pciconf.c +++ b/usr.sbin/pciconf/pciconf.c @@ -1290,20 +1290,13 @@ getsel(const char *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) { long rstart; long rend; long r; char *end; - int i; + int i, items_per_line; int fd; struct pcisel sel; @@ -1312,18 +1305,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); } @@ -1335,9 +1332,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) |
