Added silent mode and a manual page

This commit is contained in:
Jack-Benny Persson 2020-10-09 20:41:11 +02:00
parent 6f21b1b08f
commit 88949110ab
4 changed files with 90 additions and 12 deletions

View File

@ -1,5 +1,23 @@
subnetcalc: subnetcalc.c
gcc -std=c99 -Wall -pedantic subnetcalc.c -lm -o subnetcalc
CC=gcc
CFLAGS=-Wall -pedantic -std=c99
LIBS=-lm
OBJS=subnetcalc.o
BINDIR=/usr/local/bin
MANDIR=/usr/share/man/man1
clean: subnetcalc
rm subnetcalc
subnetcalc: $(OBJS)
$(CC) -o subnetcalc $(OBJS) $(LIBS)
subnetcalc: subnetcalc.c
install: subnetcalc subnetcalc.1
gzip -c subnetcalc.1 > subnetcalc.1.gz
install -g root -o root -m 0644 subnetcalc.1.gz $(MANDIR)/
install -g root -o root -m 0755 subnetcalc $(BINDIR)/
uninstall:
rm $(MANDIR)/subnetcalc.1.gz
rm $(BINDIR)/subnetcalc
clean:
rm subnetcalc $(OBJS) subnetcalc.1.gz

View File

@ -1,4 +1,4 @@
# subnetcalc #
# subnetcalc
A very simple subnet calculator written in C. It's nothing fancy at all,
just a small project of mine to get me going with C.
@ -16,3 +16,19 @@ You can also give the netmask to the program as an argument. For example:
256 total addresses
254 usable addresses for hosts
Since version 1.0 it's also possible to run the program in silent mode
accepting input in stdin. This is excellent for batch processing. But note that
in this mode the program only calculates the total number of addresses.
## Installation
make
sudo make install
## Uninstalling
sudo make uninstall
## Getting help
man subnetcalc

22
subnetcalc.1 Normal file
View File

@ -0,0 +1,22 @@
.TH SUBNETCALC 1 "OCTOBER 2020" "Version 1.0" "User Manuals"
.SH NAME
subnetcalc \- calculate the number of address in a subnet
.SH SYNOPSIS
.B subnetcalc \fR[netmask] [-s]
.SH DESCRIPTION
.B subnetcalc
is a simple subnet calculator. The user provides a subnet to the program,
either as a argument or interactivly. The program then calculates the total
number of addresses and usable addresses in that subnet. It is also possible to
do batch processing with \fB\-s\fR option.
.SH OPTIONS
.TP
.B \-s
Makes the program silent and accept input on stdin. In this mode, the program
will only calculate the total number of addresses in the given netmask.
.SH RETURN VALUES
Upon error, 1 is returned. Otherwise, 0 is returned.
.SH AUTHOR
Jack-Benny Persson <jack-benny@cyberinfo.se>
.SH "SEE ALSO"
.BR bc (1)

View File

@ -1,6 +1,7 @@
/* A very simple subnet calculator in C, written by
* Jack-Benny Persson (jack-benny@cyberinfo.se).
* Released under GNU GPLv2.
* Version 1.0 (2020-10-09)
*/
#include <stdio.h>
#include <math.h>
@ -15,7 +16,7 @@ void printHelp(char progname[]);
int main(int argc, char *argv[])
{
char input[3] = { 0 };
char input[4] = { 0 };
int net;
if (argc == 1) /* User didn't supply an argument, go interactive */
@ -37,7 +38,23 @@ int main(int argc, char *argv[])
}
else if (argc == 2) /* User supplied exactly one argument */
{
if (checkInput(argv[1]))
if ( strcmp("-s", argv[1]) == 0) /* User wants a silent batch */
{
while(fgets(input, sizeof(input), stdin) != NULL)
{
/* Check if netmask is numeric (and do conversion) */
if( checkInput(input) )
{
printf("%ld\n", calcAddr(atoi(input)));
}
else
{
printHelp(argv[0]);
return 1;
}
}
}
else if (checkInput(argv[1]))
{
net = atoi(argv[1]);
printAddr(net);
@ -105,11 +122,16 @@ void printAddr(int netmask)
void printHelp(char progname[])
{
printf("Usage: %s [netmask]\n", progname);
printf("Usage: %s [netmask] [-s]\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);
"it is started in interactive mode.\n"
"Optionally, a netmask can be supplied as a "
"argument.\n"
"If the program is started with the -s option, "
"the program will silently accept input on stdin,\n"
"outputting the number of total address on stdout\n");
printf("\nExample: %s \n"
"Example: %s 24\n"
"Example: %s -s\n", progname, progname, progname);
}