This makes netstat show only listening sockets for tcp sockets
when invoked as netstat -l.

With it "netstat -l -finet -p tcp" is equivalent to
"netstat -a -finet | grep LISTEN"

I (re)used -l because Linux has the same option.

FreeBSD does not have -l, but it does not error out when invoked with -l.

ok?

diff --git usr.bin/netstat/inet.c usr.bin/netstat/inet.c
index 979750dad8e..3301af87475 100644
--- usr.bin/netstat/inet.c
+++ usr.bin/netstat/inet.c
@@ -294,9 +294,14 @@ netdomainpr(struct kinfo_file *kf, int proto)
        }
 
        /* filter listening sockets out unless -a is set */
-       if (!aflag && istcp && kf->t_state <= TCPS_LISTEN)
+       if (!(aflag||lflag) && istcp && kf->t_state <= TCPS_LISTEN)
                return;
-       else if (!aflag && isany)
+       else if (!(aflag||lflag) && isany)
+               return;
+
+       /* when -l is set, show only listening sockets */
+       if (!aflag && lflag && kf->so_type == SOCK_STREAM &&
+           kf->t_state != TCPS_LISTEN)
                return;
 
        if (af != kf->so_family || type != kf->so_type) {
@@ -305,6 +310,8 @@ netdomainpr(struct kinfo_file *kf, int proto)
                printf("Active Internet connections");
                if (aflag)
                        printf(" (including servers)");
+               else if (lflag)
+                       printf(" (only servers)");
                putchar('\n');
                if (Aflag) {
                        addrlen = 18;
diff --git usr.bin/netstat/main.c usr.bin/netstat/main.c
index e534b166634..b524089e72c 100644
--- usr.bin/netstat/main.c
+++ usr.bin/netstat/main.c
@@ -128,7 +128,7 @@ main(int argc, char *argv[])
        tableid = getrtable();
 
        while ((ch = getopt(argc, argv,
-           "AaBbc:dFf:ghI:ilM:mN:np:P:qrsT:tuvW:w:")) != -1)
+           "AaBbc:dFf:ghI:iLlM:mN:np:P:qrsT:tuvW:w:")) != -1)
                switch (ch) {
                case 'A':
                        Aflag = 1;
@@ -441,7 +441,7 @@ static void
 usage(void)
 {
        (void)fprintf(stderr,
-           "usage: %s [-AaBn] [-f address_family] [-M core] [-N system]\n"
+           "usage: %s [-AaBln] [-f address_family] [-M core] [-N system]\n"
            "       %s [-bdFgilmnqrstu] [-f address_family] [-M core] [-N 
system]\n"
            "               [-T tableid]\n"
            "       %s [-bdhn] [-c count] [-I interface] [-M core] [-N system] 
[-w wait]\n"
diff --git usr.bin/netstat/netstat.1 usr.bin/netstat/netstat.1
index 2347af1470e..7cec2905be1 100644
--- usr.bin/netstat/netstat.1
+++ usr.bin/netstat/netstat.1
@@ -38,7 +38,7 @@
 .Nd show network status
 .Sh SYNOPSIS
 .Nm netstat
-.Op Fl AaBn
+.Op Fl AaBln
 .Op Fl f Ar address_family
 .Op Fl p Ar protocol
 .Op Fl M Ar core
@@ -200,6 +200,8 @@ option) is present, show per-interface statistics on all 
interfaces
 for the specified
 .Ar address_family .
 .It Fl l
+With the default display,
+show only listening sockets.
 With the
 .Fl g
 option, display wider fields for the IPv6 multicast routing table
diff --git usr.bin/netstat/netstat.h usr.bin/netstat/netstat.h
index 7d117013f2f..75cc378289b 100644
--- usr.bin/netstat/netstat.h
+++ usr.bin/netstat/netstat.h
@@ -49,7 +49,8 @@ int   Fflag;          /* show routes whose gateways are in 
specified AF */
 int    gflag;          /* show group (multicast) routing or stats */
 int    hflag;          /* print human numbers */
 int    iflag;          /* show interfaces */
-int    lflag;          /* show routing table with use and ref */
+int    lflag;          /* show only listening sockets (only servers), */
+                       /* with -g, show routing table with use and ref */
 int    mflag;          /* show memory stats */
 int    nflag;          /* show addresses numerically */
 int    pflag;          /* show given protocol */

Reply via email to