--- dig.c.old	Tue Jun 16 12:14:11 1998
+++ dig.c	Tue Jun 16 15:06:38 1998
@@ -107,6 +107,14 @@
  ******************************************************************
  *      DiG -- Domain Information Groper                          *
  *                                                                *
+ *        dig.c - Version 8.1.3 (6/16/1998)                       *
+ *        Hacked by: Uwe Ohse <uwe@ohse.de>                       *
+ *               Computer Service Langenbach, nrw.net             *
+ *               o +noor option makes print_axfr printing full    *
+ *                 domain names                                   *
+ *               o some buffer overruns fixed                     *
+ *               o ignore all -P options after the first          *
+ *                                                                *
  *        dig.c - Version 2.1 (7/12/94) ("BIND takeover")         *
  *                                                                *
  *        Developed by: Steve Hotz & Paul Mockapetris             *
@@ -184,7 +192,7 @@
 /* Global. */
 
 #define VERSION 81
-#define VSTRING "8.1"
+#define VSTRING "8.1.3"
 
 #define PRF_DEF		0x2ff9
 #define PRF_MIN		0xA930
@@ -202,9 +210,9 @@
 #define SAVEENV "DiG.env"
 #define DIG_MAXARGS 30
 
-static char		*defsrv, *srvmsg;
+static char		*defsrv;
+static char		*srvmsg;
 static char		defbuf[40] = "default -- ";
-static char		srvbuf[60];
 static char		myhostname[MAXHOSTNAMELEN];
 
 /* stuff for nslookup modules */
@@ -220,6 +228,7 @@
 void		yyrestart(FILE *f) { }
 #endif
 char		*pager = NULL;
+static int	no_origin;
 /* end of nslookup stuff */
 
 /* Forward. */
@@ -249,8 +258,7 @@
 #define	packet  (packet_.packet_)
 	u_char answer[8*1024];
 	int n;
-	char doping[90];
-	char pingstr[50];
+	char *pingstr=NULL;
 	char *afile;
 	char *addrc, *addrend, *addrbegin;
 
@@ -263,11 +271,11 @@
 	int qtypeSet;
 	int addrflag = 0;
 	int zone = 0;
-        int bytes_out, bytes_in;
+	int bytes_out, bytes_in;
 
-	char cmd[256];
-	char domain[MAXDNAME];
-        char msg[120], *msgptr;
+	char *cmd=NULL;
+	size_t cmdsize=0;
+	char *domain=NULL;
 	char **vtmp;
 	char *args[DIG_MAXARGS];
 	char **ax;
@@ -282,9 +290,10 @@
 	res_init();
 	_res.pfcode = PRF_DEF;
 	qtypeSet = 0;
-	memset(domain, 0, sizeof domain);
 	gethostname(myhostname, (sizeof myhostname));
 	defsrv = strcat(defbuf, inet_ntoa(_res.nsaddr.sin_addr));
+	srvmsg = malloc(strlen(defsrv)+1);
+	strcpy(srvmsg,defsrv);
 	res_x = _res;
 
 /*
@@ -335,6 +344,12 @@
 	_res.id = 1;
 	gettimeofday(&tv1, NULL);
 
+	cmdsize=1024;
+	cmd=malloc(cmdsize);
+	if (!cmd) {
+		fprintf(stderr,"; out of memory\n");
+		exit(1);
+	}
 /*
  * Main section: once if cmd-line query
  *               while !EOF if batch mode
@@ -343,6 +358,7 @@
 	while ((dofile && fgets(fileq, sizeof fileq, qfp) != NULL) || 
 	       (!dofile && once--)) 
 	{
+		size_t cmdlen;
 		if (*fileq == '\n' || *fileq == '#' || *fileq==';')
 			continue;	/* ignore blank lines & comments */
 
@@ -364,10 +380,9 @@
 		queryType = ns_t_ns;
 		queryClass = ns_c_in;
 		zone = 0;
-		*pingstr = 0;
 		srv = NULL;
 
