NetBSD-5.0.2/regress/lib/libc/servent/compare

#!/bin/sh
# compare - compare output
# usage: compare original-servent-file reader-program
#
# $NetBSD: compare,v 1.4 2008/03/09 01:31:07 dholland Exp $
#

REF=reference-output
OBS=observed-output
DIFFS=differences

if [ $# != 2 ]; then
    echo "$0: usage: $0 original-servent-file reader-program" 1>&2
    exit 1
fi
if [ ! -f "$1" ]; then
    echo "$0: $1 missing" 1>&2
    exit 1
fi
if [ ! -x "$2" ]; then
    echo "$0: $2 missing" 1>&2
    exit 1
fi

#
# Munge original to:
#  (1) match output format of the test program
#  (2) fold all names for the same port/proto together
#  (3) prune duplicates
#
tr '\t' ' ' < "$1" | awk '
    function add(key, name,      i, n, ar) {
	n = split(names[key], ar);
	for (i=1; i<=n; i++) {
	    if (name == ar[i]) {
		return;
	    }
	}
	delete ar;
	names[key] = names[key] " " name;
    }

    {
	sub("#.*", "", $0);
	gsub("  *", " ", $0);
	if (NF==0) {
	    next;
	}
	add($2, $1, 0);
	for (i=3; i<=NF; i++) {
	    add($2, $i, 1);
	}
    }
    END {
	for (key in names) {
	    portproto = key;
	    sub("/", ", proto=", portproto);
	    portproto = "port=" portproto;

	    n = split(names[key], ar);
	    printf "name=%s, %s, aliases=", ar[1], portproto;
	    for (i=2; i<=n; i++) {
		if (i>2) {
		    printf " ";
		}
		printf "%s", ar[i];
	    }
	    printf "\n";
	    delete ar;
	}
    }
' | sort > $REF

# run test program
$2 | sed 's/ *$//' | sort > $OBS

diff $REF $OBS >$DIFFS 2>&1

if [ -s $DIFFS ]; then
	echo "servent: Observed output does not match reference output" 1>&2
	echo "servent: Outputs left in `pwd`" 1>&2
	exit 1
fi
rm -f $REF $OBS $DIFFS
exit 0