1177bc5fc1c18e53503365240b39eeacfd9fc9bb
[dragonfly.git] / usr.bin / netstat / if.c
1 /*
2  * Copyright (c) 1983, 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)if.c     8.3 (Berkeley) 4/28/95
34  * $FreeBSD: src/usr.bin/netstat/if.c,v 1.32.2.9 2001/09/17 14:35:46 ru Exp $
35  * $DragonFly: src/usr.bin/netstat/if.c,v 1.6 2004/08/19 23:35:45 joerg Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/protosw.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
42 #include <sys/time.h>
43
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/if_dl.h>
47 #include <net/if_types.h>
48 #include <net/bridge/bridge.h>
49 #include <net/ethernet.h>
50 #include <netinet/in.h>
51 #include <netinet/in_var.h>
52 #include <netproto/ipx/ipx.h>
53 #include <netproto/ipx/ipx_if.h>
54 #ifdef NS
55 #include <netproto/ns/ns.h>
56 #include <netproto/ns/ns_if.h>
57 #endif
58 #ifdef ISO
59 #include <netiso/iso.h>
60 #include <netiso/iso_var.h>
61 #endif
62 #include <arpa/inet.h>
63
64 #include <signal.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <unistd.h>
69
70 #include "netstat.h"
71
72 #define YES     1
73 #define NO      0
74
75 static void sidewaysintpr (u_int, u_long);
76 static void catchalarm (int);
77
78 #ifdef INET6
79 char *netname6 (struct sockaddr_in6 *, struct in6_addr *);
80 static char ntop_buf[INET6_ADDRSTRLEN];         /* for inet_ntop() */
81 static int bdg_done;
82 #endif
83
84 /* print bridge statistics */
85 void
86 bdg_stats(u_long dummy __unused, char *name, int af __unused)
87 {
88     int i;
89     size_t slen ;
90     struct bdg_stats s ;
91     int mib[4] ;
92
93     slen = sizeof(s);
94
95     mib[0] = CTL_NET ;
96     mib[1] = PF_LINK ;
97     mib[2] = IFT_ETHER ;
98     mib[3] = PF_BDG ;
99     if (sysctl(mib,4, &s,&slen,NULL,0)==-1)
100         return ; /* no bridging */
101 #ifdef INET6
102     if (bdg_done != 0)
103         return;
104     else
105         bdg_done = 1;
106 #endif
107     printf("-- Bridging statistics (%s) --\n", name) ;
108     printf(
109 "Name          In      Out  Forward     Drop    Bcast    Mcast    Local  Unknown\n");
110     for (i = 0 ; i < 16 ; i++) {
111         if (s.s[i].name[0])
112         printf("%-6s %9ld%9ld%9ld%9ld%9ld%9ld%9ld%9ld\n",
113           s.s[i].name,
114           s.s[i].p_in[(int)BDG_IN],
115           s.s[i].p_in[(int)BDG_OUT],
116           s.s[i].p_in[(int)BDG_FORWARD],
117           s.s[i].p_in[(int)BDG_DROP],
118           s.s[i].p_in[(int)BDG_BCAST],
119           s.s[i].p_in[(int)BDG_MCAST],
120           s.s[i].p_in[(int)BDG_LOCAL],
121           s.s[i].p_in[(int)BDG_UNKNOWN] );
122     }
123 }
124
125
126
127
128 /*
129  * Display a formatted value, or a '-' in the same space.
130  */
131 static void
132 show_stat(const char *fmt, int width, u_long value, short showvalue)
133 {
134         char newfmt[32];
135
136         /* Construct the format string */
137         if (showvalue) {
138                 sprintf(newfmt, "%%%d%s", width, fmt);
139                 printf(newfmt, value);
140         } else {
141                 sprintf(newfmt, "%%%ds", width);
142                 printf(newfmt, "-");
143         }
144 }
145
146
147
148 /*
149  * Print a description of the network interfaces.
150  */
151 void
152 intpr(int interval, u_long ifnetaddr, void (*pfunc)(char *))
153 {
154         struct ifnet ifnet;
155         struct ifnethead ifnethead;
156         union {
157                 struct ifaddr ifa;
158                 struct in_ifaddr in;
159 #ifdef INET6
160                 struct in6_ifaddr in6;
161 #endif
162                 struct ipx_ifaddr ipx;
163 #ifdef NS
164                 struct ns_ifaddr ns;
165 #endif
166 #ifdef ISO
167                 struct iso_ifaddr iso;
168 #endif
169         } ifaddr;
170         u_long ifaddraddr;
171         u_long ifaddrfound;
172         u_long ifnetfound;
173         u_long opackets;
174         u_long ipackets;
175         u_long obytes;
176         u_long ibytes;
177         u_long oerrors;
178         u_long ierrors;
179         u_long collisions;
180         short timer;
181         int drops;
182         struct sockaddr *sa = NULL;
183         char name[IFNAMSIZ];
184         short network_layer;
185         short link_layer;
186
187         if (ifnetaddr == 0) {
188                 printf("ifnet: symbol not defined\n");
189                 return;
190         }
191         if (interval) {
192                 sidewaysintpr((unsigned)interval, ifnetaddr);
193                 return;
194         }
195         if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead))
196                 return;
197         ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
198         if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
199                 return;
200
201         if (!pfunc) {
202                 printf("%-7.7s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
203                        "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
204                 if (bflag)
205                         printf(" %10.10s","Ibytes");
206                 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
207                 if (bflag)
208                         printf(" %10.10s","Obytes");
209                 printf(" %5s", "Coll");
210                 if (tflag)
211                         printf(" %s", "Time");
212                 if (dflag)
213                         printf(" %s", "Drop");
214                 putchar('\n');
215         }
216         ifaddraddr = 0;
217         while (ifnetaddr || ifaddraddr) {
218                 struct sockaddr_in *sin;
219 #ifdef INET6
220                 struct sockaddr_in6 *sin6;
221 #endif
222                 register char *cp;
223                 int n, m;
224
225                 network_layer = 0;
226                 link_layer = 0;
227
228                 if (ifaddraddr == 0) {
229                         ifnetfound = ifnetaddr;
230                         if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
231                                 return;
232                         strlcpy(name, ifnet.if_xname, sizeof(name));
233                         ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link);
234                         if (interface != 0 && (strcmp(name, interface) != 0))
235                                 continue;
236                         cp = index(name, '\0');
237
238                         if (pfunc) {
239                                 (*pfunc)(name);
240                                 continue;
241                         }
242
243                         if ((ifnet.if_flags&IFF_UP) == 0)
244                                 *cp++ = '*';
245                         *cp = '\0';
246                         ifaddraddr = (u_long)TAILQ_FIRST(&ifnet.if_addrhead);
247                 }
248                 ifaddrfound = ifaddraddr;
249
250                 /*
251                  * Get the interface stats.  These may get
252                  * overriden below on a per-interface basis.
253                  */
254                 opackets = ifnet.if_opackets;
255                 ipackets = ifnet.if_ipackets;
256                 obytes = ifnet.if_obytes;
257                 ibytes = ifnet.if_ibytes;
258                 oerrors = ifnet.if_oerrors;
259                 ierrors = ifnet.if_ierrors;
260                 collisions = ifnet.if_collisions;
261                 timer = ifnet.if_timer;
262                 drops = ifnet.if_snd.ifq_drops;
263
264                 if (ifaddraddr == 0) {
265                         printf("%-7.7s %-5lu ", name, ifnet.if_mtu);
266                         printf("%-13.13s ", "none");
267                         printf("%-15.15s ", "none");
268                 } else {
269                         if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
270                                 ifaddraddr = 0;
271                                 continue;
272                         }
273 #define CP(x) ((char *)(x))
274                         cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
275                                 CP(&ifaddr);
276                         sa = (struct sockaddr *)cp;
277                         if (af != AF_UNSPEC && sa->sa_family != af) {
278                                 ifaddraddr =
279                                     (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
280                                 continue;
281                         }
282                         printf("%-7.7s %-5lu ", name, ifnet.if_mtu);
283                         switch (sa->sa_family) {
284                         case AF_UNSPEC:
285                                 printf("%-13.13s ", "none");
286                                 printf("%-15.15s ", "none");
287                                 break;
288                         case AF_INET:
289                                 sin = (struct sockaddr_in *)sa;
290 #ifdef notdef
291                                 /* can't use inet_makeaddr because kernel
292                                  * keeps nets unshifted.
293                                  */
294                                 in = inet_makeaddr(ifaddr.in.ia_subnet,
295                                         INADDR_ANY);
296                                 printf("%-13.13s ", netname(in.s_addr,
297                                     ifaddr.in.ia_subnetmask));
298 #else
299                                 printf("%-13.13s ",
300                                     netname(htonl(ifaddr.in.ia_subnet),
301                                     ifaddr.in.ia_subnetmask));
302 #endif
303                                 printf("%-15.15s ",
304                                     routename(sin->sin_addr.s_addr));
305
306                                 network_layer = 1;
307                                 break;
308 #ifdef INET6
309                         case AF_INET6:
310                                 sin6 = (struct sockaddr_in6 *)sa;
311                                 printf("%-11.11s ",
312                                        netname6(&ifaddr.in6.ia_addr,
313                                                 &ifaddr.in6.ia_prefixmask.sin6_addr));
314                                 printf("%-17.17s ",
315                                     (char *)inet_ntop(AF_INET6,
316                                         &sin6->sin6_addr,
317                                         ntop_buf, sizeof(ntop_buf)));
318
319                                 network_layer = 1;
320                                 break;
321 #endif /*INET6*/
322                         case AF_IPX:
323                                 {
324                                 struct sockaddr_ipx *sipx =
325                                         (struct sockaddr_ipx *)sa;
326                                 u_long net;
327                                 char netnum[10];
328
329                                 *(union ipx_net *) &net = sipx->sipx_addr.x_net;
330                                 sprintf(netnum, "%lx", (u_long)ntohl(net));
331                                 printf("ipx:%-8s  ", netnum);
332 /*                              printf("ipx:%-8s ", netname(net, 0L)); */
333                                 printf("%-15s ",
334                                     ipx_phost((struct sockaddr *)sipx));
335                                 }
336                                 break;
337
338                         case AF_APPLETALK:
339                                 printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
340                                 printf("%-9.9s  ",atalk_print(sa,0x0b) );
341                                 break;
342 #ifdef NS
343                         case AF_NS:
344                                 {
345                                 struct sockaddr_ns *sns =
346                                         (struct sockaddr_ns *)sa;
347                                 u_long net;
348                                 char netnum[10];
349
350                                 *(union ns_net *) &net = sns->sns_addr.x_net;
351                                 sprintf(netnum, "%lxH", ntohl(net));
352                                 upHex(netnum);
353                                 printf("ns:%-8s ", netnum);
354                                 printf("%-15s ",
355                                     ns_phost((struct sockaddr *)sns));
356                                 }
357                                 break;
358 #endif
359                         case AF_LINK:
360                                 {
361                                 struct sockaddr_dl *sdl =
362                                         (struct sockaddr_dl *)sa;
363                                 char linknum[10];
364                                 cp = (char *)LLADDR(sdl);
365                                 n = sdl->sdl_alen;
366                                 sprintf(linknum, "<Link#%d>", sdl->sdl_index);
367                                 m = printf("%-11.11s ", linknum);
368                                 }
369                                 goto hexprint;
370                         default:
371                                 m = printf("(%d)", sa->sa_family);
372                                 for (cp = sa->sa_len + (char *)sa;
373                                         --cp > sa->sa_data && (*cp == 0);) {}
374                                 n = cp - sa->sa_data + 1;
375                                 cp = sa->sa_data;
376                         hexprint:
377                                 while (--n >= 0)
378                                         m += printf("%02x%c", *cp++ & 0xff,
379                                                     n > 0 ? ':' : ' ');
380                                 m = 30 - m;
381                                 while (m-- > 0)
382                                         putchar(' ');
383
384                                 link_layer = 1;
385                                 break;
386                         }
387
388                         /*
389                          * Fixup the statistics for interfaces that
390                          * update stats for their network addresses
391                          */
392                         if (network_layer) {
393                                 opackets = ifaddr.in.ia_ifa.if_opackets;
394                                 ipackets = ifaddr.in.ia_ifa.if_ipackets;
395                                 obytes = ifaddr.in.ia_ifa.if_obytes;
396                                 ibytes = ifaddr.in.ia_ifa.if_ibytes;
397                         }
398
399                         ifaddraddr = (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
400                 }
401
402                 show_stat("lu", 8, ipackets, link_layer|network_layer);
403                 printf(" ");
404                 show_stat("lu", 5, ierrors, link_layer);
405                 printf(" ");
406                 if (bflag) {
407                         show_stat("lu", 10, ibytes, link_layer|network_layer);
408                         printf(" ");
409                 }
410                 show_stat("lu", 8, opackets, link_layer|network_layer);
411                 printf(" ");
412                 show_stat("lu", 5, oerrors, link_layer);
413                 printf(" ");
414                 if (bflag) {
415                         show_stat("lu", 10, obytes, link_layer|network_layer);
416                         printf(" ");
417                 }
418                 show_stat("lu", 5, collisions, link_layer);
419                 if (tflag) {
420                         printf(" ");
421                         show_stat("d", 3, timer, link_layer);
422                 }
423                 if (dflag) {
424                         printf(" ");
425                         show_stat("d", 3, drops, link_layer);
426                 }
427                 putchar('\n');
428                 if (aflag && ifaddrfound) {
429                         /*
430                          * Print family's multicast addresses
431                          */
432                         u_long multiaddr;
433                         struct ifmultiaddr ifma;
434                         union {
435                                 struct sockaddr sa;
436                                 struct sockaddr_in in;
437 #ifdef INET6
438                                 struct sockaddr_in6 in6;
439 #endif /* INET6 */
440                                 struct sockaddr_dl dl;
441                         } msa;
442                         const char *fmt;
443
444                         for(multiaddr = (u_long)ifnet.if_multiaddrs.lh_first;
445                             multiaddr;
446                             multiaddr = (u_long)ifma.ifma_link.le_next) {
447                                 if (kread(multiaddr, (char *)&ifma,
448                                           sizeof ifma))
449                                         break;
450                                 if (kread((u_long)ifma.ifma_addr, (char *)&msa,
451                                           sizeof msa))
452                                         break;
453                                 if (msa.sa.sa_family != sa->sa_family)
454                                         continue;
455                                 
456                                 fmt = 0;
457                                 switch (msa.sa.sa_family) {
458                                 case AF_INET:
459                                         fmt = routename(msa.in.sin_addr.s_addr);
460                                         break;
461 #ifdef INET6
462                                 case AF_INET6:
463                                         printf("%23s %-19.19s(refs: %d)\n", "",
464                                                inet_ntop(AF_INET6,
465                                                          &msa.in6.sin6_addr,
466                                                          ntop_buf,
467                                                          sizeof(ntop_buf)),
468                                                ifma.ifma_refcount);
469                                         break;
470 #endif /* INET6 */
471                                 case AF_LINK:
472                                         switch (msa.dl.sdl_type) {
473                                         case IFT_ETHER:
474                                         case IFT_FDDI:
475                                                 fmt = ether_ntoa(
476                                                         (struct ether_addr *)
477                                                         LLADDR(&msa.dl));
478                                                 break;
479                                         }
480                                         break;
481                                 }
482                                 if (fmt)
483                                         printf("%23s %s\n", "", fmt);
484                         }
485                 }
486         }
487 }
488
489 struct  iftot {
490         SLIST_ENTRY(iftot) chain;
491         char    ift_name[IFNAMSIZ];     /* interface name */
492         u_long  ift_ip;                 /* input packets */
493         u_long  ift_ie;                 /* input errors */
494         u_long  ift_op;                 /* output packets */
495         u_long  ift_oe;                 /* output errors */
496         u_long  ift_co;                 /* collisions */
497         u_int   ift_dr;                 /* drops */
498         u_long  ift_ib;                 /* input bytes */
499         u_long  ift_ob;                 /* output bytes */
500 };
501
502 u_char  signalled;                      /* set if alarm goes off "early" */
503
504 /*
505  * Print a running summary of interface statistics.
506  * Repeat display every interval seconds, showing statistics
507  * collected over that interval.  Assumes that interval is non-zero.
508  * First line printed at top of screen is always cumulative.
509  * XXX - should be rewritten to use ifmib(4).
510  */
511 static void
512 sidewaysintpr(unsigned interval, u_long off)
513 {
514         struct ifnet ifnet;
515         u_long firstifnet;
516         struct ifnethead ifnethead;
517         struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
518         register int line;
519         int oldmask, first;
520         u_long interesting_off;
521
522         if (kread(off, (char *)&ifnethead, sizeof ifnethead))
523                 return;
524         firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
525
526         if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
527                 printf("malloc failed\n");
528                 exit(1);
529         }
530         memset(iftot, 0, sizeof(struct iftot));
531
532         interesting = NULL;
533         interesting_off = 0;
534         for (off = firstifnet, ip = iftot; off;) {
535                 char name[IFNAMSIZ];
536
537                 if (kread(off, (char *)&ifnet, sizeof ifnet))
538                         break;
539                 strlcpy(name, ifnet.if_xname, sizeof(name));
540                 if (interface && strcmp(name, interface) == 0) {
541                         interesting = ip;
542                         interesting_off = off;
543                 }
544                 snprintf(ip->ift_name, 16, "(%s)", name);;
545                 if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
546                         printf("malloc failed\n");
547                         exit(1);
548                 }
549                 memset(ipn, 0, sizeof(struct iftot));
550                 SLIST_NEXT(ip, chain) = ipn;
551                 ip = ipn;
552                 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
553         }
554         if ((total = malloc(sizeof(struct iftot))) == NULL) {
555                 printf("malloc failed\n");
556                 exit(1);
557         }
558         memset(total, 0, sizeof(struct iftot));
559         if ((sum = malloc(sizeof(struct iftot))) == NULL) {
560                 printf("malloc failed\n");
561                 exit(1);
562         }
563         memset(sum, 0, sizeof(struct iftot));
564
565
566         (void)signal(SIGALRM, catchalarm);
567         signalled = NO;
568         (void)alarm(interval);
569         first = 1;
570 banner:
571         printf("%17s %14s %16s", "input",
572             interesting ? interesting->ift_name : "(Total)", "output");
573         putchar('\n');
574         printf("%10s %5s %10s %10s %5s %10s %5s",
575             "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
576         if (dflag)
577                 printf(" %5.5s", "drops");
578         putchar('\n');
579         fflush(stdout);
580         line = 0;
581 loop:
582         if (interesting != NULL) {
583                 ip = interesting;
584                 if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) {
585                         printf("???\n");
586                         exit(1);
587                 };
588                 if (!first) {
589                         printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
590                                 ifnet.if_ipackets - ip->ift_ip,
591                                 ifnet.if_ierrors - ip->ift_ie,
592                                 ifnet.if_ibytes - ip->ift_ib,
593                                 ifnet.if_opackets - ip->ift_op,
594                                 ifnet.if_oerrors - ip->ift_oe,
595                                 ifnet.if_obytes - ip->ift_ob,
596                                 ifnet.if_collisions - ip->ift_co);
597                         if (dflag)
598                                 printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr);
599                 }
600                 ip->ift_ip = ifnet.if_ipackets;
601                 ip->ift_ie = ifnet.if_ierrors;
602                 ip->ift_ib = ifnet.if_ibytes;
603                 ip->ift_op = ifnet.if_opackets;
604                 ip->ift_oe = ifnet.if_oerrors;
605                 ip->ift_ob = ifnet.if_obytes;
606                 ip->ift_co = ifnet.if_collisions;
607                 ip->ift_dr = ifnet.if_snd.ifq_drops;
608         } else {
609                 sum->ift_ip = 0;
610                 sum->ift_ie = 0;
611                 sum->ift_ib = 0;
612                 sum->ift_op = 0;
613                 sum->ift_oe = 0;
614                 sum->ift_ob = 0;
615                 sum->ift_co = 0;
616                 sum->ift_dr = 0;
617                 for (off = firstifnet, ip = iftot;
618                      off && SLIST_NEXT(ip, chain) != NULL;
619                      ip = SLIST_NEXT(ip, chain)) {
620                         if (kread(off, (char *)&ifnet, sizeof ifnet)) {
621                                 off = 0;
622                                 continue;
623                         }
624                         sum->ift_ip += ifnet.if_ipackets;
625                         sum->ift_ie += ifnet.if_ierrors;
626                         sum->ift_ib += ifnet.if_ibytes;
627                         sum->ift_op += ifnet.if_opackets;
628                         sum->ift_oe += ifnet.if_oerrors;
629                         sum->ift_ob += ifnet.if_obytes;
630                         sum->ift_co += ifnet.if_collisions;
631                         sum->ift_dr += ifnet.if_snd.ifq_drops;
632                         off = (u_long)TAILQ_NEXT(&ifnet, if_link);
633                 }
634                 if (!first) {
635                         printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
636                                 sum->ift_ip - total->ift_ip,
637                                 sum->ift_ie - total->ift_ie,
638                                 sum->ift_ib - total->ift_ib,
639                                 sum->ift_op - total->ift_op,
640                                 sum->ift_oe - total->ift_oe,
641                                 sum->ift_ob - total->ift_ob,
642                                 sum->ift_co - total->ift_co);
643                         if (dflag)
644                                 printf(" %5u", sum->ift_dr - total->ift_dr);
645                 }
646                 *total = *sum;
647         }
648         if (!first)
649                 putchar('\n');
650         fflush(stdout);
651         oldmask = sigblock(sigmask(SIGALRM));
652         if (! signalled) {
653                 sigpause(0);
654         }
655         sigsetmask(oldmask);
656         signalled = NO;
657         (void)alarm(interval);
658         line++;
659         first = 0;
660         if (line == 21)
661                 goto banner;
662         else
663                 goto loop;
664         /*NOTREACHED*/
665 }
666
667 /*
668  * Called if an interval expires before sidewaysintpr has completed a loop.
669  * Sets a flag to not wait for the alarm.
670  */
671 static void
672 catchalarm(int signo __unused)
673 {
674         signalled = YES;
675 }