-		sprintf(cmd, "\n; <<>> DiG %s <<>> ", VSTRING);
+		cmdlen=sprintf(cmd, "\n; <<>> DiG %s <<>> ", VSTRING);
 		argv = args;
 		argc = ax - args;
 /*
@@ -375,8 +390,17 @@
  * deal with ....
  */
 		while (*(++argv) != NULL && **argv != '\0') { 
+			if (strlen(*argv)+1+cmdlen>=cmdsize) {
+				cmdsize=cmdsize*2+strlen(*argv);
+				cmd=realloc(cmd,cmdsize);
+				if (!cmd) {
+					fprintf(stderr,"; out of memory\n");
+					exit(1);
+				}
+			}
 			strcat(cmd, *argv);
 			strcat(cmd, " ");
+			cmdlen+=strlen(*argv)+1;
 			if (**argv == '@') {
 				srv = (*argv+1);
 				continue;
@@ -451,21 +475,29 @@
 					addrend = addrc + strlen(addrc);
 					if (*addrend == '.')
 						*addrend = '\0';
+					if (domain)
+						free(domain);
+					domain=malloc(strlen(addrc)+1+20);  /* ".in-addr.arpa" */
+					if (!domain) {fprintf(stderr, "; out of memory\n"); exit(1);}
 					*domain = '\0';
 					while ((addrbegin = strrchr(addrc,'.'))) {
 						strcat(domain, addrbegin+1);
 						strcat(domain, ".");
-						*addrbegin = '\0';
+						*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");
+					if (pingstr) {
+						printf( "; only one -P allowed\n");
+					}  else {
+						if (argv[0][2] != '\0')
+							pingstr=argv[0]+2;
+						else
+							pingstr=(char *)"ping -s"; /* oops, warning 'bout const beeing disqualified */
+					}
 					break;
 				case 'n':
 					_res.ndots = atoi(&argv[0][2]);
@@ -490,8 +522,11 @@
 				   != -1) { 
 				queryClass = tmp; 
 			} else {
-				memset(domain, 0, sizeof domain);
-				sprintf(domain,"%s",*argv);
+				if (domain)
+					free(domain);
+				domain=malloc(strlen(*argv)+1);
+				if (!domain) {fprintf(stderr, "; out of memory\n"); exit(1);}
+				strcpy(domain,*argv);
 			}
 		} /* while argv remains */
 
@@ -543,15 +578,17 @@
  * 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);
+				if (srvmsg)
+					free(srvmsg);
+				srvmsg = malloc(strlen(srv)+1);
+				if (!srvmsg) {fprintf(stderr, "; out of memory\n"); exit(1);}
+				strcpy(srvmsg,srv);
 			} else {
 				res_t = _res;
 				_res.pfcode = 0;
@@ -567,10 +604,15 @@
 		"; Bad server: %s -- using default server and timer opts\n",
 						srv);
 					fflush(stderr);
-					srvmsg = defsrv;
+					if (srvmsg)
+						free(srvmsg);
+					srvmsg = malloc(strlen(defsrv)+1);
+					if (!srvmsg) {fprintf(stderr, "; out of memory\n"); exit(1);}
+					strcpy(srvmsg,defsrv);
 					srv = NULL;
 				} else {
 					u_int32_t **addr;
+					char *in;
 
 					_res.nscount = 0;
 					for (addr = (u_int32_t**)hp->h_addr_list;
@@ -580,12 +622,14 @@
 							_res.nscount++
 						].sin_addr.s_addr = **addr;
 					}
