commit 2d0b19ccf179f18cfdb328b0b0d3a8626a09ef8b Author: Jack-Benny Persson Date: Fri Mar 7 10:45:29 2014 +0100 Initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..dc6f96f --- /dev/null +++ b/LICENSE @@ -0,0 +1,16 @@ +Copyright (C) 2014 Jack-Benny Persson + +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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ac6b05b --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +subnetcalc: subnetcalc.c + gcc subnetcalc.c -lm -o subnetcalc + +clean: subnetcalc + rm subnetcalc diff --git a/README.md b/README.md new file mode 100644 index 0000000..e157b55 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# 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. + +Simply build it (make), run it (./subnetcalc) and enter the subnet +mask in slash notation but without the slash. +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. + diff --git a/subnetcalc.c b/subnetcalc.c new file mode 100644 index 0000000..838157a --- /dev/null +++ b/subnetcalc.c @@ -0,0 +1,38 @@ +/* A very simple subnet calculator in C, written by + * Jack-Benny Persson (jack-benny@cyberinfo.se). + * Released under GNU GPLv2. +*/ + +#include +#include + +main() +{ + int net; + double hosts; + double addr; + printf("Enter netmask in slash-notation without the slash: "); + scanf("%d", &net); + + 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); + + hosts = addr-2; + if (hosts < 0) // Check if number of usable hosts is a negative value + { + hosts = 0; // Set usable hosts to zero if it was negative + } + printf("%.0lf usable addresses for hosts\n", hosts); + return 0; + } + else // If the user entered anything else than 0-32 + { + printf("Only values between 0-32 are accepted\n"); + return 1; + } + + return 1; +}