The branch main has been updated by markj:

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

commit fbd3448614fbd7bd92e7d34c3bfd2592a07391e3
Author:     Mark Johnston <ma...@freebsd.org>
AuthorDate: 2024-12-04 01:12:39 +0000
Commit:     Mark Johnston <ma...@freebsd.org>
CommitDate: 2024-12-04 16:22:50 +0000

    sockstat: Ensure that there is always a space between columns
    
    PR:             282978
    Reviewed by:    asomers
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D47840
---
 usr.bin/sockstat/sockstat.c | 56 ++++++++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 23 deletions(-)

diff --git a/usr.bin/sockstat/sockstat.c b/usr.bin/sockstat/sockstat.c
index b18c56ca6f88..3347978c8b47 100644
--- a/usr.bin/sockstat/sockstat.c
+++ b/usr.bin/sockstat/sockstat.c
@@ -1129,14 +1129,12 @@ displaysock(struct sock *s, int pos)
                switch (s->family) {
                case AF_INET:
                case AF_INET6:
-                       if (laddr != NULL) {
+                       if (laddr != NULL)
                                pos += printaddr(&laddr->address);
-                               if (s->family == AF_INET6 && pos >= 58)
-                                       pos += xprintf(" ");
-                       }
                        offset += opt_w ? 46 : 22;
-                       while (pos < offset)
+                       do
                                pos += xprintf(" ");
+                       while (pos < offset);
                        if (faddr != NULL)
                                pos += printaddr(&faddr->address);
                        offset += opt_w ? 46 : 22;
@@ -1208,8 +1206,9 @@ displaysock(struct sock *s, int pos)
                        abort();
                }
                if (opt_f) {
-                       while (pos < offset)
+                       do
                                pos += xprintf(" ");
+                       while (pos < offset);
                        pos += xprintf("%d", s->fibnum);
                        offset += 7;
                }
@@ -1220,12 +1219,14 @@ displaysock(struct sock *s, int pos)
                                sp = RB_FIND(socks_t, &socks, &(struct sock)
                                    { .socket = s->splice_socket });
                                if (sp != NULL) {
-                                       while (pos < offset)
+                                       do
                                                pos += xprintf(" ");
+                                       while (pos < offset);
                                        pos += printaddr(&sp->laddr->address);
                                } else {
-                                       while (pos < offset)
+                                       do
                                                pos += xprintf(" ");
+                                       while (pos < offset);
                                        pos += xprintf("??");
                                        offset += opt_w ? 46 : 22;
                                }
@@ -1235,8 +1236,9 @@ displaysock(struct sock *s, int pos)
                if (opt_i) {
                        if (s->proto == IPPROTO_TCP ||
                            s->proto == IPPROTO_UDP) {
-                               while (pos < offset)
+                               do
                                        pos += xprintf(" ");
+                               while (pos < offset);
                                pos += xprintf("%" PRIu64, s->inp_gencnt);
                        }
                        offset += 9;
@@ -1250,8 +1252,9 @@ displaysock(struct sock *s, int pos)
                             (s->proto == IPPROTO_TCP &&
                              s->state != TCPS_CLOSED &&
                              s->state != TCPS_LISTEN))) {
-                               while (pos < offset)
+                               do
                                        pos += xprintf(" ");
+                               while (pos < offset);
                                pos += xprintf("%u",
                                    ntohs(faddr->encaps_port));
                        }
@@ -1263,8 +1266,9 @@ displaysock(struct sock *s, int pos)
                            s->state != SCTP_CLOSED &&
                            s->state != SCTP_BOUND &&
                            s->state != SCTP_LISTEN) {
-                               while (pos < offset)
+                               do
                                        pos += xprintf(" ");
+                               while (pos < offset);
                                pos += xprintf("%s",
                                    sctp_path_state(faddr->state));
                        }
@@ -1274,8 +1278,9 @@ displaysock(struct sock *s, int pos)
                        if (opt_s) {
                                if (s->proto == IPPROTO_SCTP ||
                                    s->proto == IPPROTO_TCP) {
-                                       while (pos < offset)
+                                       do
                                                pos += xprintf(" ");
+                                       while (pos < offset);
                                        switch (s->proto) {
                                        case IPPROTO_SCTP:
                                                pos += xprintf("%s",
@@ -1295,8 +1300,9 @@ displaysock(struct sock *s, int pos)
                        }
                        if (opt_S) {
                                if (s->proto == IPPROTO_TCP) {
-                                       while (pos < offset)
+                                       do
                                                pos += xprintf(" ");
+                                       while (pos < offset);
                                        pos += xprintf("%.*s",
                                            TCP_FUNCTION_NAME_LEN_MAX,
                                            s->stack);
@@ -1305,8 +1311,9 @@ displaysock(struct sock *s, int pos)
                        }
                        if (opt_C) {
                                if (s->proto == IPPROTO_TCP) {
-                                       while (pos < offset)
+                                       do
                                                pos += xprintf(" ");
+                                       while (pos < offset);
                                        xprintf("%.*s", TCP_CA_NAME_MAX, s->cc);
                                }
                                offset += TCP_CA_NAME_MAX + 1;
@@ -1371,18 +1378,21 @@ display(void)
                        pos = 0;
                        if (opt_n ||
                            (pwd = cap_getpwuid(cappwd, xf->xf_uid)) == NULL)
-                               pos += xprintf("%lu ", (u_long)xf->xf_uid);
+                               pos += xprintf("%lu", (u_long)xf->xf_uid);
                        else
-                               pos += xprintf("%s ", pwd->pw_name);
-                       while (pos < 9)
+                               pos += xprintf("%s", pwd->pw_name);
+                       do
                                pos += xprintf(" ");
+                       while (pos < 9);
                        pos += xprintf("%.10s", getprocname(xf->xf_pid));
-                       while (pos < 20)
+                       do
                                pos += xprintf(" ");
-                       pos += xprintf("%5lu ", (u_long)xf->xf_pid);
-                       while (pos < 26)
+                       while (pos < 20);
+                       pos += xprintf("%5lu", (u_long)xf->xf_pid);
+                       do
                                pos += xprintf(" ");
-                       pos += xprintf("%-3d ", xf->xf_fd);
+                       while (pos < 26);
+                       pos += xprintf("%-3d", xf->xf_fd);
                        displaysock(s, pos);
                }
        }
@@ -1391,7 +1401,7 @@ display(void)
        SLIST_FOREACH(s, &nosocks, socket_list) {
                if (!check_ports(s))
                        continue;
-               pos = xprintf("%-8s %-10s %-5s %-2s ",
+               pos = xprintf("%-8s %-10s %-5s %-3s",
                    "?", "?", "?", "?");
                displaysock(s, pos);
        }
@@ -1400,7 +1410,7 @@ display(void)
                        continue;
                if (!check_ports(s))
                        continue;
-               pos = xprintf("%-8s %-10s %-5s %-2s ",
+               pos = xprintf("%-8s %-10s %-5s %-3s",
                    "?", "?", "?", "?");
                displaysock(s, pos);
        }

Reply via email to