if (showHostname) { int x; hostinfo = gethostbyaddr((char *) &ip, sizeof(ip), AF_INET); if (!hostinfo) {
if (showHostname) { if ((hostName = (char *) get_hostname(ip.s_addr)) == NULL) {
int main(int argc, char ** argv) {    int showBroadcast = 0, showNetwork = 0, showHostname = 0, showNetmask = 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,		"Display calculated broadcast address", },	    { "hostname", '\0', 0, &showHostname, 0,		"Show hostname determined via DNS" },	    { "netmask", '\0', 0, &showNetmask, 0,		"Display default netmask for IP (class A, B, or C)" },	    { "network", '\0', 0, &showNetwork, 0,		"Display calculated network address", },	    { "silent", '\0', 0, &beSilent, 0,		"Don't ever display error messages " },	    POPT_AUTOHELP	    { NULL, '\0', 0, 0, 0 },    };    optCon = poptGetContext("ipcalc", argc, argv, optionsTable,0);    poptReadDefaultConfig(optCon, 1);    if ((rc = poptGetNextOpt(optCon)) < -1) {	if (!beSilent)	    fprintf(stderr, "ipcalc: bad argument %s: %s\n", 		    poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 		    poptStrerror(rc));	return 1;    }    if (!(ipStr = poptGetArg(optCon))) {	if (!beSilent)	    fprintf(stderr, "ipcalc: ip address 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))) {	if (!beSilent)	    fprintf(stderr, "ipcalc: unexpected argument: %s\n", chptr);	return 1;    }    poptFreeContext(optCon);    if (!inet_aton(ipStr, (struct in_addr *) &ip)) {	if (!beSilent)	    fprintf(stderr, "ipcalc: bad ip address: %s\n", ipStr);	return 1;    }    if (showNetmask) {	if (((ntohl(ip) & 0xFF000000) >> 24) <= 127)	    chptr = "255.0.0.0";	else if (((ntohl(ip) & 0xFF000000) >> 24) <= 191)	    chptr = "255.255.0.0";	else 	    chptr = "255.255.255.0";	printf("NETMASK=%s\n", chptr);    }    if (showBroadcast) {	broadcast = (ip & netmask) | ~netmask;	printf("BROADCAST=%s\n", inet_ntoa(*((struct in_addr *) &broadcast)));    }    if (showNetwork) {	network = ip & netmask;	printf("NETWORK=%s\n", inet_ntoa(*((struct in_addr *) &network)));    }    if (showHostname) {        int x;	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;	}        for (x=0; hostinfo->h_name[x]; x++) {		hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);	}	printf("HOSTNAME=%s\n", hostinfo->h_name);    }	    return 0;}