bcmp is obsolete
[unix-history] / usr / src / sbin / ping / ping.c
index f7fd9f5..e5a5aa4 100644 (file)
@@ -1,6 +1,6 @@
 /*
 /*
- * Copyright (c) 1989 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1989, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  * This code is derived from software contributed to Berkeley by
  * Mike Muuss.
  *
  * This code is derived from software contributed to Berkeley by
  * Mike Muuss.
@@ -9,13 +9,13 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
- All rights reserved.\n";
+static char copyright[] =
+"@(#) Copyright (c) 1989, 1993\n\
      The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)ping.c     5.8 (Berkeley) %G%";
+static char sccsid[] = "@(#)ping.c     8.3 (Berkeley) %G%";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -106,9 +106,9 @@ int interval = 1;           /* interval between packets */
 
 /* timing */
 int timing;                    /* flag to do timing */
 
 /* timing */
 int timing;                    /* flag to do timing */
-long tmin = LONG_MAX;          /* minimum round trip time */
-long tmax;                     /* maximum round trip time */
-u_long tsum;                   /* sum of all times, for doing average */
+double tmin = 999999999.0;     /* minimum round trip time */
+double tmax = 0.0;             /* maximum round trip time */
+double tsum = 0.0;             /* sum of all times, for doing average */
 
 char *pr_addr();
 void catcher(), finish();
 
 char *pr_addr();
 void catcher(), finish();
@@ -149,7 +149,7 @@ main(argc, argv)
                case 'f':
                        if (getuid()) {
                                (void)fprintf(stderr,
                case 'f':
                        if (getuid()) {
                                (void)fprintf(stderr,
-                                   "ping: you must be root to use the -f option.\n");
+                                   "ping: %s\n", strerror(EPERM));
                                exit(1);
                        }
                        options |= F_FLOOD;
                                exit(1);
                        }
                        options |= F_FLOOD;
@@ -214,7 +214,7 @@ main(argc, argv)
                usage();
        target = *argv;
 
                usage();
        target = *argv;
 
-       bzero((char *)&whereto, sizeof(struct sockaddr));
+       memset(&whereto, 0, sizeof(struct sockaddr));
        to = (struct sockaddr_in *)&whereto;
        to->sin_family = AF_INET;
        to->sin_addr.s_addr = inet_addr(target);
        to = (struct sockaddr_in *)&whereto;
        to->sin_family = AF_INET;
        to->sin_addr.s_addr = inet_addr(target);
@@ -228,7 +228,7 @@ main(argc, argv)
                        exit(1);
                }
                to->sin_family = hp->h_addrtype;
                        exit(1);
                }
                to->sin_family = hp->h_addrtype;
-               bcopy(hp->h_addr, (caddr_t)&to->sin_addr, hp->h_length);
+               memmove(&to->sin_addr, hp->h_addr, hp->h_length);
                (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1);
                hostname = hnamebuf;
        }
                (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1);
                hostname = hnamebuf;
        }
@@ -346,7 +346,7 @@ main(argc, argv)
  * catcher --
  *     This routine causes another PING to be transmitted, and then
  * schedules another SIGALRM for 1 second from now.
  * catcher --
  *     This routine causes another PING to be transmitted, and then
  * schedules another SIGALRM for 1 second from now.
- * 
+ *
  * bug --
  *     Our sense of time will slowly skew (i.e., packets will not be
  * launched exactly at 1-second intervals).  This does not affect the
  * bug --
  *     Our sense of time will slowly skew (i.e., packets will not be
  * launched exactly at 1-second intervals).  This does not affect the
