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