12147Seric # include	"../hdr/defines.h"
22147Seric # include	"../hdr/had.h"
3*8341Ssam # include	<dir.h>
42147Seric 
5*8341Ssam SCCSID(@(#)get.c	4.4);
62147Seric USXALLOC();
72147Seric 
82147Seric int	Debug	0;
92147Seric struct packet gpkt;
102147Seric struct sid sid;
112147Seric unsigned	Ser;
122147Seric int	num_files;
132147Seric char	had[26];
142147Seric char	*ilist, *elist, *lfile;
152147Seric long	cutoff	0X7FFFFFFFL;	/* max positive long */
162147Seric int verbosity;
17*8341Ssam char	Gfile[MAXNAMLEN + 3];
182147Seric char	*Type;
192147Seric int	Did_id;
202147Seric 
212147Seric main(argc,argv)
222147Seric int argc;
232147Seric register char *argv[];
242147Seric {
252147Seric 	register int i;
262147Seric 	register char *p;
272147Seric 	char c;
282147Seric 	int testmore;
292147Seric 	extern int Fcnt;
302147Seric 	extern get();
312147Seric 
322147Seric 	Fflags = FTLEXIT | FTLMSG | FTLCLN;
332147Seric 	for(i=1; i<argc; i++)
342147Seric 		if(argv[i][0] == '-' && (c=argv[i][1])) {
352147Seric 			p = &argv[i][2];
362147Seric 			testmore = 0;
372147Seric 			switch (c) {
382147Seric 
392147Seric 			case 'a':
402147Seric 				if (!p[0]) {
412147Seric 					argv[i] = 0;
422147Seric 					continue;
432147Seric 				}
442147Seric 				Ser = patoi(p);
452147Seric 				break;
462147Seric 			case 'r':
472147Seric 				if (!p[0]) {
482147Seric 					argv[i] = 0;
492147Seric 					continue;
502147Seric 				}
512147Seric 				chksid(sid_ab(p,&sid),&sid);
522147Seric 				break;
532147Seric 			case 'c':
542147Seric 				if (!p[0]) {
552147Seric 					argv[i] = 0;
562147Seric 					continue;
572147Seric 				}
582147Seric 				if (date_ab(p,&cutoff))
592147Seric 					fatal("bad date/time (cm5)");
602147Seric 				break;
612147Seric 			case 'l':
622147Seric 				lfile = p;
632147Seric 				break;
642147Seric 			case 'i':
652147Seric 				if (!p[0]) {
662147Seric 					argv[i] = 0;
672147Seric 					continue;
682147Seric 				}
692147Seric 				ilist = p;
702147Seric 				break;
712147Seric 			case 'x':
722147Seric 				if (!p[0]) {
732147Seric 					argv[i] = 0;
742147Seric 					continue;
752147Seric 				}
762147Seric 				elist = p;
772147Seric 				break;
782147Seric 			case 'b':
792147Seric 			case 'g':
802147Seric 			case 'e':
812147Seric 			case 'p':
822147Seric 			case 'k':
832147Seric 			case 'm':
842147Seric 			case 'n':
852147Seric 			case 's':
862147Seric 			case 't':
872147Seric 				testmore++;
882147Seric 				break;
892147Seric 			default:
902147Seric 				fatal("unknown key letter (cm1)");
912147Seric 			}
922147Seric 
932147Seric 			if (testmore) {
942147Seric 				testmore = 0;
952147Seric 				if (*p)
962147Seric 					fatal(sprintf(Error,
972147Seric 					  "value after %c arg (cm8)",c));
982147Seric 			}
992147Seric 			if (had[c - 'a']++)
1002147Seric 				fatal("key letter twice (cm2)");
1012147Seric 			argv[i] = 0;
1022147Seric 		}
1032147Seric 		else num_files++;
1042147Seric 
1052147Seric 	if(num_files == 0)
1062147Seric 		fatal("missing file arg (cm3)");
1072147Seric 	if (HADE && HADM)
1082147Seric 		fatal("e not allowed with m (ge3)");
1092147Seric 	if (HADE || HADI || HADX)
1102147Seric 		HADK = 1;
1112147Seric 	if (!HADS)
1122147Seric 		verbosity = -1;
1132147Seric 	setsig();
1142147Seric 	Fflags =& ~FTLEXIT;
1152147Seric 	Fflags =| FTLJMP;
1162147Seric 	for (i=1; i<argc; i++)
1172147Seric 		if (p=argv[i])
1182147Seric 			do_file(p,get);
1192147Seric 	exit(Fcnt ? 1 : 0);
1202147Seric }
1212147Seric 
1222147Seric 
1232147Seric get(file)
1242147Seric {
1252147Seric 	register char *p;
1262147Seric 	register unsigned ser;
1272147Seric 	extern char had_dir, had_standinp;
1282147Seric 	extern char *Sflags[];
1292147Seric 	struct stats stats;
1302147Seric 	char	str[32];
1312147Seric 
1322147Seric 	if (setjmp(Fjmp))
1332147Seric 		return;
1342147Seric 	sinit(&gpkt,file,1);
1352147Seric 	gpkt.p_ixuser = (HADI | HADX);
1362147Seric 	gpkt.p_reqsid.s_rel = sid.s_rel;
1372147Seric 	gpkt.p_reqsid.s_lev = sid.s_lev;
1382147Seric 	gpkt.p_reqsid.s_br = sid.s_br;
1392147Seric 	gpkt.p_reqsid.s_seq = sid.s_seq;
1402147Seric 	gpkt.p_verbose = verbosity;
1412147Seric 	gpkt.p_stdout = (HADP ? stderr : stdout);
1422147Seric 	gpkt.p_cutoff = cutoff;
1432147Seric 	gpkt.p_lfile = lfile;
1442147Seric 	copy(auxf(gpkt.p_file,'g'),Gfile);
1452147Seric 
1462147Seric 	if (gpkt.p_verbose && (num_files > 1 || had_dir || had_standinp))
1472147Seric 		fprintf(gpkt.p_stdout,"\n%s:\n",gpkt.p_file);
1482147Seric 	if (dodelt(&gpkt,&stats,0,0) == 0)
1492147Seric 		fmterr(&gpkt);
1502147Seric 	finduser(&gpkt);
1512147Seric 	doflags(&gpkt);
1522147Seric 	if (!HADA)
1532147Seric 		ser = getser(&gpkt);
1542147Seric 	else {
1552147Seric 		if ((ser = Ser) > maxser(&gpkt))
1562147Seric 			fatal("serial number too large (ge19)");
1572147Seric 		move(&gpkt.p_idel[ser].i_sid, &gpkt.p_gotsid, sizeof(sid));
1582147Seric 		if (HADR && sid.s_rel != gpkt.p_gotsid.s_rel) {
1592147Seric 			zero(&gpkt.p_reqsid, sizeof(gpkt.p_reqsid));
1602147Seric 			gpkt.p_reqsid.s_rel = sid.s_rel;
1612147Seric 		}
1622147Seric 		else
1632147Seric 			move(&gpkt.p_gotsid, &gpkt.p_reqsid, sizeof(sid));
1642147Seric 	}
1652147Seric 	doie(&gpkt,ilist,elist,0);
1662147Seric 	setup(&gpkt,ser);
1672147Seric 	if (!(Type = Sflags[TYPEFLAG - 'a']))
1682147Seric 		Type = Null;
1692448Seric 	if (!(HADP || HADG) && writable(Gfile))
1702147Seric 		fatal(sprintf(Error,"writable `%s' exists (ge4)",Gfile));
1712147Seric 	if (gpkt.p_verbose) {
1722147Seric 		sid_ba(&gpkt.p_gotsid,str);
1732147Seric 		fprintf(gpkt.p_stdout,"%s\n",str);
1742147Seric 	}
1752147Seric 	if (HADE) {
1762147Seric 		if (!HADR)
1772147Seric 			move(&gpkt.p_gotsid,&gpkt.p_reqsid,
1782147Seric 			     sizeof(gpkt.p_reqsid));
1792147Seric 		newsid(&gpkt,Sflags[BRCHFLAG - 'a'] && HADB);
1802147Seric 		permiss(&gpkt);
1812147Seric 		wrtpfile(&gpkt,ilist,elist);
1822147Seric 	}
1832147Seric 	setuid(getuid());
1842147Seric 	if (HADL)
1852147Seric 		gen_lfile(&gpkt);
1862147Seric 	if (HADG) {
1872147Seric 		fclose(gpkt.p_iop);
1882147Seric 		xfreeall();
1892147Seric 		return;
1902147Seric 	}
1912147Seric 	flushto(&gpkt,EUSERTXT,1);
1922147Seric 	idsetup(&gpkt);
1932147Seric 	gpkt.p_chkeof = 1;
1942147Seric 	Did_id = 0;
1952147Seric 	while(readmod(&gpkt)) {
1962147Seric 		if (gpkt.p_gout == 0) {
1972147Seric 			if (HADP)
1982147Seric 				gpkt.p_gout = stdout;
1992147Seric 			else
2002539Seric 				gpkt.p_gout = xfcreat(Gfile,HADK ? 0666 : 0444);
2012147Seric 		}
2022147Seric 		prfx(&gpkt);
2032147Seric 		p = idsubst(&gpkt,gpkt.p_line);
2042147Seric 		fputs(p,gpkt.p_gout);
2052147Seric 	}
2062147Seric 	fflush(gpkt.p_gout);
2072147Seric 	if (gpkt.p_gout && gpkt.p_gout != stdout)
2082147Seric 		fclose(gpkt.p_gout);
2092147Seric 	if (gpkt.p_verbose)
2102147Seric 		fprintf(gpkt.p_stdout,"%u lines\n",gpkt.p_glnno);
2112147Seric 	if (!Did_id && !HADK)
2122147Seric 		if (Sflags[IDFLAG - 'a'])
2132147Seric 			fatal("no id keywords (cm6)");
2142147Seric 		else if (gpkt.p_verbose)
2152147Seric 			fprintf(stderr,"No id keywords (cm7)\n");
2162147Seric 	xfreeall();
2172147Seric }
2182147Seric 
2192448Seric writable(fn)
2202448Seric char *fn;
2212448Seric {
2222448Seric 	struct stat s;
2232147Seric 
2242448Seric 	return (stat(fn, &s) >= 0 && (s.st_mode & 0222) != 0);
2252448Seric }
2262448Seric 
2272448Seric 
2282147Seric newsid(pkt,branch)
2292147Seric register struct packet *pkt;
2302147Seric int branch;
2312147Seric {
2322147Seric 	int chkbr;
2332147Seric 
2342147Seric 	chkbr = 0;
2352147Seric 	if (pkt->p_reqsid.s_br == 0) {
2362147Seric 		pkt->p_reqsid.s_lev =+ 1;
2372147Seric 		if (sidtoser(&pkt->p_reqsid,pkt) ||
2382147Seric 			pkt->p_maxr > pkt->p_reqsid.s_rel || branch) {
2392147Seric 				pkt->p_reqsid.s_rel = pkt->p_gotsid.s_rel;
2402147Seric 				pkt->p_reqsid.s_lev = pkt->p_gotsid.s_lev;
2412147Seric 				pkt->p_reqsid.s_br = pkt->p_gotsid.s_br + 1;
2422147Seric 				pkt->p_reqsid.s_seq = 1;
2432147Seric 				chkbr++;
2442147Seric 		}
2452147Seric 	}
2462147Seric 	else if (pkt->p_reqsid.s_seq == 0 && !branch)
2472147Seric 		pkt->p_reqsid.s_seq = pkt->p_gotsid.s_seq + 1;
2482147Seric 	else {
2492147Seric 		pkt->p_reqsid.s_seq =+ 1;
2502147Seric 		if (branch || sidtoser(&pkt->p_reqsid,pkt)) {
2512147Seric 			pkt->p_reqsid.s_br =+ 1;
2522147Seric 			pkt->p_reqsid.s_seq = 1;
2532147Seric 			chkbr++;
2542147Seric 		}
2552147Seric 	}
2562147Seric 	if (chkbr)
2572147Seric 		while (sidtoser(&pkt->p_reqsid,pkt))
2582147Seric 			pkt->p_reqsid.s_br =+ 1;
2592147Seric 	if (sidtoser(&pkt->p_reqsid,pkt))
2602147Seric 		fatal("internal error in newsid()");
2612147Seric }
2622147Seric 
2632147Seric 
2642147Seric enter(pkt,ch,n,sidp)
2652147Seric struct packet *pkt;
2662147Seric char ch;
2672147Seric int n;
2682147Seric struct sid *sidp;
2692147Seric {
2702147Seric 	char str[32];
2712147Seric 	register struct apply *ap;
2722147Seric 
2732147Seric 	sid_ba(sidp,str);
2742147Seric 	if (pkt->p_verbose)
2752147Seric 		fprintf(pkt->p_stdout,"%s\n",str);
2762147Seric 	ap = &pkt->p_apply[n];
2772147Seric 	switch(ap->a_code) {
2782147Seric 
2792147Seric 	case EMPTY:
2802147Seric 		if (ch == INCLUDE)
2812147Seric 			condset(ap,APPLY,INCLUSER);
2822147Seric 		else
2832147Seric 			condset(ap,NOAPPLY,EXCLUSER);
2842147Seric 		break;
2852147Seric 	case APPLY:
2862147Seric 		sid_ba(sidp,str);
2872147Seric 		fatal(sprintf(Error,"%s already included (ge9)",str));
2882147Seric 		break;
2892147Seric 	case NOAPPLY:
2902147Seric 		sid_ba(sidp,str);
2912147Seric 		fatal(sprintf(Error,"%s already excluded (ge10)",str));
2922147Seric 		break;
2932147Seric 	default:
2942147Seric 		fatal("internal error in get/enter() (ge11)");
2952147Seric 		break;
2962147Seric 	}
2972147Seric }
2982147Seric 
2992147Seric 
3002147Seric gen_lfile(pkt)
3012147Seric register struct packet *pkt;
3022147Seric {
3032147Seric 	int n;
3042147Seric 	int reason;
3052147Seric 	char str[32];
3062147Seric 	char line[BUFSIZ];
3072147Seric 	struct deltab dt;
3082147Seric 	FILE *in;
3092147Seric 	FILE *out;
3102147Seric 
3112147Seric 	in = xfopen(pkt->p_file,0);
3122147Seric 	if (*pkt->p_lfile)
3132147Seric 		out = stdout;
3142147Seric 	else
3152147Seric 		out = xfcreat(auxf(pkt->p_file,'l'),0444);
3162147Seric 	fgets(line,sizeof(line),in);
3172147Seric 	while (fgets(line,sizeof(line),in) != NULL && line[0] == CTLCHAR && line[1] == STATS) {
3182147Seric 		fgets(line,sizeof(line),in);
3192147Seric 		del_ab(line,&dt);
3202147Seric 		if (dt.d_type == 'D') {
3212147Seric 			reason = pkt->p_apply[dt.d_serial].a_reason;
3222147Seric 			if (pkt->p_apply[dt.d_serial].a_code == APPLY) {
3232147Seric 				putc(' ',out);
3242147Seric 				putc(' ',out);
3252147Seric 			}
3262147Seric 			else {
3272147Seric 				putc('*',out);
3282147Seric 				if (reason & IGNR)
3292147Seric 					putc(' ',out);
3302147Seric 				else
3312147Seric 					putc('*',out);
3322147Seric 			}
3332147Seric 			switch (reason & (INCL | EXCL | CUTOFF)) {
3342147Seric 
3352147Seric 			case INCL:
3362147Seric 				putc('I',out);
3372147Seric 				break;
3382147Seric 			case EXCL:
3392147Seric 				putc('X',out);
3402147Seric 				break;
3412147Seric 			case CUTOFF:
3422147Seric 				putc('C',out);
3432147Seric 				break;
3442147Seric 			default:
3452147Seric 				putc(' ',out);
3462147Seric 				break;
3472147Seric 			}
3482147Seric 			putc(' ',out);
3492147Seric 			sid_ba(&dt.d_sid,str);
3502147Seric 			fprintf(out,"%s\t",str);
3512147Seric 			date_ba(&dt.d_datetime,str);
3522147Seric 			fprintf(out,"%s %s\n",str,dt.d_pgmr);
3532147Seric 		}
3542147Seric 		while ((n = fgets(line,sizeof(line),in)) != NULL)
3552147Seric 			if (line[0] != CTLCHAR)
3562147Seric 				break;
3572147Seric 			else {
3582147Seric 				switch (line[1]) {
3592147Seric 
3602147Seric 				case EDELTAB:
3612147Seric 					break;
3622147Seric 				default:
3632147Seric 					continue;
3642147Seric 				case MRNUM:
3652147Seric 				case COMMENTS:
3662147Seric 					if (dt.d_type == 'D')
3672147Seric 						fprintf(out,"\t%s",&line[3]);
3682147Seric 					continue;
3692147Seric 				}
3702147Seric 				break;
3712147Seric 			}
3722147Seric 		if (n == NULL || line[0] != CTLCHAR)
3732147Seric 			break;
3742147Seric 		putc('\n',out);
3752147Seric 	}
3762147Seric 	fclose(in);
3772147Seric 	if (out != stdout)
3782147Seric 		fclose(out);
3792147Seric }
3802147Seric 
3812147Seric 
3822147Seric char	Curdate[18];
3832147Seric char	*Curtime;
3842147Seric char	Gdate[9];
3852147Seric char	Chgdate[18];
3862147Seric char	*Chgtime;
3872147Seric char	Gchgdate[9];
3882147Seric char	Sid[32];
389*8341Ssam char	Mod[MAXNAMLEN + 3];		/* should be as large as Gfile? */
3902147Seric char	Olddir[BUFSIZ];
3912147Seric char	Pname[BUFSIZ];
3922147Seric char	Dir[BUFSIZ];
3932147Seric 
3942147Seric idsetup(pkt)
3952147Seric register struct packet *pkt;
3962147Seric {
3972147Seric 	extern long Timenow;
3982147Seric 	register int n;
3992147Seric 	register char *p;
4002147Seric 
4012147Seric 	date_ba(&Timenow,Curdate);
4022147Seric 	Curtime = &Curdate[9];
4032147Seric 	Curdate[8] = 0;
4042147Seric 	copy(pkt->p_file,Dir);
4052147Seric 	dname(Dir);
4062147Seric 	if(curdir(Olddir) != 0)
4072147Seric 		fatal("curdir failed (ge20)");
4082147Seric 	if(chdir(Dir) != 0)
4092147Seric 		fatal("cannot change directory (ge22)");
4102147Seric 	if(curdir(Pname) != 0)
4112147Seric 		fatal("curdir failed (ge21)");
4122147Seric 	if(chdir(Olddir) != 0)
4132147Seric 		fatal("cannot change directory (ge23)");
4142147Seric 	makgdate(Curdate,Gdate);
4152147Seric 	for (n = maxser(pkt); n; n--)
4162147Seric 		if (pkt->p_apply[n].a_code == APPLY)
4172147Seric 			break;
4182147Seric 	if (n)
4192147Seric 		date_ba(&pkt->p_idel[n].i_datetime,Chgdate);
4202147Seric 	Chgtime = &Chgdate[9];
4212147Seric 	Chgdate[8] = 0;
4222147Seric 	makgdate(Chgdate,Gchgdate);
4232147Seric 	sid_ba(&pkt->p_gotsid,Sid);
4242147Seric 	if (p = Sflags[MODFLAG - 'a'])
4252147Seric 		copy(p,Mod);
4262147Seric 	else
4272147Seric 		copy(Gfile,Mod);
4282147Seric }
4292147Seric 
4302147Seric 
4312147Seric makgdate(old,new)
4322147Seric register char *old, *new;
4332147Seric {
4342147Seric 	if ((*new = old[3]) != '0')
4352147Seric 		new++;
4362147Seric 	*new++ = old[4];
4372147Seric 	*new++ = '/';
4382147Seric 	if ((*new = old[6]) != '0')
4392147Seric 		new++;
4402147Seric 	*new++ = old[7];
4412147Seric 	*new++ = '/';
4422147Seric 	*new++ = old[0];
4432147Seric 	*new++ = old[1];
4442147Seric 	*new = 0;
4452147Seric }
4462147Seric 
4472147Seric 
4482147Seric static char Zkeywd[5]	"@(#)";
4492147Seric 
4502147Seric idsubst(pkt,line)
4512147Seric register struct packet *pkt;
4522147Seric char line[];
4532147Seric {
4542147Seric 	static char tline[BUFSIZ];
4552147Seric 	static char str[32];
4562147Seric 	register char *lp, *tp;
4572147Seric 	extern char *Type;
4582147Seric 	extern char *Sflags[];
4592147Seric 
4602147Seric 	if (HADK || !any('%',line))
4612147Seric 		return(line);
4622147Seric 
4632147Seric 	tp = tline;
4642147Seric 	for(lp=line; *lp != 0; lp++) {
4652147Seric 		if(lp[0] == '%' && lp[1] != 0 && lp[2] == '%') {
4662147Seric 			switch(*++lp) {
4672147Seric 
4682147Seric 			case 'M':
4692147Seric 				tp = trans(tp,Mod);
4702147Seric 				break;
4712147Seric 			case 'R':
4722147Seric 				sprintf(str,"%u",pkt->p_gotsid.s_rel);
4732147Seric 				tp = trans(tp,str);
4742147Seric 				break;
4752147Seric 			case 'L':
4762147Seric 				sprintf(str,"%u",pkt->p_gotsid.s_lev);
4772147Seric 				tp = trans(tp,str);
4782147Seric 				break;
4792147Seric 			case 'B':
4802147Seric 				sprintf(str,"%u",pkt->p_gotsid.s_br);
4812147Seric 				tp = trans(tp,str);
4822147Seric 				break;
4832147Seric 			case 'S':
4842147Seric 				sprintf(str,"%u",pkt->p_gotsid.s_seq);
4852147Seric 				tp = trans(tp,str);
4862147Seric 				break;
4872147Seric 			case 'D':
4882147Seric 				tp = trans(tp,Curdate);
4892147Seric 				break;
4902147Seric 			case 'H':
4912147Seric 				tp = trans(tp,Gdate);
4922147Seric 				break;
4932147Seric 			case 'T':
4942147Seric 				tp = trans(tp,Curtime);
4952147Seric 				break;
4962147Seric 			case 'E':
4972147Seric 				tp = trans(tp,Chgdate);
4982147Seric 				break;
4992147Seric 			case 'G':
5002147Seric 				tp = trans(tp,Gchgdate);
5012147Seric 				break;
5022147Seric 			case 'U':
5032147Seric 				tp = trans(tp,Chgtime);
5042147Seric 				break;
5052147Seric 			case 'Z':
5062147Seric 				tp = trans(tp,Zkeywd);
5072147Seric 				break;
5082147Seric 			case 'Y':
5092147Seric 				tp = trans(tp,Type);
5102147Seric 				break;
5112147Seric 			case 'W':
5122147Seric 				tp = trans(tp,Zkeywd);
5132147Seric 				tp = trans(tp,Mod);
5142147Seric 				*tp++ = '\t';
5152147Seric 			case 'I':
5162147Seric 				tp = trans(tp,Sid);
5172147Seric 				break;
5182147Seric 			case 'P':
5192147Seric 				tp = trans(tp,Pname);
5202147Seric 				*tp++ = '/';
5212147Seric 				tp = trans(tp,(sname(pkt->p_file)));
5222147Seric 				break;
5232147Seric 			case 'F':
5242147Seric 				tp = trans(tp,pkt->p_file);
5252147Seric 				break;
5262147Seric 			case 'C':
5272147Seric 				sprintf(str,"%u",pkt->p_glnno);
5282147Seric 				tp = trans(tp,str);
5292147Seric 				break;
5302147Seric 			case 'A':
5312147Seric 				tp = trans(tp,Zkeywd);
5322147Seric 				tp = trans(tp,Type);
5332147Seric 				*tp++ = ' ';
5342147Seric 				tp = trans(tp,Mod);
5352147Seric 				*tp++ = ' ';
5362147Seric 				tp = trans(tp,Sid);
5372147Seric 				tp = trans(tp,Zkeywd);
5382147Seric 				break;
5392147Seric 			default:
5402147Seric 				*tp++ = '%';
5412147Seric 				*tp++ = *lp;
5422147Seric 				continue;
5432147Seric 			}
5442147Seric 			lp++;
5452147Seric 		}
5462147Seric 		else
5472147Seric 			*tp++ = *lp;
5482147Seric 	}
5492147Seric 
5502147Seric 	*tp = 0;
5512147Seric 	return(tline);
5522147Seric }
5532147Seric 
5542147Seric 
5552147Seric trans(tp,str)
5562147Seric register char *tp, *str;
5572147Seric {
5582147Seric 	Did_id = 1;
5592147Seric 	while(*tp++ = *str++)
5602147Seric 		;
5612147Seric 	return(tp-1);
5622147Seric }
5632147Seric 
5642147Seric 
5652147Seric prfx(pkt)
5662147Seric register struct packet *pkt;
5672147Seric {
5682147Seric 	char str[32];
5692147Seric 
5702147Seric 	if (HADN)
5712147Seric 		fprintf(pkt->p_gout,"%s\t",Mod);
5722147Seric 	if (HADM) {
5732147Seric 		sid_ba(&pkt->p_inssid,str);
5742147Seric 		fprintf(pkt->p_gout,"%s\t",str);
5752147Seric 	}
5762147Seric }
5772147Seric 
5782147Seric 
5792147Seric clean_up(n)
5802147Seric {
5812147Seric 	if (gpkt.p_file[0])
5822147Seric 		unlockit(auxf(gpkt.p_file,'z'),getpid());
5832147Seric 	if (gpkt.p_iop)
5842147Seric 		fclose(gpkt.p_iop);
5852147Seric 	xfreeall();
5862147Seric }
5872147Seric 
5882147Seric 
5892147Seric wrtpfile(pkt,inc,exc)
5902147Seric register struct packet *pkt;
5912147Seric char *inc, *exc;
5922147Seric {
5932147Seric 	char line[64], str1[32], str2[32];
5942147Seric 	char *user;
5952147Seric 	FILE *in, *out;
5962147Seric 	struct pfile pf;
5972147Seric 	register char *p;
5982147Seric 	int fd;
5992147Seric 	int i;
6002147Seric 	extern long Timenow;
6012147Seric 
6022147Seric 	user = logname();
6032147Seric 	if (lockit(auxf(pkt->p_file,'z'),2,getpid()))
6042147Seric 		fatal("cannot create lock file (cm4)");
6052147Seric 	if (exists(p = auxf(pkt->p_file,'p'))) {
6062147Seric 		fd = xopen(p,2);
6072147Seric 		in = fdfopen(fd,0);
6082147Seric 		while (fgets(line,sizeof(line),in) != NULL) {
6092147Seric 			p = line;
6102147Seric 			p[length(p) - 1] = 0;
6112147Seric 			pf_ab(p,&pf,0);
6122147Seric 			if ((pf.pf_gsid.s_rel == pkt->p_gotsid.s_rel &&
6132147Seric 				pf.pf_gsid.s_lev == pkt->p_gotsid.s_lev &&
6142147Seric 				pf.pf_gsid.s_br == pkt->p_gotsid.s_br &&
6152147Seric 				pf.pf_gsid.s_seq == pkt->p_gotsid.s_seq) ||
6162147Seric 				(pf.pf_nsid.s_rel == pkt->p_reqsid.s_rel &&
6172147Seric 				pf.pf_nsid.s_lev == pkt->p_reqsid.s_lev &&
6182147Seric 				pf.pf_nsid.s_br == pkt->p_reqsid.s_br &&
6192147Seric 				pf.pf_nsid.s_seq == pkt->p_reqsid.s_seq)) {
6202147Seric 				fclose(in);
6212147Seric 				fatal(sprintf(Error,"being edited: `%s' (ge17)",
6222147Seric 					line));
6232147Seric 				}
6242147Seric 			if (!equal(pf.pf_user,user))
6252147Seric 				fprintf(stderr,"WARNING: being edited: `%s' (ge18)\n",line);
6262147Seric 		}
6272147Seric 		out = fdfopen(dup(fd),1);
6282147Seric 		fclose(in);
6292147Seric 	}
6302147Seric 	else
6312539Seric 		out = xfcreat(p,0666);
6322147Seric 	fseek(out,0L,2);
6332147Seric 	sid_ba(&pkt->p_gotsid,str1);
6342147Seric 	sid_ba(&pkt->p_reqsid,str2);
6352147Seric 	date_ba(&Timenow,line);
6362147Seric 	fprintf(out,"%s %s %s %s",str1,str2,user,line);
6372147Seric 	if (inc)
6382147Seric 		fprintf(out," -i%s",inc);
6392147Seric 	if (exc)
6402147Seric 		fprintf(out," -x%s",exc);
6412147Seric 	fprintf(out,"\n");
6422147Seric 	fclose(out);
6432147Seric 	if (pkt->p_verbose)
6442147Seric 		fprintf(pkt->p_stdout,"new delta %s\n",str2);
6452147Seric 	unlockit(auxf(pkt->p_file,'z'),getpid());
6462147Seric }
6472147Seric 
6482147Seric 
6492147Seric getser(pkt)
6502147Seric register struct packet *pkt;
6512147Seric {
6522147Seric 	register struct idel *rdp;
6532147Seric 	int n, ser, def;
6542147Seric 	char *p;
6552147Seric 	extern char *Sflags[];
6562147Seric 
6572147Seric 	def = 0;
6582147Seric 	if (pkt->p_reqsid.s_rel == 0) {
6592147Seric 		if (p = Sflags[DEFTFLAG - 'a'])
6602147Seric 			chksid(sid_ab(p, &pkt->p_reqsid), &pkt->p_reqsid);
6612147Seric 		else {
6622147Seric 			pkt->p_reqsid.s_rel = MAX;
6632147Seric 			def = 1;
6642147Seric 		}
6652147Seric 	}
6662147Seric 	ser = 0;
6672147Seric 	if (pkt->p_reqsid.s_lev == 0) {
6682147Seric 		for (n = maxser(pkt); n; n--) {
6692147Seric 			rdp = &pkt->p_idel[n];
6702147Seric 			if ((rdp->i_sid.s_br == 0 || HADT) &&
6712147Seric 				pkt->p_reqsid.s_rel >= rdp->i_sid.s_rel &&
6722147Seric 				rdp->i_sid.s_rel > pkt->p_gotsid.s_rel) {
6732147Seric 					ser = n;
6742147Seric 					pkt->p_gotsid.s_rel = rdp->i_sid.s_rel;
6752147Seric 			}
6762147Seric 		}
6772147Seric 	}
6782147Seric 	else if (pkt->p_reqsid.s_br && pkt->p_reqsid.s_seq == 0) {
6792147Seric 		for (n = maxser(pkt); n; n--) {
6802147Seric 			rdp = &pkt->p_idel[n];
6812147Seric 			if (rdp->i_sid.s_rel == pkt->p_reqsid.s_rel &&
6822147Seric 				rdp->i_sid.s_lev == pkt->p_reqsid.s_lev &&
6832147Seric 				rdp->i_sid.s_br == pkt->p_reqsid.s_br)
6842147Seric 					break;
6852147Seric 		}
6862147Seric 		ser = n;
6872147Seric 	}
6882147Seric 	else {
6892147Seric 		ser = sidtoser(&pkt->p_reqsid,pkt);
6902147Seric 	}
6912147Seric 	if (ser == 0)
6922147Seric 		fatal("nonexistent sid (ge5)");
6932147Seric 	rdp = &pkt->p_idel[ser];
6942147Seric 	move(&rdp->i_sid, &pkt->p_gotsid, sizeof(pkt->p_gotsid));
6952147Seric 	if (def || (pkt->p_reqsid.s_lev == 0 && pkt->p_reqsid.s_rel == pkt->p_gotsid.s_rel))
6962147Seric 		move(&pkt->p_gotsid, &pkt->p_reqsid, sizeof(pkt->p_gotsid));
6972147Seric 	return(ser);
6982147Seric }
6992147Seric 
7002147Seric 
7012147Seric /* Null routine to satisfy external reference from dodelt() */
7022147Seric 
7032147Seric escdodelt()
7042147Seric {
7052147Seric }
706