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

$have_hosts = 0;

sub hosts_read {
	local ($_, $host, $addr, $rest);

	return if ($have_hosts);

	open(HOSTS, "<$source{hosts}") ||
		&fatal("can't open $source{hosts}: $!");
	while (<HOSTS>) {
		chop;
		s/#.*//go;
		s/^\s+//o;
		s/\s+$//o;
		next if (/^$/o);
		($addr, $rest) = /(\S*)\s+(.*)/o;
		$addr2host{$addr} = $rest;
		$hostsline{$addr} = $_;
		# print "$addr\t$addr2host{$addr}\n";
	}
	close HOSTS;
	$have_hosts = 1;
}

sub hosts_byname {
	local($addr, $hname);

	&hosts_read;

	print OUT "YP_INTERDOMAIN\tYP_INTERDOMAIN\n";
	foreach $addr (keys %addr2host) {
		foreach $hname (split(/\s+/, $addr2host{$addr})) {
			printf OUT "$hname\t$hostsline{$addr}\n";
		}
	}
}

sub hosts_byaddr {
	local($addr);

	&hosts_read;

	print OUT "YP_INTERDOMAIN\tYP_INTERDOMAIN\n";
	foreach $addr (keys %addr2host) {
		printf OUT "$addr\t$hostsline{$addr}\n";
	}
}

1;
