Re-wrote the program to accept the netmask as an argument
This commit is contained in:
parent
41a934985a
commit
73b9e160b2
10
README.md
10
README.md
@ -1,6 +1,6 @@
|
||||
# subnetcalc #
|
||||
A very simple subnet calculator written in C. It's nothing fancy at all,
|
||||
just a small project of mine to get going with C.
|
||||
just a small project of mine to get me going with C.
|
||||
|
||||
Simply build it (make), run it (./subnetcalc) and enter the subnet
|
||||
mask in slash notation but without the slash.
|
||||
@ -8,3 +8,11 @@ For example enter 24 for a 24-bit subnet mask and the
|
||||
program will output the total number of addresses in the range aswell
|
||||
as the total number of usable address for hosts.
|
||||
|
||||
You can also give the netmask to the program as an argument. For example:
|
||||
|
||||
$> ./subnetcalc 24
|
||||
Netmask bit: 24
|
||||
|
||||
256 total addresses
|
||||
254 usable addresses for hosts
|
||||
|
||||
|
116
subnetcalc.c
116
subnetcalc.c
@ -2,37 +2,113 @@
|
||||
* Jack-Benny Persson (jack-benny@cyberinfo.se).
|
||||
* Released under GNU GPLv2.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void)
|
||||
int checkInput(char input[]);
|
||||
long int calcAddr(int netmask);
|
||||
long int calcHosts(int netmask);
|
||||
void printAddr(int netmask);
|
||||
void printHelp(char progname[]);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char input[3] = { 0 };
|
||||
int net;
|
||||
long int hosts, addr;
|
||||
printf("Enter netmask in slash-notation without the slash: ");
|
||||
scanf("%2d", &net);
|
||||
|
||||
if (net >= 0 && net <= 32) /* Sanity check the user input */
|
||||
if (argc == 1) /* User didn't supply an argument, go interactive */
|
||||
{
|
||||
printf("Netmask bit: %d\n\n", net);
|
||||
addr = pow(2, 32-net); /* Calculate number of addresses */
|
||||
printf("%ld total addresses\n", addr);
|
||||
printf("Enter netmask in slash-notation without the slash: ");
|
||||
scanf("%2c", input);
|
||||
|
||||
hosts = addr-2;
|
||||
/* Check if number of usable hosts is a negative value */
|
||||
if (hosts < 0)
|
||||
if (checkInput(input))
|
||||
{
|
||||
hosts = 0; /* Set usable hosts to zero if it was negative */
|
||||
}
|
||||
printf("%ld usable addresses for hosts\n", hosts);
|
||||
return 0;
|
||||
net = atoi(input);
|
||||
printAddr(net);
|
||||
return 0;
|
||||
}
|
||||
else /* If the user entered anything else than 0-32 */
|
||||
{
|
||||
printHelp(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else /* If the user entered anything else than 0-32 */
|
||||
else if (argc == 2) /* User supplied exactly one argument */
|
||||
{
|
||||
printf("Only values between 0-32 are accepted\n");
|
||||
if (checkInput(argv[1]))
|
||||
{
|
||||
net = atoi(argv[1]);
|
||||
printAddr(net);
|
||||
}
|
||||
else
|
||||
{
|
||||
printHelp(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else /* User supplied to many arguments */
|
||||
{
|
||||
printHelp(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int checkInput(char input[])
|
||||
{
|
||||
int netmask;
|
||||
/* Sanity check the user input */
|
||||
if( strspn(input, "0123456789.-\n") == strlen(input) )
|
||||
{
|
||||
netmask = atoi(input);
|
||||
if (netmask >= 0 && netmask <= 32)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Only values between 0 and 32 are accepted\n\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Only values between 0 and 32 are accepted\n\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
long int calcAddr(int netmask)
|
||||
{
|
||||
return pow(2, 32-netmask);
|
||||
}
|
||||
|
||||
long int calcHosts(int netmask)
|
||||
{
|
||||
long int hosts;
|
||||
hosts = (pow(2, 32-netmask))-2;
|
||||
if (hosts < 0) /* If usables hosts < 0, set to 0 */
|
||||
{
|
||||
hosts = 0;
|
||||
}
|
||||
return hosts;
|
||||
}
|
||||
|
||||
void printAddr(int netmask)
|
||||
{
|
||||
printf("Netmask bit: %d\n\n", netmask);
|
||||
printf("%ld total addresses\n", calcAddr(netmask));
|
||||
printf("%ld usable addresses for hosts\n", calcHosts(netmask));
|
||||
}
|
||||
|
||||
void printHelp(char progname[])
|
||||
{
|
||||
printf("Usage: %s [netmask]\n", progname);
|
||||
printf("If the program is started without any arguments, "
|
||||
"it's started in interactive mode.\n");
|
||||
printf("Optionally, a netmask can be supplied as a "
|
||||
"argument.\n");
|
||||
printf("Examle: %s 24\n", progname);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user