#
# /etc/protocols processing
#
# Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>

$have_protocols = 0;

sub read_protocols {
	local($_, $prog, $number, $rest);

	return if ($have_protocols);
	open(PROTOCOLS, "<$source{protocols}") ||
		&fatal("can't open $source{protocols}: $!");
	while (<PROTOCOLS>) {
		chop;
		s/#.*//o;
		s/^\s+//o;
		s/\s+$//o;
		next if (/^$/);
		($name, $prot, $rest) = /(\S+)\s+(\S+)\s*(.*)/o;
		$name2proto{$name} = $_;
		$proto2name{$prot} = $_;
		foreach $name (split(/\s+/, $rest)) {
			$name2proto{$name} = $_;
		}
	}
	close PROTOCOLS;
	$have_protocols = 1;
}

sub protocols_byname {

	local($name);

	&read_protocols;

	foreach $name (keys %name2proto) {
		printf OUT "$name\t$name2proto{$name}\n";
	}
}

sub protocols_bynumber {

	local($prot);

	&read_protocols;

	foreach $prot (keys %proto2name) {
		printf OUT "$prot\t$proto2name{$prot}\n";
	}
}

1;
