On 13 May 2011 c. 06:54:04 Todd T. Fries wrote:
> Utilities which go into the install media should not be grown without
> cause, or at the very least, growth wrapped with #ifndef SMALL.

Oops, thanks.

> I think if you want some messages quieted, maybe you should look up
> some standard unix utilities.
>
> ping host 2>&1 | awk '/is down/{next}{print}'

Yes, I'm usually using the following:

 ping 1.2.3.4 2>&1 | fgrep 'bytes from'

But this way I do not see any errors. And your script will not work
because it will allow lines such as:

 ping: wrote 1.2.3.4 64 chars, ret=-1

to be printed too. ;)

Of course, mentioned functionality could be managed as shell script, but
I thought it may be handy to have such functionality in the base.

Anyway, here is an improved version, using your suggestion, for the case
anyone interested.

--
  Best wishes,
    Vadim Zhukov

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?


Index: ping.8
===================================================================
RCS file: /cvs/src/sbin/ping/ping.8,v
retrieving revision 1.45
diff -u -p -r1.45 ping.8
--- ping.8      3 Jul 2010 04:44:51 -0000       1.45
+++ ping.8      13 May 2011 07:45:09 -0000
@@ -39,7 +39,7 @@
 .Sh SYNOPSIS
 .Nm ping
 .Bk -words
-.Op Fl DdEefLnqRrv
+.Op Fl DdEefLnqRrvW
 .Op Fl c Ar count
 .Op Fl I Ar ifaddr
 .Op Fl i Ar wait
@@ -192,6 +192,13 @@ Verbose output.
 ICMP packets other than
 .Dv ECHO_REPLY
 that are received are listed.
+.It Fl W
+Do not print
+.Dq Host is down
+or
+.Dq Network is down
+error messages.
+Mnemonic: Wait until it come up.
 .It Fl w Ar maxwait
 Specifies the maximum number of seconds to wait for responses
 after the last request has been sent.
Index: ping.c
===================================================================
RCS file: /cvs/src/sbin/ping/ping.c,v
retrieving revision 1.88
diff -u -p -r1.88 ping.c
--- ping.c      3 Jul 2010 04:44:51 -0000       1.88
+++ ping.c      13 May 2011 07:45:09 -0000
@@ -108,6 +108,9 @@ int options;
 #define        F_SO_JUMBO      0x1000
 #define        F_AUD_RECV      0x2000
 #define        F_AUD_MISS      0x4000
+#ifndef SMALL
+#define        F_NODOWN        0x8000
+#endif

 /* multicast options */
 int moptions;
@@ -201,7 +204,12 @@ main(int argc, char *argv[])
        preload = 0;
        datap = &outpack[8 + sizeof(struct tvi)];
        while ((ch = getopt(argc, argv,
-           "DEI:LRS:c:defi:jl:np:qrs:T:t:V:vw:")) != -1)
+#ifndef SMALL
+           "c:DdEefI:i:jL:l:np:qRrS:s:T:t:V:vWw:"
+#else
+           "c:DdEefI:i:jL:l:np:qRrS:s:T:t:V:vw:"
+#endif
+           )) != -1)
                switch(ch) {
                case 'c':
                        npackets = (unsigned long)strtonum(optarg, 0,
@@ -319,6 +327,11 @@ main(int argc, char *argv[])
                case 'v':
                        options |= F_VERBOSE;
                        break;
+#ifndef SMALL
+               case 'W':
+                       options |= F_NODOWN;
+                       break;
+#endif
                case 'w':
                        maxwait = (unsigned int)strtonum(optarg, 1, INT_MAX,
                            &errstr);
@@ -653,11 +666,19 @@ pinger(void)
            sizeof(whereto));

        if (i < 0 || i != cc)  {
-               if (i < 0)
-                       perror("ping: sendto");
-               snprintf(buf, sizeof buf, "ping: wrote %s %d chars, ret=%d\n",
-                   hostname, cc, i);
-               write(STDOUT_FILENO, buf, strlen(buf));
+#ifndef SMALL
+               if (i >= 0 || (errno != ENETDOWN && errno != EHOSTDOWN) ||
+                   (options & F_NODOWN) != F_NODOWN) {
+#else
+               if (i >= 0) {
+#endif
+                       if (i < 0)
+                               perror("ping: sendto");
+                       snprintf(buf, sizeof buf,
+                           "ping: wrote %s %d chars, ret=%d\n",
+                           hostname, cc, i);
+                       write(STDOUT_FILENO, buf, strlen(buf));
+               }
        }
        if (!(options & F_QUIET) && options & F_FLOOD)
                (void)write(STDOUT_FILENO, &DOT, 1);
@@ -1363,7 +1384,12 @@ void
 usage(void)
 {
        (void)fprintf(stderr,
-           "usage: ping [-DdEefLnqRrv] [-c count] [-I ifaddr] [-i wait]\n"
+#ifndef SMALL
+           "usage: ping [-DdEefLnqRrWv]"
+#else
+           "usage: ping [-DdEefLnqRrv]"
+#endif
+                                      " [-c count] [-I ifaddr] [-i wait]\n"
            "\t[-l preload] [-p pattern] [-s packetsize] [-T tos] [-t ttl]\n"
            "\t[-V rtable] [-w maxwait] host\n");
        exit(1);

Reply via email to