Patch from Larry Doolittle (with minor touchups from me so everything compiles
[oweals/busybox.git] / ping.c
diff --git a/ping.c b/ping.c
index 6386c0e42d10262356d7d8193cb94c91096b7a06..a4363098e6e341fede7a4b8727667fe6944f1edd 100644 (file)
--- a/ping.c
+++ b/ping.c
@@ -1,6 +1,6 @@
 /* vi: set sw=4 ts=4: */
 /*
- * $Id: ping.c,v 1.17 2000/06/19 18:51:53 andersen Exp $
+ * $Id: ping.c,v 1.26 2000/11/14 23:29:24 andersen Exp $
  * Mini ping implementation for busybox
  *
  * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
@@ -31,7 +31,7 @@
  * Original copyright notice is retained at the end of this file.
  */
 
-#include "internal.h"
+#include "busybox.h"
 #include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/file.h>
@@ -51,7 +51,7 @@
 
 /* It turns out that libc5 doesn't have proper icmp support
  * built into it header files, so we have to supplement it */
-#if __GNU_LIBRARY__ < 5
+#if ! defined __GLIBC__ && ! defined __UCLIBC__
 typedef unsigned int socklen_t;
 
 #define        ICMP_MINLEN     8                               /* abs minimum */
@@ -173,13 +173,7 @@ static int in_cksum(unsigned short *buf, int sz)
 }
 
 /* simple version */
-#ifdef BB_SIMPLE_PING
-static const char *ping_usage = "ping host\n"
-#ifndef BB_FEATURE_TRIVIAL_HELP
-       "\nSend ICMP ECHO_REQUEST packets to network hosts\n"
-#endif
-       ;
-
+#ifdef BB_FEATURE_SIMPLE_PING
 static char *hostname = NULL;
 
 static void noresp(int ign)
@@ -208,7 +202,7 @@ static void ping(const char *host)
 
        pingaddr.sin_family = AF_INET;
        if (!(h = gethostbyname(host))) {
-               fprintf(stderr, "ping: unknown host %s\n", host);
+               errorMsg("unknown host %s\n", host);
                exit(1);
        }
        memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
@@ -225,7 +219,7 @@ static void ping(const char *host)
        if (c < 0 || c != sizeof(packet)) {
                if (c < 0)
                        perror("ping: sendto");
-               fprintf(stderr, "ping: write incomplete\n");
+               errorMsg("write incomplete\n");
                exit(1);
        }
 
@@ -265,19 +259,8 @@ extern int ping_main(int argc, char **argv)
        exit(TRUE);
 }
 
-#else /* ! BB_SIMPLE_PING */
+#else /* ! BB_FEATURE_SIMPLE_PING */
 /* full(er) version */
-static const char *ping_usage = "ping [OPTION]... host\n"
-#ifndef BB_FEATURE_TRIVIAL_HELP
-       "\nSend ICMP ECHO_REQUEST packets to network hosts.\n\n"
-       "Options:\n"
-       "\t-c COUNT\tSend only COUNT pings.\n"
-       "\t-s SIZE\t\tSend SIZE data bytes in packets (default=56).\n"
-       "\t-q\t\tQuiet mode, only displays output at start\n"
-       "\t\t\tand when finished.\n"
-#endif
-       ;
-
 static char *hostname = NULL;
 static struct sockaddr_in pingaddr;
 static int pingsock = -1;
@@ -294,8 +277,10 @@ static void unpack(char *, int, struct sockaddr_in *);
 
 /**************************************************************************/
 
-static void pingstats(int ign)
+static void pingstats(int junk)
 {
+       int status;
+
        signal(SIGINT, SIG_IGN);
 
        printf("\n--- %s ping statistics ---\n", hostname);
@@ -311,10 +296,14 @@ static void pingstats(int ign)
                           tmin / 10, tmin % 10,
                           (tsum / (nreceived + nrepeats)) / 10,
                           (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
-       exit(0);
+       if (nreceived != 0)
+               status = EXIT_SUCCESS;
+       else
+               status = EXIT_FAILURE;
+       exit(status);
 }
 
-static void sendping(int ign)
+static void sendping(int junk)
 {
        struct icmp *pkt;
        int i;
@@ -336,8 +325,8 @@ static void sendping(int ign)
                           (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
 
        if (i < 0)
-               fatalError("ping: sendto: %s\n", strerror(errno));
-       else if (i != sizeof(packet))
+               fatalError("sendto: %s\n", strerror(errno));
+       else if ((size_t)i != sizeof(packet))
                fatalError("ping wrote %d chars; %d expected\n", i,
                           (int)sizeof(packet));
 
@@ -433,8 +422,7 @@ static void unpack(char *buf, int sz, struct sockaddr_in *from)
                printf("\n");
        } else 
                if (icmppkt->icmp_type != ICMP_ECHO)
-                       fprintf(stderr,
-                                       "Warning: Got ICMP %d (%s)\n",
+                       errorMsg("Warning: Got ICMP %d (%s)\n",
                                        icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type));
 }
 
@@ -452,7 +440,7 @@ static void ping(const char *host)
        if ((pingsock = socket(AF_INET, SOCK_RAW,
                                                   (proto ? proto->p_proto : 1))) < 0) {        /* 1 == ICMP */
                if (errno == EPERM) {
-                       fprintf(stderr, "ping: permission denied. (are you root?)\n");
+                       errorMsg("permission denied. (are you root?)\n");
                } else {
                        perror("ping: creating a raw socket");
                }
@@ -466,13 +454,12 @@ static void ping(const char *host)
 
        pingaddr.sin_family = AF_INET;
        if (!(h = gethostbyname(host))) {
-               fprintf(stderr, "ping: unknown host %s\n", host);
+               errorMsg("unknown host %s\n", host);
                exit(1);
        }
 
        if (h->h_addrtype != AF_INET) {
-               fprintf(stderr,
-                               "ping: unknown address type; only AF_INET is currently supported.\n");
+               errorMsg("unknown address type; only AF_INET is currently supported.\n");
                exit(1);
        }
 
@@ -561,7 +548,7 @@ extern int ping_main(int argc, char **argv)
        ping(*argv);
        return(TRUE);
 }
-#endif /* ! BB_SIMPLE_PING */
+#endif /* ! BB_FEATURE_SIMPLE_PING */
 
 /*
  * Copyright (c) 1989 The Regents of the University of California.
@@ -578,10 +565,10 @@ extern int ping_main(int argc, char **argv)
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *     This product includes software developed by the University of
- *     California, Berkeley and its contributors.
+ *
+ * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change 
+ *             ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> 
+ *
  * 4. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.