aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorErik Troan <ewt@redhat.com>1998-03-10 15:37:37 +0000
committerErik Troan <ewt@redhat.com>1998-03-10 15:37:37 +0000
commitf9610273f64504f14f1459448f93ec78d5f34422 (patch)
tree2093889af30f073f0b7c3749979feef9ade6ffc4
parent488a836a7b99d6e96030a2aeb72c17940dde81eb (diff)
downloadinitscripts-f9610273f64504f14f1459448f93ec78d5f34422.tar
initscripts-f9610273f64504f14f1459448f93ec78d5f34422.tar.gz
initscripts-f9610273f64504f14f1459448f93ec78d5f34422.tar.bz2
initscripts-f9610273f64504f14f1459448f93ec78d5f34422.tar.xz
initscripts-f9610273f64504f14f1459448f93ec78d5f34422.zip
added --hostname option
-rw-r--r--src/ipcalc.c58
1 files changed, 44 insertions, 14 deletions
diff --git a/src/ipcalc.c b/src/ipcalc.c
index 5640d8c3..2535bb6b 100644
--- a/src/ipcalc.c
+++ b/src/ipcalc.c
@@ -3,18 +3,24 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
+#include <netdb.h>
typedef unsigned int int32;
int main(int argc, char ** argv) {
- int showBroadcast = 0, showNetwork = 0;
+ int showBroadcast = 0, showNetwork = 0, showHostname = 0;
+ int beSilent = 0;
int rc;
poptContext optCon;
char * ipStr, * netmaskStr, * chptr;
int32 ip, netmask, network, broadcast;
+ struct hostent * hostinfo;
+ char errBuf[250];
struct poptOption optionsTable[] = {
{ "broadcast", '\0', 0, &showBroadcast, 0 },
+ { "hostname", '\0', 0, &showHostname, 0 },
{ "network", '\0', 0, &showNetwork, 0 },
+ { "silent", '\0', 0, &beSilent, 0 },
{ NULL, '\0', 0, 0, 0 },
};
@@ -22,38 +28,47 @@ int main(int argc, char ** argv) {
poptReadDefaultConfig(optCon, 1);
if ((rc = poptGetNextOpt(optCon)) < -1) {
- fprintf(stderr, "ipcalc: bad argument %s: %s\n",
- poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
- poptStrerror(rc));
+ if (!beSilent)
+ fprintf(stderr, "ipcalc: bad argument %s: %s\n",
+ poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
+ poptStrerror(rc));
return 1;
}
if (!(ipStr = poptGetArg(optCon))) {
- fprintf(stderr, "ipcalc: ip address expected\n");
+ if (!beSilent)
+ fprintf(stderr, "ipcalc: ip address expected\n");
return 1;
}
- if (!(netmaskStr = poptGetArg(optCon))) {
- fprintf(stderr, "ipcalc: netmask expected\n");
- return 1;
+ if (showBroadcast || showNetwork) {
+ if (!(netmaskStr = poptGetArg(optCon))) {
+ if (!beSilent)
+ fprintf(stderr, "ipcalc: netmask expected\n");
+ return 1;
+ }
+
+ if (!inet_aton(netmaskStr, (struct in_addr *) &netmask)) {
+ if (!beSilent)
+ fprintf(stderr, "ipcalc: bad netmask: %s\n", netmaskStr);
+ return 1;
+ }
}
if ((chptr = poptGetArg(optCon))) {
- fprintf(stderr, "ipcalc: unexpected argument: %s\n", chptr);
+ if (!beSilent)
+ fprintf(stderr, "ipcalc: unexpected argument: %s\n", chptr);
return 1;
}
poptFreeContext(optCon);
if (!inet_aton(ipStr, (struct in_addr *) &ip)) {
- fprintf(stderr, "ipcalc: bad ip address: %s\n", ipStr);
+ if (!beSilent)
+ fprintf(stderr, "ipcalc: bad ip address: %s\n", ipStr);
return 1;
}
- if (!inet_aton(netmaskStr, (struct in_addr *) &netmask)) {
- fprintf(stderr, "ipcalc: bad netmask: %s\n", netmaskStr);
- return 1;
- }
if (showBroadcast) {
broadcast = (ip & netmask) | ~netmask;
@@ -65,5 +80,20 @@ int main(int argc, char ** argv) {
printf("NETWORK=%s\n", inet_ntoa(*((struct in_addr *) &network)));
}
+ if (showHostname) {
+ hostinfo = gethostbyaddr((char *) &ip, sizeof(ip), AF_INET);
+ if (!hostinfo) {
+ if (!beSilent) {
+ sprintf(errBuf, "ipcalc: cannot find hostname for %s", ipStr);
+ herror(errBuf);
+ }
+
+ return 1;
+ }
+
+ printf("HOSTNAME=%s\n", hostinfo->h_name);
+ }
+
+
return 0;
}