119925Ssam # include	"../hdr/defines.h"
219925Ssam 
3*30497Slepreau static char Sccsid[] = "@(#)putline.c	1.2	02/15/87";
419925Ssam /*
519925Ssam 	Routine to write out either the current line in the packet
619925Ssam 	(if newline is zero) or the line specified by newline.
719925Ssam 	A line is actually written (and the x-file is only
819925Ssam 	opened) if pkt->p_upd is non-zero.  When the current line from
919925Ssam 	the packet is written, pkt->p_wrttn is set non-zero, and
1019925Ssam 	further attempts to write it are ignored.  When a line is
1119925Ssam 	read into the packet, pkt->p_wrttn must be turned off.
1219925Ssam */
1319925Ssam 
1419925Ssam int	Xcreate;
1519925Ssam FILE	*Xiop;
1619925Ssam 
1719925Ssam 
putline(pkt,newline)1819925Ssam putline(pkt,newline)
1919925Ssam register struct packet *pkt;
2019925Ssam char *newline;
2119925Ssam {
2219925Ssam 	static char obf[BUFSIZ];
2319925Ssam 	char *xf;
2419925Ssam 	register char *p;
2519925Ssam 
2619925Ssam 	if(pkt->p_upd == 0) return;
2719925Ssam 
2819925Ssam 	if(!Xcreate) {
2919925Ssam 		stat(pkt->p_file,&Statbuf);
3019925Ssam 		xf = auxf(pkt->p_file,'x');
3119925Ssam 		Xiop = xfcreat(xf,Statbuf.st_mode);
3219925Ssam 		setbuf(Xiop,obf);
3319925Ssam 		chown(xf,Statbuf.st_uid,Statbuf.st_gid);
3419925Ssam 	}
3519925Ssam 	if (newline)
3619925Ssam 		p = newline;
3719925Ssam 	else {
3819925Ssam 		if(!pkt->p_wrttn++)
3919925Ssam 			p = pkt->p_line;
4019925Ssam 		else
4119925Ssam 			p = 0;
4219925Ssam 	}
4319925Ssam 	if (p) {
4419925Ssam 		fputs(p,Xiop);
4519925Ssam 		if (Xcreate)
4619925Ssam 			while (*p)
47*30497Slepreau 				pkt->p_nhash += *p++;
4819925Ssam 	}
4919925Ssam 	Xcreate = 1;
5019925Ssam }
5119925Ssam 
5219925Ssam 
flushline(pkt,stats)5319925Ssam flushline(pkt,stats)
5419925Ssam register struct packet *pkt;
5519925Ssam register struct stats *stats;
5619925Ssam {
5719925Ssam 	register char *p;
5819925Ssam 	char ins[6], del[6], unc[6], hash[6];
5919925Ssam 
6019925Ssam 	if (pkt->p_upd == 0)
6119925Ssam 		return;
6219925Ssam 	putline(pkt,0);
6319925Ssam 	rewind(Xiop);
6419925Ssam 
6519925Ssam 	if (stats) {
6619925Ssam 		sprintf(ins,"%05u",stats->s_ins);
6719925Ssam 		sprintf(del,"%05u",stats->s_del);
6819925Ssam 		sprintf(unc,"%05u",stats->s_unc);
6919925Ssam 		for (p = ins; *p; p++)
70*30497Slepreau 			pkt->p_nhash += (*p - '0');
7119925Ssam 		for (p = del; *p; p++)
72*30497Slepreau 			pkt->p_nhash += (*p - '0');
7319925Ssam 		for (p = unc; *p; p++)
74*30497Slepreau 			pkt->p_nhash += (*p - '0');
7519925Ssam 	}
7619925Ssam 
7719925Ssam 	sprintf(hash,"%5u",pkt->p_nhash&0xFFFF);
7819925Ssam 	zeropad(hash);
7919925Ssam 	fprintf(Xiop,"%c%c%s\n",CTLCHAR,HEAD,hash);
8019925Ssam 	if (stats)
8119925Ssam 		fprintf(Xiop,"%c%c %s/%s/%s\n",CTLCHAR,STATS,ins,del,unc);
8219925Ssam 	fclose(Xiop);
8319925Ssam }
8419925Ssam 
8519925Ssam 
8619925Ssam xrm(pkt)
8719925Ssam struct packet *pkt;
8819925Ssam {
8919925Ssam 	if (Xiop)
9019925Ssam 		fclose(Xiop);
9119925Ssam 	Xiop = Xcreate = 0;
9219925Ssam }
93