bin/81300: [PATCH] add option to sockstat(1) to always separate
Lolownia
creep at desk.pl
Fri May 20 11:10:03 GMT 2005
>Number: 81300
>Category: bin
>Synopsis: [PATCH] add option to sockstat(1) to always separate
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: freebsd-bugs
>State: open
>Quarter:
>Keywords:
>Date-Required:
>Class: change-request
>Submitter-Id: current-users
>Arrival-Date: Fri May 20 11:10:02 GMT 2005
>Closed-Date:
>Last-Modified:
>Originator: Lolownia
>Release: FreeBSD 5.2.1-RELEASE-p9
>Organization:
DESK.pl
>Environment:
FreeBSD niobe.desk.pl 5.2.1-RELEASE-p9 FreeBSD 5.2.1-RELEASE-p9 #0: Mon Aug 9 14:20:31 CEST 2004 creep at niobe.desk.pl:/home/hate/src/sys/i386/compile/SAMSARA i386
>Description:
If You use sockstat(1) and there are in its output users or program names longer then 8 and 10 chars respectively, the whitespace separating the fields disappears e.g.
spinacz100BitchX-1.029557 5 tcp4 *:63322 *:*
This makes sockstat(1) to be unreliable to use in scripts.
>How-To-Repeat:
Make a connection with credentials of user with name longer than 8 characters or with a program which name is longer then 10 characters.
>Fix:
Apply a patch:
=============8<=================8<=================
--- usr.bin/sockstat/sockstat.c.orig Fri May 20 12:58:28 2005
+++ usr.bin/sockstat/sockstat.c Fri May 20 12:59:00 2005
@@ -62,6 +62,7 @@
static int opt_4; /* Show IPv4 sockets */
static int opt_6; /* Show IPv6 sockets */
static int opt_c; /* Show connected sockets */
+static int opt_n; /* always display spaces */
static int opt_l; /* Show listening sockets */
static int opt_u; /* Show Unix domain sockets */
static int opt_v; /* Verbose mode */
@@ -487,6 +488,17 @@
return (0);
}
+
+static int
+padspace(int pos, int to)
+{
+ if (pos >= to && opt_n)
+ pos += xprintf(" ");
+ while (pos < to)
+ pos += xprintf(" ");
+ return (pos);
+}
+
static void
display(void)
{
@@ -516,30 +528,24 @@
pos += xprintf("%lu", (u_long)xf->xf_uid);
else
pos += xprintf("%s", pwd->pw_name);
- while (pos < 9)
- pos += xprintf(" ");
+ pos = padspace(pos,9);
pos += xprintf("%.10s", getprocname(xf->xf_pid));
- while (pos < 20)
- pos += xprintf(" ");
+ pos = padspace(pos,20);
pos += xprintf("%lu", (u_long)xf->xf_pid);
- while (pos < 26)
- pos += xprintf(" ");
+ pos = padspace(pos,26);
pos += xprintf("%d", xf->xf_fd);
- while (pos < 29)
- pos += xprintf(" ");
+ pos = padspace(pos,29);
pos += xprintf("%s", s->protoname);
if (s->vflag & INP_IPV4)
pos += xprintf("4");
if (s->vflag & INP_IPV6)
pos += xprintf("6");
- while (pos < 36)
- pos += xprintf(" ");
+ pos = padspace(pos,36);
switch (s->family) {
case AF_INET:
case AF_INET6:
pos += printaddr(s->family, &s->laddr);
- while (pos < 58)
- pos += xprintf(" ");
+ pos = padspace(pos,58);
pos += printaddr(s->family, &s->faddr);
break;
case AF_UNIX:
@@ -577,7 +583,7 @@
static void
usage(void)
{
- fprintf(stderr, "Usage: sockstat [-46clu] [-p ports]\n");
+ fprintf(stderr, "Usage: sockstat [-46cnlu] [-p ports]\n");
exit(1);
}
@@ -586,7 +592,7 @@
{
int o;
- while ((o = getopt(argc, argv, "46clp:uv")) != -1)
+ while ((o = getopt(argc, argv, "46cnlp:uv")) != -1)
switch (o) {
case '4':
opt_4 = 1;
@@ -596,6 +602,9 @@
break;
case 'c':
opt_c = 1;
+ break;
+ case 'n':
+ opt_n = 1;
break;
case 'l':
opt_l = 1;
===========8<====================8<======================
This adds option -n which makes sure there is a space separator in output.
spinacz100 BitchX-1.0 29557 4 tcp4 81.219.11.27:63318 212.182.63.110:6667
>Release-Note:
>Audit-Trail:
>Unformatted:
More information about the freebsd-bugs
mailing list