Annotation of 43BSDReno/usr.sbin/traceroute/median.awk, revision 1.1.1.1

1.1       root        1: #!/bin/awk -f
                      2: #
                      3: # Copyright (c) 1990 The Regents of the University of California.
                      4: # All rights reserved.
                      5: #
                      6: # This code is derived from software contributed to Berkeley by
                      7: # Van Jacobson.
                      8: #
                      9: # Redistribution and use in source and binary forms are permitted
                     10: # provided that the above copyright notice and this paragraph are
                     11: # duplicated in all such forms and that any documentation,
                     12: # advertising materials, and other materials related to such
                     13: # distribution and use acknowledge that the software was developed
                     14: # by the University of California, Berkeley.  The name of the
                     15: # University may not be used to endorse or promote products derived
                     16: # from this software without specific prior written permission.
                     17: # THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
                     18: # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
                     19: # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     20: #
                     21: #      @(#)median.awk  5.2 (Berkeley) 4/28/90
                     22: #
                     23: /^ *[0-9]/     {
                     24:        # print out the median time to each hop along a route.
                     25:        tottime = 0; n = 0;
                     26:        for (f = 5; f <= NF; ++f) {
                     27:                if ($f == "ms") {
                     28:                        ++n
                     29:                        time[n] = $(f - 1)
                     30:                }
                     31:        }
                     32:        if (n > 0) {
                     33:                # insertion sort the times to find the median
                     34:                for (i = 2; i <= n; ++i) {
                     35:                        v = time[i]; j = i - 1;
                     36:                        while (time[j] > v) {
                     37:                                time[j+1] = time[j];
                     38:                                j = j - 1;
                     39:                                if (j < 0)
                     40:                                        break;
                     41:                        }
                     42:                        time[j+1] = v;
                     43:                }
                     44:                if (n > 1 && (n % 2) == 0)
                     45:                        median = (time[n/2] + time[(n/2) + 1]) / 2
                     46:                else
                     47:                        median = time[(n+1)/2]
                     48: 
                     49:                print $1, median
                     50:        }
                     51: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.