-
-					srvmsg = strcat(srvbuf,srv);
-					strcat(srvbuf, "  ");
-					strcat(srvmsg,
-					       inet_ntoa(_res.nsaddr.sin_addr)
-					       );
+					if (srvmsg)
+						free(srvmsg);
+					in=inet_ntoa(_res.nsaddr.sin_addr);
+					srvmsg=malloc(strlen(srv)+3+strlen(in)); /* 2 space, 1 \0 */
+					if (!srvmsg) {fprintf(stderr, "; out of memory\n"); exit(1);}
+					strcpy(srvmsg,srv);
+					strcat(srvmsg, "  ");
+					strcat(srvmsg, in);
 				}
 			}
 			printf("; (%d server%s found)\n",
@@ -607,7 +651,7 @@
 			int i;
 
 			for (i = 0;  i < _res.nscount;  i++) {
-				int x = printZone(domain,
+				int x = printZone(domain ? domain : "",
 						  &_res.nsaddr_list[i]);
 				if (_res.pfcode & RES_PRF_STATS) {
 					struct timeval exectime;
@@ -628,12 +672,12 @@
 			continue;
 		}
 
-		if (*domain && !qtypeSet) {
+		if (domain && *domain && !qtypeSet) {
 			queryType = T_A;
 			qtypeSet++;
 		}
 		
-		bytes_out = n = res_mkquery(QUERY, domain,
+		bytes_out = n = res_mkquery(QUERY, domain ? domain : "",
 					    queryClass, queryType,
 					    NULL, 0, NULL,
 					    packet, sizeof packet);
@@ -650,10 +694,8 @@
 					     answer, sizeof answer)) < 0) {
 			fflush(stdout);
 			n = 0 - n;
-			msg[0]=0;
-			strcat(msg,";; res_send to server ");
-			strcat(msg,srvmsg);
-			perror(msg);
+			fprintf(stderr,";; res_send to server ");
+			perror(srvmsg);
 			fflush(stderr);
 
 			if (!dofile) {
@@ -686,10 +728,17 @@
  *   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);
+		if (pingstr) {
+			char *doping;
+			char *s=(srv==NULL)?(defsrv+10):srv;
+			doping=malloc(strlen(s)+strlen(pingstr)+20);
+			if (!doping) {
+				fprintf(stderr, "; can't ping %s: out of memory\n", srv);
+			} else {
+				sprintf(doping,"%s %s 56 3 | tail -3",pingstr,s);
+				system(doping);
+				free(doping);
+			}
 		}
 		putchar('\n');
 
@@ -735,7 +784,7 @@
 		[no]defname [no]search domain=NAME [no]ignore [no]primary\n\
 		[no]aaonly [no]cmd [no]stats [no]Header [no]header\n\
 		[no]ttlid [no]cl [no]qr [no]reply [no]ques [no]answer\n\
-		[no]author [no]addit pfdef pfmin pfset=# pfand=# pfor=#\n\
+		[no]author [no]addit pfdef pfmin pfset=# pfand=# pfor=# [no]op\n\
 ", stderr);
 	fputs("\
 notes:	defname and search don't work; use fully-qualified names.\n\
@@ -744,133 +793,142 @@
 
 static int
 SetOption(const char *string) {
-	char option[NAME_LEN], type[NAME_LEN], *ptr;
+	char *ptr;
 	int i;
 
-	i = sscanf(string, " %s", option);
-	if (i != 1) {
-		fprintf(stderr, ";*** Invalid option: %s\n",  option);
-		return (ERROR);
-	} 
-   
-	if (strncmp(option, "aa", 2) == 0) {	/* aaonly */
+	while (isspace(*string))
+		string++;
+
+	if (strncmp(string, "aa", 2) == 0) {	/* aaonly */
 		_res.options |= RES_AAONLY;
-	} else if (strncmp(option, "noaa", 4) == 0) {
+	} else if (strncmp(string, "noaa", 4) == 0) {
 		_res.options &= ~RES_AAONLY;
-	} else if (strncmp(option, "deb", 3) == 0) {	/* debug */
+	} else if (strncmp(string, "deb", 3) == 0) {	/* debug */
 		_res.options |= RES_DEBUG;
-	} else if (strncmp(option, "nodeb", 5) == 0) {
+	} else if (strncmp(string, "nodeb", 5) == 0) {
 		_res.options &= ~(RES_DEBUG | RES_DEBUG2);
-	} else if (strncmp(option, "ko", 2) == 0) {	/* keepopen */
+	} else if (strncmp(string, "ko", 2) == 0) {	/* keepopen */
 		_res.options |= (RES_STAYOPEN | RES_USEVC);
-	} else if (strncmp(option, "noko", 4) == 0) {
+	} else if (strncmp(string, "noko", 4) == 0) {
 		_res.options &= ~RES_STAYOPEN;
-	} else if (strncmp(option, "d2", 2) == 0) {	/* d2 (more debug) */
+	} else if (strncmp(string, "d2", 2) == 0) {	/* d2 (more debug) */
 		_res.options |= (RES_DEBUG | RES_DEBUG2);
-	} else if (strncmp(option, "nod2", 4) == 0) {
+	} else if (strncmp(string, "nod2", 4) == 0) {
 		_res.options &= ~RES_DEBUG2;
-	} else if (strncmp(option, "def", 3) == 0) {	/* defname */
+	} else if (strncmp(string, "def", 3) == 0) {	/* defname */
 		_res.options |= RES_DEFNAMES;
-	} else if (strncmp(option, "nodef", 5) == 0) {
+	} else if (strncmp(string, "nodef", 5) == 0) {
 		_res.options &= ~RES_DEFNAMES;
-	} else if (strncmp(option, "sea", 3) == 0) {	/* search list */
+	} else if (strncmp(string, "sea", 3) == 0) {	/* search list */
 		_res.options |= RES_DNSRCH;
-	} else if (strncmp(option, "nosea", 5) == 0) {
+	} else if (strncmp(string, "nosea", 5) == 0) {
 		_res.options &= ~RES_DNSRCH;
-	} else if (strncmp(option, "do", 2) == 0) {	/* domain */
-		ptr = strchr(option, '=');
-		if (ptr != NULL)
-			sscanf(++ptr, "%s", _res.defdname);
-	} else if (strncmp(option, "ti", 2) == 0) {      /* timeout */
-		ptr = strchr(option, '=');
+	} else if (strncmp(string, "do", 2) == 0) {	/* domain */
+		ptr = strchr(string, '=');
+		if (ptr != NULL) {
+			ptr++;
+			while (isspace(*ptr))
+				ptr++;
+			if (strlen(ptr)>=sizeof(_res.defdname)) {
+				fprintf(stderr, "; %s too long\n",ptr);
+				exit(1);
+			}
+			strcpy(_res.defdname,ptr);
+		}
+	} else if (strncmp(string, "ti", 2) == 0) {      /* timeout */
+		ptr = strchr(string, '=');
 		if (ptr != NULL)
 			sscanf(++ptr, "%d", &_res.retrans);
-	} else if (strncmp(option, "ret", 3) == 0) {    /* retry */
-		ptr = strchr(option, '=');
+	} else if (strncmp(string, "ret", 3) == 0) {    /* retry */
+		ptr = strchr(string, '=');
 		if (ptr != NULL)
 			sscanf(++ptr, "%d", &_res.retry);
-	} else if (strncmp(option, "i", 1) == 0) {	/* ignore */
+	} else if (strncmp(string, "i", 1) == 0) {	/* ignore */
 		_res.options |= RES_IGNTC;
-	} else if (strncmp(option, "noi", 3) == 0) {
+	} else if (strncmp(string, "noi", 3) == 0) {
 		_res.options &= ~RES_IGNTC;
-	} else if (strncmp(option, "pr", 2) == 0) {	/* primary */
+	} else if (strncmp(string, "pr", 2) == 0) {	/* primary */
 		_res.options |= RES_PRIMARY;
-	} else if (strncmp(option, "nop", 3) == 0) {
+	} else if (strncmp(string, "nop", 3) == 0) {
 		_res.options &= ~RES_PRIMARY;
-	} else if (strncmp(option, "rec", 3) == 0) {	/* recurse */
+	} else if (strncmp(string, "rec", 3) == 0) {	/* recurse */
 		_res.options |= RES_RECURSE;
-	} else if (strncmp(option, "norec", 5) == 0) {
+	} else if (strncmp(string, "norec", 5) == 0) {
 		_res.options &= ~RES_RECURSE;
-	} else if (strncmp(option, "v", 1) == 0) {	/* vc */
+	} else if (strncmp(string, "v", 1) == 0) {	/* vc */
 		_res.options |= RES_USEVC;
-	} else if (strncmp(option, "nov", 3) == 0) {
+	} else if (strncmp(string, "nov", 3) == 0) {
 		_res.options &= ~RES_USEVC;
-	} else if (strncmp(option, "pfset", 5) == 0) {
-		ptr = strchr(option, '=');
+	} else if (strncmp(string, "pfset", 5) == 0) {
+		ptr = strchr(string, '=');
 		if (ptr != NULL)
 			_res.pfcode = xstrtonum(++ptr);
-	} else if (strncmp(option, "pfand", 5) == 0) {
-		ptr = strchr(option, '=');
+	} else if (strncmp(string, "pfand", 5) == 0) {
+		ptr = strchr(string, '=');
 		if (ptr != NULL)
 			_res.pfcode = _res.pfcode & xstrtonum(++ptr);
-	} else if (strncmp(option, "pfor", 4) == 0) {
-		ptr = strchr(option, '=');
+	} else if (strncmp(string, "pfor", 4) == 0) {
+		ptr = strchr(string, '=');
 		if (ptr != NULL)
 			_res.pfcode |= xstrtonum(++ptr);
-	} else if (strncmp(option, "pfmin", 5) == 0) {
+	} else if (strncmp(string, "pfmin", 5) == 0) {
 		_res.pfcode = PRF_MIN;
-	} else if (strncmp(option, "pfdef", 5) == 0) {
+	} else if (strncmp(string, "pfdef", 5) == 0) {
 		_res.pfcode = PRF_DEF;
-	} else if (strncmp(option, "an", 2) == 0) {  /* answer section */
+	} else if (strncmp(string, "an", 2) == 0) {  /* answer section */
 		_res.pfcode |= RES_PRF_ANS;
-	} else if (strncmp(option, "noan", 4) == 0) {
+	} else if (strncmp(string, "noan", 4) == 0) {
 		_res.pfcode &= ~RES_PRF_ANS;
-	} else if (strncmp(option, "qu", 2) == 0) {  /* question section */
+	} else if (strncmp(string, "qu", 2) == 0) {  /* question section */
 		_res.pfcode |= RES_PRF_QUES;
-	} else if (strncmp(option, "noqu", 4) == 0) {  
+	} else if (strncmp(string, "noqu", 4) == 0) {  
 		_res.pfcode &= ~RES_PRF_QUES;
-	} else if (strncmp(option, "au", 2) == 0) {  /* authority section */
+	} else if (strncmp(string, "au", 2) == 0) {  /* authority section */
 		_res.pfcode |= RES_PRF_AUTH;
-	} else if (strncmp(option, "noau", 4) == 0) {  
+	} else if (strncmp(string, "noau", 4) == 0) {  
 		_res.pfcode &= ~RES_PRF_AUTH;
-	} else if (strncmp(option, "ad", 2) == 0) {  /* addition section */
+	} else if (strncmp(string, "ad", 2) == 0) {  /* addition section */
 		_res.pfcode |= RES_PRF_ADD;
-	} else if (strncmp(option, "noad", 4) == 0) {  
+	} else if (strncmp(string, "noad", 4) == 0) {  
 		_res.pfcode &= ~RES_PRF_ADD;
-	} else if (strncmp(option, "tt", 2) == 0) {  /* TTL & ID */
+	} else if (strncmp(string, "tt", 2) == 0) {  /* TTL & ID */
 		_res.pfcode |= RES_PRF_TTLID;
-	} else if (strncmp(option, "nott", 4) == 0) {  
+	} else if (strncmp(string, "nott", 4) == 0) {  
 		_res.pfcode &= ~RES_PRF_TTLID;
-	} else if (strncmp(option, "he", 2) == 0) {  /* head flags stats */
+	} else if (strncmp(string, "he", 2) == 0) {  /* head flags stats */
 		_res.pfcode |= RES_PRF_HEAD2;
-	} else if (strncmp(option, "nohe", 4) == 0) {  
+	} else if (strncmp(string, "nohe", 4) == 0) {  
 		_res.pfcode &= ~RES_PRF_HEAD2;
-	} else if (strncmp(option, "H", 1) == 0) {  /* header all */
+	} else if (strncmp(string, "H", 1) == 0) {  /* header all */
 		_res.pfcode |= RES_PRF_HEADX;
-	} else if (strncmp(option, "noH", 3) == 0) {  
+	} else if (strncmp(string, "noH", 3) == 0) {  
 		_res.pfcode &= ~(RES_PRF_HEADX);
-	} else if (strncmp(option, "qr", 2) == 0) {  /* query */
+	} else if (strncmp(string, "qr", 2) == 0) {  /* query */
 		_res.pfcode |= RES_PRF_QUERY;
-	} else if (strncmp(option, "noqr", 4) == 0) {  
+	} else if (strncmp(string, "noqr", 4) == 0) {  
 		_res.pfcode &= ~RES_PRF_QUERY;
-	} else if (strncmp(option, "rep", 3) == 0) {  /* reply */
+	} else if (strncmp(string, "rep", 3) == 0) {  /* reply */
 		_res.pfcode |= RES_PRF_REPLY;
-	} else if (strncmp(option, "norep", 5) == 0) {  
+	} else if (strncmp(string, "norep", 5) == 0) {  
 		_res.pfcode &= ~RES_PRF_REPLY;
-	} else if (strncmp(option, "cm", 2) == 0) {  /* command line */
+	} else if (strncmp(string, "cm", 2) == 0) {  /* command line */
 		_res.pfcode |= RES_PRF_CMD;
-	} else if (strncmp(option, "nocm", 4) == 0) {  
+	} else if (strncmp(string, "nocm", 4) == 0) {  
 		_res.pfcode &= ~RES_PRF_CMD;
-	} else if (strncmp(option, "cl", 2) == 0) {  /* class mnemonic */
+	} else if (strncmp(string, "cl", 2) == 0) {  /* class mnemonic */
 		_res.pfcode |= RES_PRF_CLASS;
-	} else if (strncmp(option, "nocl", 4) == 0) {  
+	} else if (strncmp(string, "nocl", 4) == 0) {  
 		_res.pfcode &= ~RES_PRF_CLASS;
-	} else if (strncmp(option, "st", 2) == 0) {  /* stats*/
+	} else if (strncmp(string, "st", 2) == 0) {  /* stats*/
 		_res.pfcode |= RES_PRF_STATS;
-	} else if (strncmp(option, "nost", 4) == 0) {  
+	} else if (strncmp(string, "nost", 4) == 0) {  
 		_res.pfcode &= ~RES_PRF_STATS;
+	} else if (strncmp(string, "or", 2) == 0) {  
+		no_origin=0;
+	} else if (strncmp(string, "noor", 4) == 0) {  
+		no_origin=1;
 	} else {
-		fprintf(stderr, "; *** Invalid option: %s\n",  option);
+		fprintf(stderr, "; *** Invalid option: %s\n",  string);
 		return (ERROR);
 	}
 	res_re_init();
@@ -957,7 +1015,7 @@
 	int count, type, class, rlen, done, n;
 	int numAnswers = 0, numRecords = 0, soacnt = 0;
 	u_char *cp, tmp[NS_INT16SZ];
-	char dname[2][NS_MAXDNAME], file[NAME_LEN];
+	char dname[2][NS_MAXDNAME];
 	enum { NO_ERRORS, ERR_READING_LEN, ERR_READING_MSG, ERR_PRINTING }
 		error = NO_ERRORS;
 
@@ -1164,7 +1222,7 @@
 	       msglen, ns_msg_count(handle, ns_s_an));
 #endif
 	for (;;) {
-		static char origin[NS_MAXDNAME], name_ctx[NS_MAXDNAME];
+		static char origin[NS_MAXDNAME]="", name_ctx[NS_MAXDNAME];
 		const char *name;
 		char buf[2048];		/* XXX need to malloc/realloc. */
 		ns_rr rr;
@@ -1177,17 +1235,21 @@
 			}
 			break;
 		}
-		name = ns_rr_name(rr);
-		if (origin[0] == '\0' && name[0] != '\0') {
-			fprintf(file, "$ORIGIN %s.\n", name);
-			strcpy(origin, name);
-		}
+		if (!no_origin) {
+			name = ns_rr_name(rr);
+			if (origin[0] == '\0' && name[0] != '\0') {
+				fprintf(file, "$ORIGIN %s.\n", name);
+				strcpy(origin, name);
+			}
+		} else
+			name=NULL;
 		if (ns_sprintrr(&handle, &rr, name_ctx, origin,
 				buf, sizeof buf) < 0) {
 			fprintf(file, ";; ns_sprintrr: %s\n", strerror(errno));
 			return (FORMERR);
 		}
-		strcpy(name_ctx, name);
+		if (name)
+			strcpy(name_ctx, name);
 		fputs(buf, file);
 		fputc('\n', file);
 	}
