#!/usr/local/bin/perl5.00502
#	program:	bits2mask
#	release:	1.5.0
#	input:		the number of network bits as an integer number
#			between 0 and 32.
#	output:		a dotted-quad netmask with that many network bits
#			to stdout, or "ERROR" to stdout if problem occurs.
#			Usage printed to stderr, too, if usage error occurs.
#	example:	"26" -> "255.255.255.192"
#	exit value:	0 if nothing goes wrong, 1 if something did.
#
# Copyright (C) 1993-2000 Paul A. Balyoz <pab@domtools.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

@chart = (0, 128, 192, 224, 240, 248, 252, 254, 255);


sub usage {
	print STDERR "usage: $0 numbits\n";
	print "ERROR\n";
	exit 1;
}

&usage  if $#ARGV != 0;			# exactly one arg, or error

$bits = $ARGV[0];
&usage  if $bits =~ /[^0-9]/;		# any non-digits is an error
if ($bits < 0 || $bits > 32) {		# must be in range 0..32 inclusive
	print STDERR "$0: error: numbits must be between 0 and 32 inclusive.\n";
	&usage;
}

for ($i=0; $i<4; $i++) {
	$c = $bits > 8 ? 8 : $bits;
	$bits -= $c;
	print $chart[$c];
	print ($i < 3 ? "." : "\n");
}

exit 0;
