Updated subnetcalc.c and the binary

This commit is contained in:
Jack-Benny Persson 2020-09-13 19:45:33 +02:00
parent 6fe650acdc
commit 85b8ec1be7
3 changed files with 11 additions and 12 deletions

View File

@ -1,7 +1,6 @@
subnetcalc: subnetcalc.c
gcc -Wall -pedantic subnetcalc.c -lm -static -o subnetcalc
gcc -std=c99 -Wall -pedantic subnetcalc.c -lm -static -o subnetcalc
docker build . -t subnetcalc
clean: subnetcalc
rm subnetcalc
docker image rm subnetcalc

Binary file not shown.

View File

@ -9,26 +9,26 @@
int main(void)
{
int net;
double hosts;
double addr;
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 (net >= 0 && net <= 32) /* Sanity check the user input */
{
printf("Netmask bit: %d\n\n", net);
addr = pow(2, 32-net); // Calculate number of addresses
printf("%.0lf total addresses\n", addr);
addr = pow(2, 32-net); /* Calculate number of addresses */
printf("%ld total addresses\n", addr);
hosts = addr-2;
if (hosts < 0) // Check if number of usable hosts is a negative value
hosts = addr-2;
/* Check if number of usable hosts is a negative value */
if (hosts < 0)
{
hosts = 0; // Set usable hosts to zero if it was negative
hosts = 0; /* Set usable hosts to zero if it was negative */
}
printf("%.0lf usable addresses for hosts\n", hosts);
printf("%ld usable addresses for hosts\n", hosts);
return 0;
}
else // If the user entered anything else than 0-32
else /* If the user entered anything else than 0-32 */
{
printf("Only values between 0-32 are accepted\n");
return 1;