if (nflag && !nt || iflag && nt)
if ((nflag && !nt) || (iflag && nt))
main(int argc, char **argv){	int on;	int ret;	int nflag, iflag;	struct timeval ntime;	struct servent *srvp;	char *inbuf = NULL, *cp, *cplim;	struct ifconf ifc;	struct ifreq ifreq, ifreqf, *ifr;	register struct netinfo *ntp;	struct netinfo *ntip;	struct netinfo *savefromnet;	struct netent *nentp;	struct nets *nt;	struct sockaddr_in server;	u_short port;	int inlen = 8192;	int c;	extern char *optarg;	extern int optind, opterr;#ifdef sgi	FILE *timetrim_st;#endif#define	IN_MSG "timed: -i and -n make no sense together\n"#ifdef sgi	struct tms tms;#define USAGE "timed: [-dtM] [-i net|-n net] [-F host1 host2 ...] [-G netgp] [-P trimfile]\n"#else#ifdef HAVENIS#define USAGE "timed: [-dtM] [-i net|-n net] [-F host1 host2 ...] [-G netgp]\n"#else#define USAGE "timed: [-dtM] [-i net|-n net] [-F host1 host2 ...]\n"#endif /* HAVENIS */#endif /* sgi */#ifdef lint	ntip = NULL;#endif	on = 1;	nflag = OFF;	iflag = OFF;#ifdef sgi	if (0 > syssgi(SGI_GETTIMETRIM, &timetrim)) {		perror("timed: syssgi(GETTIMETRIM)");		timetrim = 0;	}	tot_ticks = hr_ticks = times(&tms);#endif /* sgi */	opterr = 0;	while ((c = getopt(argc, argv, "Mtdn:i:F:G:P:")) != -1) {		switch (c) {		case 'M':			Mflag = 1;			break;		case 't':			trace = 1;			break;		case 'n':			if (iflag) {				fprintf(stderr, IN_MSG);				exit(1);			} else {				nflag = ON;				addnetname(optarg);			}			break;		case 'i':			if (nflag) {				fprintf(stderr, IN_MSG);				exit(1);			} else {				iflag = ON;				addnetname(optarg);			}			break;		case 'F':			add_good_host(optarg,1);			while (optind < argc && argv[optind][0] != '-')				add_good_host(argv[optind++], 1);			break;		case 'd':			debug = 1;			break;		case 'G':			if (goodgroup != 0) {				fprintf(stderr,"timed: only one net group\n");				exit(1);			}			goodgroup = optarg;			break;#ifdef sgi		case 'P':			timetrim_fn = optarg;			break;#endif /* sgi */		default:			fprintf(stderr, USAGE);			exit(1);			break;		}	}	if (optind < argc) {		fprintf(stderr, USAGE);		exit(1);	}#ifdef sgi	if (timetrim_fn == 0) {		;	} else if (0 == (timetrim_st = fopen(timetrim_fn, "r+"))) {		if (errno != ENOENT) {			(void)fprintf(stderr,"timed: ");			perror(timetrim_fn);			timetrim_fn = 0;		}	} else {		int i;		long trim;		double adj, ticks;		i = fscanf(timetrim_st, timetrim_rpat,			   &trim, &adj, &ticks);		if (i < 1		    || trim > MAX_TRIM		    || trim < -MAX_TRIM		    || i == 2		    || (i == 3			&& trim != rint(adj*CLK_TCK/ticks))) {			if (trace && i != EOF)				(void)fprintf(stderr,		    "timed: unrecognized contents in %s\n",					      timetrim_fn);		} else {			if (0 > syssgi(SGI_SETTIMETRIM,				       trim)) {			 perror("timed: syssgi(SETTIMETRIM)");			} else {				timetrim = trim;			}			if (i == 3)				tot_ticks -= ticks;		}		(void)fclose(timetrim_st);	}#endif /* sgi */	/* If we care about which machine is the master, then we must	 *	be willing to be a master	 */	if (0 != goodgroup || 0 != goodhosts)		Mflag = 1;	if (gethostname(hostname, sizeof(hostname) - 1) < 0) {		perror("gethostname");		exit(1);	}	self.l_bak = &self;	self.l_fwd = &self;	self.h_bak = &self;	self.h_fwd = &self;	self.head = 1;	self.good = 1;	if (goodhosts != 0)		/* trust ourself */		add_good_host(hostname,1);	srvp = getservbyname("timed", "udp");	if (srvp == 0) {		fprintf(stderr, "unknown service 'timed/udp'\n");		exit(1);	}	port = srvp->s_port;	bzero(&server, sizeof(server));	server.sin_port = srvp->s_port;	server.sin_family = AF_INET;	sock = socket(AF_INET, SOCK_DGRAM, 0);	if (sock < 0) {		perror("socket");		exit(1);	}	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&on,							sizeof(on)) < 0) {		perror("setsockopt");		exit(1);	}	if (bind(sock, (struct sockaddr*)&server, sizeof(server))) {		if (errno == EADDRINUSE)			fprintf(stderr,"timed: time daemon already running\n");		else			perror("bind");		exit(1);	}#ifdef sgi	/*	 * handle many slaves with our buffer	 */	if (0 > setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&bufspace,			 sizeof(bufspace))) {		perror("setsockopt");		exit(1);	}#endif /* sgi */	/* choose a unique seed for random number generation */	(void)gettimeofday(&ntime, 0);	srandom(ntime.tv_sec + ntime.tv_usec);	sequence = random();     /* initial seq number */#ifndef sgi	/* rounds kernel variable time to multiple of 5 ms. */	ntime.tv_sec = 0;	ntime.tv_usec = -((ntime.tv_usec/1000) % 5) * 1000;	(void)adjtime(&ntime, (struct timeval *)0);#endif /* sgi */	for (nt = nets; nt; nt = nt->next) {		nentp = getnetbyname(nt->name);		if (nentp == 0) {			nt->net = inet_network(nt->name);			if (nt->net != INADDR_NONE)				nentp = getnetbyaddr(nt->net, AF_INET);		}		if (nentp != 0) {			nt->net = nentp->n_net;		} else if (nt->net == INADDR_NONE) {			fprintf(stderr, "timed: unknown net %s\n", nt->name);			exit(1);		} else if (nt->net == INADDR_ANY) {			fprintf(stderr, "timed: bad net %s\n", nt->name);			exit(1);		} else {			fprintf(stderr,				"timed: warning: %s unknown in /etc/networks\n",				nt->name);		}		if (0 == (nt->net & 0xff000000))		    nt->net <<= 8;		if (0 == (nt->net & 0xff000000))		    nt->net <<= 8;		if (0 == (nt->net & 0xff000000))		    nt->net <<= 8;	}	while (1) {		char *ninbuf;		ifc.ifc_len = inlen;		ninbuf = realloc(inbuf, inlen);		if (ninbuf == NULL) {			if (inbuf)				free(inbuf);			close(sock);			return (-1);		}		ifc.ifc_buf = inbuf = ninbuf;		if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {			(void) close(sock);			free(inbuf);			perror("timed: get interface configuration");			exit(1);		}		if (ifc.ifc_len + sizeof(ifreq) < inlen)			break;		inlen *= 2;	}	ntp = NULL;#ifdef sgi#define size(p)	(sizeof(*ifr) - sizeof(ifr->ifr_name))  /* XXX hack. kludge */#else#define size(p)	max((p).sa_len, sizeof(p))#endif	cplim = inbuf + ifc.ifc_len; /*skip over if's with big ifr_addr's */	for (cp = inbuf; cp < cplim;			cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {		ifr = (struct ifreq *)cp;		if (ifr->ifr_addr.sa_family != AF_INET)			continue;		if (!ntp)			ntp = (struct netinfo*)malloc(sizeof(struct netinfo));		bzero(ntp,sizeof(*ntp));		ntp->my_addr=((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;		ntp->status = NOMASTER;		ifreq = *ifr;		ifreqf = *ifr;		if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreqf) < 0) {			perror("get interface flags");			continue;		}		if ((ifreqf.ifr_flags & IFF_UP) == 0)			continue;		if ((ifreqf.ifr_flags & IFF_BROADCAST) == 0 &&		    (ifreqf.ifr_flags & IFF_POINTOPOINT) == 0) {			continue;		}		((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr = ntp->my_addr;		if (ioctl(sock, SIOCGIFNETMASK, (char *)&ifreq) < 0) {			perror("get netmask");			continue;		}		ntp->mask = ((struct sockaddr_in *)			&ifreq.ifr_addr)->sin_addr.s_addr;		if (ifreqf.ifr_flags & IFF_BROADCAST) {			((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr =				ntp->my_addr;			if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {				perror("get broadaddr");				continue;			}			ntp->dest_addr = *(struct sockaddr_in *)&ifreq.ifr_broadaddr;			/* What if the broadcast address is all ones?			 * So we cannot just mask ntp->dest_addr.  */			ntp->net = ntp->my_addr;			ntp->net.s_addr &= ntp->mask;		} else { 			((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr =				ntp->my_addr;			if (ioctl(sock, SIOCGIFDSTADDR,						(char *)&ifreq) < 0) {				perror("get destaddr");				continue;			}			ntp->dest_addr = *(struct sockaddr_in *)&ifreq.ifr_dstaddr;			ntp->net = ntp->dest_addr.sin_addr;		}		ntp->dest_addr.sin_port = port;		for (nt = nets; nt; nt = nt->next) {			if (ntohl(ntp->net.s_addr) == nt->net)				break;		}		if (nflag && !nt || iflag && nt)			continue;		ntp->next = NULL;		if (nettab == NULL) {			nettab = ntp;		} else {			ntip->next = ntp;		}		ntip = ntp;		ntp = NULL;	}	if (ntp)		(void) free((char *)ntp);	if (nettab == NULL) {		fprintf(stderr, "timed: no network usable\n");		exit(1);	}	free(inbuf);#ifdef sgi	(void)schedctl(RENICE,0,10);	   /* run fast to get good time */	/* ticks to delay before responding to a broadcast */	delay1 = casual(0, CLK_TCK/10);#else	/* microseconds to delay before responding to a broadcast */	delay1 = casual(1, 100*1000);#endif /* sgi */	/* election timer delay in secs. */	delay2 = casual(MINTOUT, MAXTOUT);#ifdef sgi	(void)_daemonize(debug ? _DF_NOFORK|_DF_NOCHDIR : 0, sock, -1, -1);#else	if (!debug)		daemon(debug, 0);#endif /* sgi */	if (trace)		traceon();	openlog("timed", LOG_CONS|LOG_PID, LOG_DAEMON);	/*	 * keep returning here	 */	ret = setjmp(jmpenv);	savefromnet = fromnet;	setstatus();	if (Mflag) {		switch (ret) {		case 0:			checkignorednets();			pickslavenet(0);			break;		case 1:			/* Just lost our master */			if (slavenet != 0)				slavenet->status = election(slavenet);			if (!slavenet || slavenet->status == MASTER) {				checkignorednets();				pickslavenet(0);			} else {				makeslave(slavenet);	/* prune extras */			}			break;		case 2:			/* Just been told to quit */			justquit = 1;			pickslavenet(savefromnet);			break;		}		setstatus();		if (!(status & MASTER) && sock_raw != -1) {			/* sock_raw is not being used now */			(void)close(sock_raw);			sock_raw = -1;		}		if (status == MASTER)			master();		else			slave();	} else {		if (sock_raw != -1) {			(void)close(sock_raw);			sock_raw = -1;		}		if (ret) {			/* we just lost our master or were told to quit */			justquit = 1;		}		for (ntp = nettab; ntp != NULL; ntp = ntp->next) {			if (ntp->status == MASTER)				rmnetmachs(ntp);				ntp->status = NOMASTER;		}		checkignorednets();		pickslavenet(0);		setstatus();		slave();	}	/* NOTREACHED */#ifdef lint	return(0);#endif}