printf(";; WHEN: %s", ctime((time_t *) &(exectime.tv_sec)));
tt = (time_t)exectime.tv_sec; printf(";; WHEN: %s", ctime(&tt));
main(argc, argv)	int argc;	char **argv;{	struct hostent *hp;	short port = htons(NAMESERVER_PORT);	/* Wierd stuff for SPARC alignment, hurts nothing else. */	union {		HEADER header_;		u_char packet_[PACKETSZ];	} packet_;#define	packet  (packet_.packet_)	u_char answer[8*1024];	int n;	char doping[90];	char pingstr[50];	char *afile;	char *addrc, *addrend, *addrbegin;	struct timeval exectime, tv1, tv2, start_time, end_time, query_time;	char *srv;	int anyflag = 0;	int sticky = 0;	int tmp; 	int qtypeSet;	int addrflag = 0;	int zone = 0;        int bytes_out, bytes_in;	char cmd[256];	char domain[MAXDNAME];        char msg[120], *msgptr;	char **vtmp;	char *args[DIG_MAXARGS];	char **ax;	char **ay;	int once = 1, dofile = 0; /* batch -vs- interactive control */	char fileq[100];	char *qptr;	int  fp;	int wait=0, delay;	int envset=0, envsave=0;	struct __res_state res_x, res_t;	char *pp;	res_init();	_res.pfcode = PRF_DEF;	qtypeSet = 0;	bzero(domain, (sizeof domain));	gethostname(myhostname, (sizeof myhostname));	defsrv = strcat(defbuf, inet_ntoa(_res.nsaddr.sin_addr));	res_x = _res; /* ** If LOCALDEF in environment, should point to file ** containing local favourite defaults.  Also look for file ** DiG.env (i.e. SAVEENV) in local directory. */	if ((((afile = (char *) getenv("LOCALDEF")) != (char *) NULL) &&	     ((fp = open(afile, O_RDONLY)) > 0)) ||	    ((fp = open(SAVEENV, O_RDONLY)) > 0)) {		read(fp, (char *)&res_x, (sizeof res_x));		close(fp);		_res = res_x;	} /* **   check for batch-mode DiG; also pre-scan for 'help' */	vtmp = argv;	ax = args;	while (*vtmp != NULL) {		if (strcmp(*vtmp, "-h") == 0 ||		    strcmp(*vtmp, "-help") == 0 ||		    strcmp(*vtmp, "-usage") == 0 ||		    strcmp(*vtmp, "help") == 0) {			Usage();			exit(0);		}		if (strcmp(*vtmp, "-f") == 0) {			dofile++; once=0;			if ((qfp = fopen(*++vtmp, "r")) == NULL) {				fflush(stdout);				perror("file open");				fflush(stderr);				exit(10);			}		} else {			if (ax - args == DIG_MAXARGS) {				fprintf(stderr, "dig: too many arguments\n");				exit(10);			}			*ax++ = strdup(*vtmp);		}		vtmp++;	}	_res.id = 1;	gettimeofday(&tv1, NULL); /* **  Main section: once if cmd-line query **                while !EOF if batch mode */	*fileq = '\0';	while ((dofile && (fgets(fileq,100,qfp) != NULL)) || 	       ((!dofile) && (once--))) 	{		if ((*fileq=='\n') || (*fileq=='#') || (*fileq==';')) {			continue; /* ignore blank lines & comments */		}/* * "sticky" requests that before current parsing args * return to current "working" environment (X******) */		if (sticky) {			printf(";; (using sticky settings)\n");			_res = res_x;		}/* concat cmd-line and file args */		ay = ax;		qptr = fileq;		stackarg(ay, qptr);		/* defaults */		queryType = T_NS;		queryClass = C_IN;		zone = 0;		*pingstr = 0;		srv = NULL;		sprintf(cmd,"\n; <<>> DiG %s <<>> ",VSTRING);		argv = args;		argc = ax - args;/* * More cmd-line options than anyone should ever have to * deal with .... */		while (*(++argv) != NULL && **argv != '\0') {			if ((strlen(*argv) + 2) >			    (sizeof(cmd)/sizeof(char) - strlen(cmd))) {				fprintf(stderr, "Command line too long.\n");				exit(1);			}			strcat(cmd,*argv); strcat(cmd," ");			if (**argv == '@') {				srv = (*argv+1);				continue;			}			if (**argv == '%')				continue;			if (**argv == '+') {				SetOption(*argv+1);				continue;			}	 			if (strncmp(*argv,"-nost",5) == 0) {				sticky = 0;				continue;			} else if (strncmp(*argv,"-st",3) == 0) {				sticky++;				continue;			} else if (strncmp(*argv,"-envsa",6) == 0) {				envsave++;				continue;			} else if (strncmp(*argv,"-envse",6) == 0) {				envset++;				continue;			}			if (**argv == '-') {				switch (argv[0][1]) { 				case 'T': wait = atoi(*++argv);					break;				case 'c': 					if ((tmp = atoi(*++argv))					    || *argv[0]=='0') {						queryClass = tmp;					} else if (tmp = StringToClass(*argv,								       0, NULL)						   ) {						queryClass = tmp;					} else {						printf(						  "; invalid class specified\n"						       );					}					break;				case 't': 					if ((tmp = atoi(*++argv))					    || *argv[0]=='0') {						queryType = tmp;						qtypeSet++;					} else if (tmp = StringToType(*argv,								      0, NULL)						   ) {						queryType = tmp;						qtypeSet++;					} else {						printf(						   "; invalid type specified\n"						       );						}					break;				case 'x':					if (!qtypeSet) {						queryType = T_ANY;						qtypeSet++;					}					if (!(addrc = *++argv)) {						printf(						       "; no arg for -x?\n"						       );						break;					}					addrend = addrc + strlen(addrc);					if (*addrend == '.')						*addrend = '\0';					*domain = '\0';					while (addrbegin = strrchr(addrc,'.')) {						strcat(domain, addrbegin+1);						strcat(domain, ".");						*addrbegin = '\0';					}					strcat(domain, addrc);					strcat(domain, ".in-addr.arpa.");					break;				case 'p': port = htons(atoi(*++argv)); break;				case 'P':					if (argv[0][2] != '\0')						strcpy(pingstr,&argv[0][2]);					else						strcpy(pingstr,"ping -s");					break;#if defined(__RES) && (__RES >= 19931104)				case 'n':					_res.ndots = atoi(&argv[0][2]);					break;#endif /*__RES*/				} /* switch - */				continue;			} /* if '-'   */			if ((tmp = StringToType(*argv, -1, NULL)) != -1) { 				if ((T_ANY == tmp) && anyflag++) {  					queryClass = C_ANY; 						continue; 				}				if (T_AXFR == tmp) {					_res.pfcode = PRF_ZONE;					zone++;				} else {					queryType = tmp; 					qtypeSet++;				}			} else if ((tmp = StringToClass(*argv, -1, NULL))				   != -1) { 				queryClass = tmp; 			} else {				bzero(domain, (sizeof domain));				sprintf(domain,"%s",*argv);			}		} /* while argv remains */		if (_res.pfcode & 0x80000)			printf("; pfcode: %08lx, options: %08lx\n",			       _res.pfcode, _res.options);	  /* * Current env. (after this parse) is to become the * new "working environmnet. Used in conj. with sticky. */		if (envset) {			res_x = _res;			envset = 0;		}/* * Current env. (after this parse) is to become the * new default saved environmnet. Save in user specified * file if exists else is SAVEENV (== "DiG.env"). */		if (envsave) {			afile = (char *) getenv("LOCALDEF");			if ((afile &&			     ((fp = open(afile,					 O_WRONLY|O_CREAT|O_TRUNC,					 S_IREAD|S_IWRITE)) > 0))			    ||			    ((fp = open(SAVEENV,					O_WRONLY|O_CREAT|O_TRUNC,					S_IREAD|S_IWRITE)) > 0)) {				write(fp, (char *)&_res, (sizeof _res));				close(fp);			}			envsave = 0;		}		if (_res.pfcode & RES_PRF_CMD)			printf("%s\n", cmd);		addrflag = anyflag = 0;/* * Find address of server to query. If not dot-notation, then * try to resolve domain-name (if so, save and turn off print  * options, this domain-query is not the one we want. Restore * user options when done. * Things get a bit weird since we need to use resolver to be * able to "put the resolver to work". */		srvbuf[0] = 0;		srvmsg = defsrv;		if (srv != NULL) {			struct in_addr addr;			if (inet_aton(srv, &addr)) {				_res.nscount = 1;				_res.nsaddr.sin_addr = addr;				srvmsg = strcat(srvbuf, srv);			} else {				res_t = _res;				_res.pfcode = 0;				_res.options = RES_DEFAULT;				res_init();				hp = gethostbyname(srv);				_res = res_t;				if (hp == NULL				    || hp->h_addr_list == NULL				    || *hp->h_addr_list == NULL) {					fflush(stdout);					fprintf(stderr,		"; Bad server: %s -- using default server and timer opts\n",						srv);					fflush(stderr);					srvmsg = defsrv;					srv = NULL;				} else {					u_int32_t **addr;					_res.nscount = 0;					for (addr = (u_int32_t**)hp->h_addr_list;					     *addr && (_res.nscount < MAXNS);					     addr++) {						_res.nsaddr_list[							_res.nscount++						].sin_addr.s_addr = **addr;					}					srvmsg = strcat(srvbuf,srv);					strcat(srvbuf, "  ");					strcat(srvmsg,					       inet_ntoa(_res.nsaddr.sin_addr)					       );				}			}			printf("; (%d server%s found)\n",			       _res.nscount, (_res.nscount==1)?"":"s");			_res.id += _res.retry;		}		{			int i;			for (i = 0;  i < _res.nscount;  i++) {				_res.nsaddr_list[i].sin_family = AF_INET;				_res.nsaddr_list[i].sin_port = port;			}			_res.id += _res.retry;		}		if (zone) {			int i;			for (i = 0;  i < _res.nscount;  i++) {				int x = printZone(domain,						  &_res.nsaddr_list[i]);				if (_res.pfcode & RES_PRF_STATS) {					struct timeval exectime;					gettimeofday(&exectime,NULL);					printf(";; FROM: %s to SERVER: %s\n",					       myhostname,					       inet_ntoa(_res.nsaddr_list[i]							 .sin_addr));					printf(";; WHEN: %s",					       ctime((time_t *) &(exectime.tv_sec)));				}				if (!x)					break;	/* success */			}			fflush(stdout);			continue;		}		if (*domain && !qtypeSet) {			queryType = T_A;			qtypeSet++;		}				bytes_out = n = res_mkquery(QUERY, domain,					    queryClass, queryType,					    NULL, 0, NULL,					    packet, sizeof(packet));		if (n < 0) {			fflush(stderr);			printf(";; res_mkquery: buffer too small\n\n");			continue;		}		eecode = 0;		if (_res.pfcode & RES_PRF_HEAD1)			__fp_resstat(NULL, stdout);		(void) gettimeofday(&start_time, NULL);		if ((bytes_in = n = res_send(packet, n,					     answer, sizeof(answer))) < 0) {			fflush(stdout);			n = 0 - n;			msg[0]=0;			strcat(msg,";; res_send to server ");			strcat(msg,srvmsg);			perror(msg);			fflush(stderr);			if (!dofile) {				if (eecode)					exit(eecode);				else					exit(9);			}		}		(void) gettimeofday(&end_time, NULL);		if (_res.pfcode & RES_PRF_STATS) {			query_time = difftv(start_time, end_time);			printf(";; Total query time: ");			prnttime(query_time);			putchar('\n');			gettimeofday(&exectime,NULL);			printf(";; FROM: %s to SERVER: %s\n",			       myhostname, srvmsg);			printf(";; WHEN: %s",			       ctime((time_t *) &(exectime.tv_sec)));			printf(";; MSG SIZE  sent: %d  rcvd: %d\n",			       bytes_out, bytes_in);		}	  		fflush(stdout);/* *   Argh ... not particularly elegant. Should put in *real* ping code. *   Would necessitate root priviledges for icmp port though! */		if (*pingstr) {			sprintf(doping,"%s %s 56 3 | tail -3",pingstr,				(srv==NULL)?(defsrv+10):srv);			system(doping);		}		putchar('\n');/* * Fairly crude method and low overhead method of keeping two * batches started at different sites somewhat synchronized. */		gettimeofday(&tv2, NULL);		delay = (int)(tv2.tv_sec - tv1.tv_sec);		if (delay < wait) {			sleep(wait - delay);		}	}	return(eecode);}