home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / ethertop / sort.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-12  |  3.3 KB  |  107 lines

  1. /*
  2.  * Copyright (c) 1991 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Irvine.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17. #include <stdio.h>
  18. #include "ethertop.h"
  19. #include "buffer.h"
  20.  
  21. #define subtract(x,y) ((x)>(y) ? (x)-(y) : (y)-(x))
  22.  
  23. void subtract_etherstat(new,old,diff)
  24. struct etherstat *new, *old;
  25. etherfloatstat *diff;
  26. {
  27.   register unsigned int x;
  28.   float difftime;
  29.   difftime=(float) subtract(new->e_time.tv_seconds,old->e_time.tv_seconds)
  30.     + (subtract((float) new->e_time.tv_useconds , (float) old->e_time.tv_useconds)) / 1000000.00;
  31.   diff->e_time=difftime;
  32.   diff->e_bytes  =subtract(new->e_bytes,old->e_bytes)/difftime;
  33.   diff->e_packets=subtract(new->e_packets,old->e_packets)/difftime;
  34.   diff->e_bcast  =subtract(new->e_bcast,old->e_bcast)/difftime;
  35.   for (x=0; x<NPROTOS; x++)
  36.     {
  37.       diff->e_proto[x]=subtract(new->e_proto[x],old->e_proto[x])/difftime;
  38.     }
  39.   for (x=0; x<NBUCKETS; x++)
  40.     {
  41.       diff->e_size[x]=subtract(new->e_size[x],old->e_size[x])/difftime;
  42.     }
  43. }
  44.  
  45.  
  46.  
  47.  
  48. int size_compare(a,b)
  49. etherfloat_node **a,**b;
  50. {
  51.   if ((*b)->h_cnt < (*a)->h_cnt)
  52.     return -1;
  53.   if((*b)->h_cnt > (*a)->h_cnt)
  54.     return 1;
  55.   return 0;
  56. }
  57.  
  58.  
  59. void subtract_and_sort_etheraddrs(new, old, buf)
  60.      struct etheraddrs *new,*old;
  61.      buffer *buf;
  62. {
  63.   register unsigned int i;
  64.   register float difftime;
  65.   etherhmem_node *newptr, *oldptr;
  66.   etherfloat_node *diffptr;
  67.   difftime=(float) subtract(new->e_time.tv_seconds,old->e_time.tv_seconds)
  68.     + (subtract((float) new->e_time.tv_useconds , (float) old->e_time.tv_useconds)) / 1000000.00;
  69.  
  70.   for (i=0; i<HASHSIZE; i++)
  71.     {
  72.       newptr=new->e_addrs[i];
  73.       oldptr=old->e_addrs[i];
  74.       while(newptr!=NULL) /* not a valid record */
  75.     {
  76.       while (oldptr!=NULL &&  /* old record is there */
  77.          oldptr->h_addr!=newptr->h_addr) /* but record has been inserted */
  78.         {
  79.           oldptr=oldptr->h_nxt; /* jump to next old address until you reach end */
  80.                                 /* or addresses match */
  81.         }
  82.       if (oldptr==NULL)  /* old record does not exist */
  83.         {
  84.           diffptr=(etherfloat_node *)malloc(sizeof(etherfloat_node));
  85.           diffptr->h_addr=newptr->h_addr;
  86.           diffptr->h_cnt=(float)(newptr->h_cnt) / difftime;
  87.           add_buffer(buf,diffptr);
  88.         }
  89.       else /* old record existed */
  90.         { 
  91.           if (newptr->h_cnt!=oldptr->h_cnt) /* count different */
  92.         {
  93.           diffptr=(etherfloat_node *)malloc(sizeof(etherfloat_node));
  94.           diffptr->h_addr=newptr->h_addr;
  95.           diffptr->h_cnt=(float) (subtract(newptr->h_cnt,oldptr->h_cnt)/difftime);
  96.           add_buffer(buf,diffptr);
  97.         }
  98.           oldptr=oldptr->h_nxt;
  99.         }
  100.       newptr=newptr->h_nxt;
  101.     }
  102.     }
  103.   qsort( (char *) buf->p, buf->end, sizeof(void *), size_compare);
  104. }
  105.  
  106.  
  107.