Radix cross Linux

The main Radix cross Linux repository contains the build scripts of packages, which have the most complete and common functionality for desktop machines

452 Commits   2 Branches   1 Tag
     5         kx /* ipmask.c
     5         kx  *
     5         kx  * Given argv[1] as a decimal netmask and argv[2] as a decimal IP address,
     5         kx  * print the resulting broadcast and network addresses to stdout.  This is
     5         kx  * potentially useful in scripts which need the broadcast address and the
     5         kx  * network address but want to ask the user as few questions as possible.
     5         kx  *
     5         kx  * Copyright 1994 by David Niemi.  Written in about 30 minutes on 13 Aug.
     5         kx  * The author places no restrictions on the use of this program, provided
     5         kx  * that this copyright is preserved in any derived source code.
     5         kx  *
     5         kx  * Typical compilation command for Linux:
     5         kx  *	cc ipmask.c -Wall -O -m486 -N -o ipmask -s
     5         kx  */
     5         kx 
     5         kx #define MYNAME "ipmask"
     5         kx 
     5         kx #include <stdlib.h>
     5         kx #include <stdio.h>
     5         kx 
     5         kx void Usage(void) {
     5         kx 	fprintf (stderr,
     5         kx 		"USAGE: %s <decimal netmask> <decimal IP address>\n",
     5         kx 		MYNAME);
     5         kx }
     5         kx 
     5         kx int main(int argc, char *argv[])
     5         kx {
     5         kx unsigned long netmask, ipaddr, netaddr, broadcast;
     5         kx int in[4], j;
     5         kx unsigned char bc[4],na[4];
     5         kx 
     5         kx 	if (3 != argc) {
     5         kx 		Usage();
     5         kx 		exit(1);
     5         kx 	}
     5         kx 
     5         kx 	/* Check netmask */
     5         kx 	if (4 != sscanf(argv[1],"%d.%d.%d.%d", &in[0],&in[1],&in[2],&in[3])) {
     5         kx 		fprintf (stderr,"Invalid netmask \"%s\".\n", argv[1]);
     5         kx 		Usage();
     5         kx 		exit(1);
     5         kx 	}
     5         kx 	for (j=0; j<4; ++j) {
     5         kx 		if (in[j]<0 || in[j]>255) {
     5         kx 			fprintf (stderr,
     5         kx 				"Invalid octet %d in netmask \"%s\".\n",
     5         kx 				j+1, argv[1]);
     5         kx 			Usage();
     5         kx 			exit(1);
     5         kx 		}
     5         kx 	}
     5         kx 	netmask = in[3] + 256 * (in[2] + 256 * (in[1] + 256 * in[0]));
     5         kx 
     5         kx 	/* Check IP address */
     5         kx 	if (4 != sscanf(argv[2],"%d.%d.%d.%d", &in[0],&in[1],&in[2],&in[3])) {
     5         kx 		fprintf (stderr,"Invalid IP address \"%s\".\n", argv[2]);
     5         kx 		Usage();
     5         kx 		exit(1);
     5         kx 	}
     5         kx 	for (j=0; j<4; ++j) {
     5         kx 		if (in[j]<0 || in[j]>255) {
     5         kx 			fprintf (stderr,
     5         kx 				"Invalid octet %d in IP address \"%s\".\n",
     5         kx 				j+1, argv[1]);
     5         kx 			Usage();
     5         kx 			exit(1);
     5         kx 		}
     5         kx 	}
     5         kx 	ipaddr = in[3] + 256 * (in[2] + 256 * (in[1] + 256 * in[0]));
     5         kx 
     5         kx 	broadcast = ipaddr | (~ netmask);
     5         kx 	bc[0] = broadcast / 256 / 256 / 256;
     5         kx 	bc[1] = (broadcast / 256 / 256) % 256;
     5         kx 	bc[2] = (broadcast / 256) % 256;
     5         kx 	bc[3] = broadcast % 256;
     5         kx 
     5         kx 	netaddr = ipaddr & netmask;
     5         kx 	na[0] = netaddr / 256 / 256 / 256;
     5         kx 	na[1] = (netaddr / 256 / 256) % 256;
     5         kx 	na[2] = (netaddr / 256) % 256;
     5         kx 	na[3] = netaddr % 256;
     5         kx 
     5         kx 	printf ("%d.%d.%d.%d %d.%d.%d.%d\n",
     5         kx 		bc[0], bc[1], bc[2], bc[3], na[0], na[1], na[2], na[3]);
     5         kx 
     5         kx 	exit(0);
     5         kx }