@@ -375,7 +375,7 @@ catcher()
 
 /*
  * pinger --
 
 /*
  * pinger --
- *     Compose and transmit an ICMP ECHO REQUEST packet.  The IP packet
+ *     Compose and transmit an ICMP ECHO REQUEST packet.  The IP packet
  * will be added on by the kernel.  The ID field is our UNIX process ID,
  * and the sequence number is an ascending integer.  The first 8 bytes
  * of the data portion are used to hold a UNIX "timeval" struct in VAX
  * will be added on by the kernel.  The ID field is our UNIX process ID,
  * and the sequence number is an ascending integer.  The first 8 bytes
  * of the data portion are used to hold a UNIX "timeval" struct in VAX
@@ -414,7 +414,7 @@ pinger()
                (void)printf("ping: wrote %s %d chars, ret=%d\n",
                    hostname, cc, i);
        }
                (void)printf("ping: wrote %s %d chars, ret=%d\n",
                    hostname, cc, i);
        }
-       if (options & F_FLOOD)
+       if (!(options & F_QUIET) && options & F_FLOOD)
                (void)write(STDOUT_FILENO, &DOT, 1);
 }
 
                (void)write(STDOUT_FILENO, &DOT, 1);
 }
 
@@ -438,7 +438,7 @@ pr_pack(buf, cc, from)
        static char old_rr[MAX_IPOPTLEN];
        struct ip *ip;
        struct timeval tv, *tp;
        static char old_rr[MAX_IPOPTLEN];
        struct ip *ip;
        struct timeval tv, *tp;
-       long triptime;
+       double triptime;
        int hlen, dupflag;
 
        (void)gettimeofday(&tv, (struct timezone *)NULL);
        int hlen, dupflag;
 
        (void)gettimeofday(&tv, (struct timezone *)NULL);
@@ -468,7 +468,8 @@ pr_pack(buf, cc, from)
                        tp = (struct timeval *)icp->icmp_data;
 #endif
                        tvsub(&tv, tp);
                        tp = (struct timeval *)icp->icmp_data;
 #endif
                        tvsub(&tv, tp);
-                       triptime = tv.tv_sec * 1000 + (tv.tv_usec / 1000);
+                       triptime = ((double)tv.tv_sec) * 1000.0 +
+                           ((double)tv.tv_usec) / 1000.0;
                        tsum += triptime;
                        if (triptime < tmin)
                                tmin = triptime;
                        tsum += triptime;
                        if (triptime < tmin)
                                tmin = triptime;
@@ -496,7 +497,7 @@ pr_pack(buf, cc, from)
                           icp->icmp_seq);
                        (void)printf(" ttl=%d", ip->ip_ttl);
                        if (timing)
                           icp->icmp_seq);
                        (void)printf(" ttl=%d", ip->ip_ttl);
                        if (timing)
-                               (void)printf(" time=%ld ms", triptime);
+                               (void)printf(" time=%g ms", triptime);
                        if (dupflag)
                                (void)printf(" (DUP!)");
                        /* check the data */
                        if (dupflag)
                                (void)printf(" (DUP!)");
                        /* check the data */
@@ -566,7 +567,7 @@ pr_pack(buf, cc, from)
                                continue;
                        if (i == old_rrlen
                            && cp == (u_char *)buf + sizeof(struct ip) + 2
                                continue;
                        if (i == old_rrlen
                            && cp == (u_char *)buf + sizeof(struct ip) + 2
-                           && !bcmp((char *)cp, old_rr, i)
+                           && !memcmp(cp, old_rr, i)
                            && !(options & F_FLOOD)) {
                                (void)printf("\t(same route)");
                                i = ((i + 3) / 4) * 4;
                            && !(options & F_FLOOD)) {
                                (void)printf("\t(same route)");
                                i = ((i + 3) / 4) * 4;
@@ -575,7 +576,7 @@ pr_pack(buf, cc, from)
                                break;
                        }
                        old_rrlen = i;
                                break;
                        }
                        old_rrlen = i;
-                       bcopy((char *)cp, old_rr, i);
+                       memmove(old_rr, cp, i);
                        (void)printf("\nRR: ");
                        for (;;) {
                                l = *++cp;
                        (void)printf("\nRR: ");
                        for (;;) {
                                l = *++cp;
@@ -664,6 +665,8 @@ tvsub(out, in)
 void
 finish()
 {
 void
 finish()
 {
+       register int i;
+
        (void)signal(SIGINT, SIG_IGN);
        (void)putchar('\n');
        (void)fflush(stdout);
        (void)signal(SIGINT, SIG_IGN);
        (void)putchar('\n');
        (void)fflush(stdout);
@@ -680,9 +683,12 @@ finish()
                            (int) (((ntransmitted - nreceived) * 100) /
                            ntransmitted));
        (void)putchar('\n');
                            (int) (((ntransmitted - nreceived) * 100) /
                            ntransmitted));
        (void)putchar('\n');
-       if (nreceived && timing)
-               (void)printf("round-trip min/avg/max = %ld/%lu/%ld ms\n",
-                   tmin, tsum / (nreceived + nrepeats), tmax);
+       if (nreceived && timing) {
+               /* Only display average to microseconds */
+               i = 1000.0 * tsum / (nreceived + nrepeats);
+               (void)printf("round-trip min/avg/max = %g/%g/%g ms\n",
+                   tmin, ((double)i) / 1000.0, tmax);
+       }
        exit(0);
 }
 
        exit(0);
 }
 
@@ -933,7 +939,9 @@ fill(bp, patp)
            &pat[13], &pat[14], &pat[15]);
 
        if (ii > 0)
            &pat[13], &pat[14], &pat[15]);
 
        if (ii > 0)
-               for (kk = 0; kk <= MAXPACKET - (8 + ii); kk += ii)
+               for (kk = 0;
+                   kk <= MAXPACKET - (8 + sizeof(struct timeval) + ii);
+                   kk += ii)
                        for (jj = 0; jj < ii; ++jj)
                                bp[jj + kk] = pat[jj];
        if (!(options & F_QUIET)) {
                        for (jj = 0; jj < ii; ++jj)
                                bp[jj + kk] = pat[jj];
        if (!(options & F_QUIET)) {