119942Ssam #include <sys/param.h>		/* for MAXPATHLEN */
219942Ssam #undef MAX
32147Seric # include	"../hdr/defines.h"
42147Seric # include	"../hdr/had.h"
513742Ssam # include	<sys/dir.h>
62147Seric 
7*33423Sbostic static char Sccsid[] = "@(#)get.c	4.9	02/02/88";
82147Seric 
930498Slepreau int	Debug = 0;
102147Seric struct packet gpkt;
112147Seric struct sid sid;
122147Seric unsigned	Ser;
132147Seric int	num_files;
142147Seric char	had[26];
152147Seric char	*ilist, *elist, *lfile;
1630498Slepreau long	cutoff = 0X7FFFFFFFL;	/* max positive long */
172147Seric int verbosity;
188341Ssam char	Gfile[MAXNAMLEN + 3];
1916514Smckusick char	Mod[MAXNAMLEN + 3];		/* should be as large as Gfile? */
202147Seric char	*Type;
212147Seric int	Did_id;
222147Seric 
232147Seric main(argc,argv)
242147Seric int argc;
252147Seric register char *argv[];
262147Seric {
272147Seric 	register int i;
282147Seric 	register char *p;
292147Seric 	char c;
302147Seric 	int testmore;
312147Seric 	extern int Fcnt;
322147Seric 	extern get();
332147Seric 
342147Seric 	Fflags = FTLEXIT | FTLMSG | FTLCLN;
352147Seric 	for(i=1; i<argc; i++)
362147Seric 		if(argv[i][0] == '-' && (c=argv[i][1])) {
372147Seric 			p = &argv[i][2];
382147Seric 			testmore = 0;
392147Seric 			switch (c) {
402147Seric 
412147Seric 			case 'a':
422147Seric 				if (!p[0]) {
432147Seric 					argv[i] = 0;
442147Seric 					continue;
452147Seric 				}
462147Seric 				Ser = patoi(p);
472147Seric 				break;
482147Seric 			case 'r':
492147Seric 				if (!p[0]) {
502147Seric 					argv[i] = 0;
512147Seric 					continue;
522147Seric 				}
532147Seric 				chksid(sid_ab(p,&sid),&sid);
542147Seric 				break;
552147Seric 			case 'c':
562147Seric 				if (!p[0]) {
572147Seric 					argv[i] = 0;
582147Seric 					continue;
592147Seric 				}
602147Seric 				if (date_ab(p,&cutoff))
612147Seric 					fatal("bad date/time (cm5)");
622147Seric 				break;
632147Seric 			case 'l':
642147Seric 				lfile = p;
652147Seric 				break;
662147Seric 			case 'i':
672147Seric 				if (!p[0]) {
682147Seric 					argv[i] = 0;
692147Seric 					continue;
702147Seric 				}
712147Seric 				ilist = p;
722147Seric 				break;
732147Seric 			case 'x':
742147Seric 				if (!p[0]) {
752147Seric 					argv[i] = 0;
762147Seric 					continue;
772147Seric 				}
782147Seric 				elist = p;
792147Seric 				break;
802147Seric 			case 'b':
812147Seric 			case 'g':
822147Seric 			case 'e':
832147Seric 			case 'p':
842147Seric 			case 'k':
852147Seric 			case 'm':
862147Seric 			case 'n':
872147Seric 			case 's':
882147Seric 			case 't':
892147Seric 				testmore++;
902147Seric 				break;
912147Seric 			default:
922147Seric 				fatal("unknown key letter (cm1)");
932147Seric 			}
942147Seric 
952147Seric 			if (testmore) {
962147Seric 				testmore = 0;
97*33423Sbostic 				if (*p) {
98*33423Sbostic 					sprintf(Error,"value after %c arg (cm8)",c);
99*33423Sbostic 					fatal(Error);
100*33423Sbostic 				}
1012147Seric 			}
1022147Seric 			if (had[c - 'a']++)
1032147Seric 				fatal("key letter twice (cm2)");
1042147Seric 			argv[i] = 0;
1052147Seric 		}
1062147Seric 		else num_files++;
1072147Seric 
1082147Seric 	if(num_files == 0)
1092147Seric 		fatal("missing file arg (cm3)");
1102147Seric 	if (HADE && HADM)
1112147Seric 		fatal("e not allowed with m (ge3)");
1122147Seric 	if (HADE || HADI || HADX)
1132147Seric 		HADK = 1;
1142147Seric 	if (!HADS)
1152147Seric 		verbosity = -1;
1162147Seric 	setsig();
11730498Slepreau 	Fflags &= ~FTLEXIT;
11830498Slepreau 	Fflags |= FTLJMP;
1192147Seric 	for (i=1; i<argc; i++)
1202147Seric 		if (p=argv[i])
1212147Seric 			do_file(p,get);
1222147Seric 	exit(Fcnt ? 1 : 0);
1232147Seric }
1242147Seric 
1252147Seric 
1262147Seric get(file)
1272147Seric {
1282147Seric 	register char *p;
1292147Seric 	register unsigned ser;
1302147Seric 	extern char had_dir, had_standinp;
1312147Seric 	extern char *Sflags[];
1322147Seric 	struct stats stats;
1332147Seric 	char	str[32];
1342147Seric 
1352147Seric 	if (setjmp(Fjmp))
1362147Seric 		return;
1372147Seric 	sinit(&gpkt,file,1);
1382147Seric 	gpkt.p_ixuser = (HADI | HADX);
1392147Seric 	gpkt.p_reqsid.s_rel = sid.s_rel;
1402147Seric 	gpkt.p_reqsid.s_lev = sid.s_lev;
1412147Seric 	gpkt.p_reqsid.s_br = sid.s_br;
1422147Seric 	gpkt.p_reqsid.s_seq = sid.s_seq;
1432147Seric 	gpkt.p_verbose = verbosity;
1442147Seric 	gpkt.p_stdout = (HADP ? stderr : stdout);
1452147Seric 	gpkt.p_cutoff = cutoff;
1462147Seric 	gpkt.p_lfile = lfile;
1472147Seric 	copy(auxf(gpkt.p_file,'g'),Gfile);
1482147Seric 
1492147Seric 	if (gpkt.p_verbose && (num_files > 1 || had_dir || had_standinp))
1502147Seric 		fprintf(gpkt.p_stdout,"\n%s:\n",gpkt.p_file);
1512147Seric 	if (dodelt(&gpkt,&stats,0,0) == 0)
1522147Seric 		fmterr(&gpkt);
1532147Seric 	finduser(&gpkt);
1542147Seric 	doflags(&gpkt);
1552147Seric 	if (!HADA)
1562147Seric 		ser = getser(&gpkt);
1572147Seric 	else {
1582147Seric 		if ((ser = Ser) > maxser(&gpkt))
1592147Seric 			fatal("serial number too large (ge19)");
16019942Ssam 		bcopy(&gpkt.p_idel[ser].i_sid, &gpkt.p_gotsid, sizeof(sid));
1612147Seric 		if (HADR && sid.s_rel != gpkt.p_gotsid.s_rel) {
16219942Ssam 			bzero(&gpkt.p_reqsid, sizeof(gpkt.p_reqsid));
1632147Seric 			gpkt.p_reqsid.s_rel = sid.s_rel;
1642147Seric 		}
1652147Seric 		else
16619942Ssam 			bcopy(&gpkt.p_gotsid, &gpkt.p_reqsid, sizeof(sid));
1672147Seric 	}
1682147Seric 	doie(&gpkt,ilist,elist,0);
1692147Seric 	setup(&gpkt,ser);
1702147Seric 	if (!(Type = Sflags[TYPEFLAG - 'a']))
1712147Seric 		Type = Null;
172*33423Sbostic 	if (!(HADP || HADG) && writable(Gfile)) {
173*33423Sbostic 		sprintf(Error,"writable `%s' exists (ge4)",Gfile);
174*33423Sbostic 		fatal(Error);
175*33423Sbostic 	}
1762147Seric 	if (gpkt.p_verbose) {
1772147Seric 		sid_ba(&gpkt.p_gotsid,str);
1782147Seric 		fprintf(gpkt.p_stdout,"%s\n",str);
1792147Seric 	}
1802147Seric 	if (HADE) {
1812147Seric 		if (!HADR)
18219942Ssam 			bcopy(&gpkt.p_gotsid,&gpkt.p_reqsid,
1832147Seric 			     sizeof(gpkt.p_reqsid));
1842147Seric 		newsid(&gpkt,Sflags[BRCHFLAG - 'a'] && HADB);
1852147Seric 		permiss(&gpkt);
1862147Seric 		wrtpfile(&gpkt,ilist,elist);
1872147Seric 	}
1882147Seric 	setuid(getuid());
1892147Seric 	if (HADL)
1902147Seric 		gen_lfile(&gpkt);
1912147Seric 	if (HADG) {
1922147Seric 		fclose(gpkt.p_iop);
1932147Seric 		xfreeall();
1942147Seric 		return;
1952147Seric 	}
1962147Seric 	flushto(&gpkt,EUSERTXT,1);
1972147Seric 	idsetup(&gpkt);
1982147Seric 	gpkt.p_chkeof = 1;
1992147Seric 	Did_id = 0;
2002147Seric 	while(readmod(&gpkt)) {
2012147Seric 		if (gpkt.p_gout == 0) {
2022147Seric 			if (HADP)
2032147Seric 				gpkt.p_gout = stdout;
2042147Seric 			else
2052539Seric 				gpkt.p_gout = xfcreat(Gfile,HADK ? 0666 : 0444);
2062147Seric 		}
20716514Smckusick 		if (HADN)
20816514Smckusick 			fprintf(gpkt.p_gout,"%s\t",Mod);
20916514Smckusick 		if (HADM) {
21016514Smckusick 			sid_ba(&gpkt.p_inssid,str);
21116514Smckusick 			fprintf(gpkt.p_gout,"%s\t",str);
21216514Smckusick 		}
21316514Smckusick 		p = gpkt.p_line;
21416514Smckusick 		if (!HADK && any('%',p))
21516514Smckusick 			p = idsubst(&gpkt,p);
2162147Seric 		fputs(p,gpkt.p_gout);
2172147Seric 	}
2182147Seric 	fflush(gpkt.p_gout);
2192147Seric 	if (gpkt.p_gout && gpkt.p_gout != stdout)
2202147Seric 		fclose(gpkt.p_gout);
2212147Seric 	if (gpkt.p_verbose)
2222147Seric 		fprintf(gpkt.p_stdout,"%u lines\n",gpkt.p_glnno);
2232147Seric 	if (!Did_id && !HADK)
2242147Seric 		if (Sflags[IDFLAG - 'a'])
2252147Seric 			fatal("no id keywords (cm6)");
2262147Seric 		else if (gpkt.p_verbose)
2272147Seric 			fprintf(stderr,"No id keywords (cm7)\n");
2282147Seric 	xfreeall();
2292147Seric }
2302147Seric 
2312448Seric writable(fn)
2322448Seric char *fn;
2332448Seric {
2342448Seric 	struct stat s;
2352147Seric 
2362448Seric 	return (stat(fn, &s) >= 0 && (s.st_mode & 0222) != 0);
2372448Seric }
2382448Seric 
2392448Seric 
2402147Seric newsid(pkt,branch)
2412147Seric register struct packet *pkt;
2422147Seric int branch;
2432147Seric {
2442147Seric 	int chkbr;
2452147Seric 
2462147Seric 	chkbr = 0;
2472147Seric 	if (pkt->p_reqsid.s_br == 0) {
24830498Slepreau 		pkt->p_reqsid.s_lev += 1;
2492147Seric 		if (sidtoser(&pkt->p_reqsid,pkt) ||
2502147Seric 			pkt->p_maxr > pkt->p_reqsid.s_rel || branch) {
2512147Seric 				pkt->p_reqsid.s_rel = pkt->p_gotsid.s_rel;
2522147Seric 				pkt->p_reqsid.s_lev = pkt->p_gotsid.s_lev;
2532147Seric 				pkt->p_reqsid.s_br = pkt->p_gotsid.s_br + 1;
2542147Seric 				pkt->p_reqsid.s_seq = 1;
2552147Seric 				chkbr++;
2562147Seric 		}
2572147Seric 	}
2582147Seric 	else if (pkt->p_reqsid.s_seq == 0 && !branch)
2592147Seric 		pkt->p_reqsid.s_seq = pkt->p_gotsid.s_seq + 1;
2602147Seric 	else {
26130498Slepreau 		pkt->p_reqsid.s_seq += 1;
2622147Seric 		if (branch || sidtoser(&pkt->p_reqsid,pkt)) {
26330498Slepreau 			pkt->p_reqsid.s_br += 1;
2642147Seric 			pkt->p_reqsid.s_seq = 1;
2652147Seric 			chkbr++;
2662147Seric 		}
2672147Seric 	}
2682147Seric 	if (chkbr)
2692147Seric 		while (sidtoser(&pkt->p_reqsid,pkt))
27030498Slepreau 			pkt->p_reqsid.s_br += 1;
2712147Seric 	if (sidtoser(&pkt->p_reqsid,pkt))
2722147Seric 		fatal("internal error in newsid()");
2732147Seric }
2742147Seric 
2752147Seric 
2762147Seric enter(pkt,ch,n,sidp)
2772147Seric struct packet *pkt;
2782147Seric char ch;
2792147Seric int n;
2802147Seric struct sid *sidp;
2812147Seric {
2822147Seric 	char str[32];
2832147Seric 	register struct apply *ap;
2842147Seric 
2852147Seric 	sid_ba(sidp,str);
2862147Seric 	if (pkt->p_verbose)
2872147Seric 		fprintf(pkt->p_stdout,"%s\n",str);
2882147Seric 	ap = &pkt->p_apply[n];
2892147Seric 	switch(ap->a_code) {
2902147Seric 
2912147Seric 	case EMPTY:
2922147Seric 		if (ch == INCLUDE)
2932147Seric 			condset(ap,APPLY,INCLUSER);
2942147Seric 		else
2952147Seric 			condset(ap,NOAPPLY,EXCLUSER);
2962147Seric 		break;
2972147Seric 	case APPLY:
2982147Seric 		sid_ba(sidp,str);
299*33423Sbostic 		sprintf(Error,"%s already included (ge9)",str);
300*33423Sbostic 		fatal(Error);
3012147Seric 		break;
3022147Seric 	case NOAPPLY:
3032147Seric 		sid_ba(sidp,str);
304*33423Sbostic 		sprintf(Error,"%s already excluded (ge10)",str);
305*33423Sbostic 		fatal(Error);
3062147Seric 		break;
3072147Seric 	default:
3082147Seric 		fatal("internal error in get/enter() (ge11)");
3092147Seric 		break;
3102147Seric 	}
3112147Seric }
3122147Seric 
3132147Seric 
3142147Seric gen_lfile(pkt)
3152147Seric register struct packet *pkt;
3162147Seric {
3172147Seric 	int n;
3182147Seric 	int reason;
3192147Seric 	char str[32];
3202147Seric 	char line[BUFSIZ];
3212147Seric 	struct deltab dt;
3222147Seric 	FILE *in;
3232147Seric 	FILE *out;
3242147Seric 
3252147Seric 	in = xfopen(pkt->p_file,0);
3262147Seric 	if (*pkt->p_lfile)
3272147Seric 		out = stdout;
3282147Seric 	else
3292147Seric 		out = xfcreat(auxf(pkt->p_file,'l'),0444);
3302147Seric 	fgets(line,sizeof(line),in);
3312147Seric 	while (fgets(line,sizeof(line),in) != NULL && line[0] == CTLCHAR && line[1] == STATS) {
3322147Seric 		fgets(line,sizeof(line),in);
3332147Seric 		del_ab(line,&dt);
3342147Seric 		if (dt.d_type == 'D') {
3352147Seric 			reason = pkt->p_apply[dt.d_serial].a_reason;
3362147Seric 			if (pkt->p_apply[dt.d_serial].a_code == APPLY) {
3372147Seric 				putc(' ',out);
3382147Seric 				putc(' ',out);
3392147Seric 			}
3402147Seric 			else {
3412147Seric 				putc('*',out);
3422147Seric 				if (reason & IGNR)
3432147Seric 					putc(' ',out);
3442147Seric 				else
3452147Seric 					putc('*',out);
3462147Seric 			}
3472147Seric 			switch (reason & (INCL | EXCL | CUTOFF)) {
3482147Seric 
3492147Seric 			case INCL:
3502147Seric 				putc('I',out);
3512147Seric 				break;
3522147Seric 			case EXCL:
3532147Seric 				putc('X',out);
3542147Seric 				break;
3552147Seric 			case CUTOFF:
3562147Seric 				putc('C',out);
3572147Seric 				break;
3582147Seric 			default:
3592147Seric 				putc(' ',out);
3602147Seric 				break;
3612147Seric 			}
3622147Seric 			putc(' ',out);
3632147Seric 			sid_ba(&dt.d_sid,str);
3642147Seric 			fprintf(out,"%s\t",str);
3652147Seric 			date_ba(&dt.d_datetime,str);
3662147Seric 			fprintf(out,"%s %s\n",str,dt.d_pgmr);
3672147Seric 		}
3682147Seric 		while ((n = fgets(line,sizeof(line),in)) != NULL)
3692147Seric 			if (line[0] != CTLCHAR)
3702147Seric 				break;
3712147Seric 			else {
3722147Seric 				switch (line[1]) {
3732147Seric 
3742147Seric 				case EDELTAB:
3752147Seric 					break;
3762147Seric 				default:
3772147Seric 					continue;
3782147Seric 				case MRNUM:
3792147Seric 				case COMMENTS:
3802147Seric 					if (dt.d_type == 'D')
3812147Seric 						fprintf(out,"\t%s",&line[3]);
3822147Seric 					continue;
3832147Seric 				}
3842147Seric 				break;
3852147Seric 			}
3862147Seric 		if (n == NULL || line[0] != CTLCHAR)
3872147Seric 			break;
3882147Seric 		putc('\n',out);
3892147Seric 	}
3902147Seric 	fclose(in);
3912147Seric 	if (out != stdout)
3922147Seric 		fclose(out);
3932147Seric }
3942147Seric 
3952147Seric char	Curdate[18];
3962147Seric char	*Curtime;
3972147Seric char	Gdate[9];
3982147Seric char	Chgdate[18];
3992147Seric char	*Chgtime;
4002147Seric char	Gchgdate[9];
40119942Ssam char	Qchgdate[30];
4022147Seric char	Sid[32];
40319942Ssam char	Olddir[MAXPATHLEN+1];
40419942Ssam char	Pname[MAXPATHLEN+1];
40519942Ssam char	Dir[MAXPATHLEN+1];
4062147Seric 
4072147Seric idsetup(pkt)
4082147Seric register struct packet *pkt;
4092147Seric {
4102147Seric 	extern long Timenow;
4112147Seric 	register int n;
4122147Seric 	register char *p;
4132147Seric 
4142147Seric 	date_ba(&Timenow,Curdate);
4152147Seric 	Curtime = &Curdate[9];
4162147Seric 	Curdate[8] = 0;
4172147Seric 	copy(pkt->p_file,Dir);
4182147Seric 	dname(Dir);
41919942Ssam 	if(getwd(Olddir) == 0)
42019942Ssam 		fatal("getwd failed (ge20)");
4212147Seric 	if(chdir(Dir) != 0)
4222147Seric 		fatal("cannot change directory (ge22)");
42319942Ssam 	if(getwd(Pname) == 0)
42419942Ssam 		fatal("getwd failed (ge21)");
4252147Seric 	if(chdir(Olddir) != 0)
4262147Seric 		fatal("cannot change directory (ge23)");
4272147Seric 	makgdate(Curdate,Gdate);
4282147Seric 	for (n = maxser(pkt); n; n--)
4292147Seric 		if (pkt->p_apply[n].a_code == APPLY)
4302147Seric 			break;
4312147Seric 	if (n)
4322147Seric 		date_ba(&pkt->p_idel[n].i_datetime,Chgdate);
4332147Seric 	Chgtime = &Chgdate[9];
4342147Seric 	Chgdate[8] = 0;
4352147Seric 	makgdate(Chgdate,Gchgdate);
43619942Ssam 	makqdate(Gchgdate,Qchgdate);
4372147Seric 	sid_ba(&pkt->p_gotsid,Sid);
4382147Seric 	if (p = Sflags[MODFLAG - 'a'])
4392147Seric 		copy(p,Mod);
4402147Seric 	else
4412147Seric 		copy(Gfile,Mod);
4422147Seric }
4432147Seric 
4442147Seric 
4452147Seric makgdate(old,new)
4462147Seric register char *old, *new;
4472147Seric {
4482147Seric 	if ((*new = old[3]) != '0')
4492147Seric 		new++;
4502147Seric 	*new++ = old[4];
4512147Seric 	*new++ = '/';
4522147Seric 	if ((*new = old[6]) != '0')
4532147Seric 		new++;
4542147Seric 	*new++ = old[7];
4552147Seric 	*new++ = '/';
4562147Seric 	*new++ = old[0];
4572147Seric 	*new++ = old[1];
4582147Seric 	*new = 0;
4592147Seric }
4602147Seric 
46119942Ssam makqdate(old,new)
46219942Ssam register char *old, *new;
46319942Ssam {
46419942Ssam 	static	char *months[12] =
46519942Ssam 	    { "January", "February", "March", "April", "May", "June", "July",
46619942Ssam 	      "August", "September", "October", "November", "December" };
4672147Seric 
46819942Ssam 	strcpy(new, months[atoi(old)-1]);
46919942Ssam 	while (*new != '\0')
47019942Ssam 		new++;
47119942Ssam 	while (*old++ != '/')
47219942Ssam 		;
47319942Ssam 	*new++ = ' ';
47419942Ssam 	*new++ = *old++;
47519942Ssam 	if (*old != '/')
47619942Ssam 		*new++ = *old++;
47719942Ssam 	*new++ = ',';
47819942Ssam 	*new++ = ' ';
47919942Ssam 	*new++ = '1'; *new++ = '9';	/* works for this century at least */
48019942Ssam 	*new++ = *++old;
48119942Ssam 	*new++ = *++old;
48219942Ssam 	*new = '\0';
48319942Ssam }
48419942Ssam 
48530498Slepreau static char Zkeywd[5] = "@(#)";
4862147Seric 
48719942Ssam 
4882147Seric idsubst(pkt,line)
4892147Seric register struct packet *pkt;
4902147Seric char line[];
4912147Seric {
4922147Seric 	static char tline[BUFSIZ];
4932147Seric 	static char str[32];
4942147Seric 	register char *lp, *tp;
4952147Seric 	extern char *Type;
4962147Seric 	extern char *Sflags[];
4972147Seric 
4982147Seric 	tp = tline;
4992147Seric 	for(lp=line; *lp != 0; lp++) {
5002147Seric 		if(lp[0] == '%' && lp[1] != 0 && lp[2] == '%') {
5012147Seric 			switch(*++lp) {
5022147Seric 
5032147Seric 			case 'M':
5042147Seric 				tp = trans(tp,Mod);
5052147Seric 				break;
5062147Seric 			case 'R':
5072147Seric 				sprintf(str,"%u",pkt->p_gotsid.s_rel);
5082147Seric 				tp = trans(tp,str);
5092147Seric 				break;
5102147Seric 			case 'L':
5112147Seric 				sprintf(str,"%u",pkt->p_gotsid.s_lev);
5122147Seric 				tp = trans(tp,str);
5132147Seric 				break;
5142147Seric 			case 'B':
5152147Seric 				sprintf(str,"%u",pkt->p_gotsid.s_br);
5162147Seric 				tp = trans(tp,str);
5172147Seric 				break;
5182147Seric 			case 'S':
5192147Seric 				sprintf(str,"%u",pkt->p_gotsid.s_seq);
5202147Seric 				tp = trans(tp,str);
5212147Seric 				break;
5222147Seric 			case 'D':
5232147Seric 				tp = trans(tp,Curdate);
5242147Seric 				break;
5252147Seric 			case 'H':
5262147Seric 				tp = trans(tp,Gdate);
5272147Seric 				break;
5282147Seric 			case 'T':
5292147Seric 				tp = trans(tp,Curtime);
5302147Seric 				break;
5312147Seric 			case 'E':
5322147Seric 				tp = trans(tp,Chgdate);
5332147Seric 				break;
5342147Seric 			case 'G':
5352147Seric 				tp = trans(tp,Gchgdate);
5362147Seric 				break;
53719942Ssam 			case 'Q':
53819942Ssam 				tp = trans(tp,Qchgdate);
53919942Ssam 				break;
5402147Seric 			case 'U':
5412147Seric 				tp = trans(tp,Chgtime);
5422147Seric 				break;
5432147Seric 			case 'Z':
5442147Seric 				tp = trans(tp,Zkeywd);
5452147Seric 				break;
5462147Seric 			case 'Y':
5472147Seric 				tp = trans(tp,Type);
5482147Seric 				break;
5492147Seric 			case 'W':
5502147Seric 				tp = trans(tp,Zkeywd);
5512147Seric 				tp = trans(tp,Mod);
5522147Seric 				*tp++ = '\t';
5532147Seric 			case 'I':
5542147Seric 				tp = trans(tp,Sid);
5552147Seric 				break;
5562147Seric 			case 'P':
5572147Seric 				tp = trans(tp,Pname);
5582147Seric 				*tp++ = '/';
5592147Seric 				tp = trans(tp,(sname(pkt->p_file)));
5602147Seric 				break;
5612147Seric 			case 'F':
5622147Seric 				tp = trans(tp,pkt->p_file);
5632147Seric 				break;
5642147Seric 			case 'C':
5652147Seric 				sprintf(str,"%u",pkt->p_glnno);
5662147Seric 				tp = trans(tp,str);
5672147Seric 				break;
5682147Seric 			case 'A':
5692147Seric 				tp = trans(tp,Zkeywd);
5702147Seric 				tp = trans(tp,Type);
5712147Seric 				*tp++ = ' ';
5722147Seric 				tp = trans(tp,Mod);
5732147Seric 				*tp++ = ' ';
5742147Seric 				tp = trans(tp,Sid);
5752147Seric 				tp = trans(tp,Zkeywd);
5762147Seric 				break;
5772147Seric 			default:
5782147Seric 				*tp++ = '%';
5792147Seric 				*tp++ = *lp;
5802147Seric 				continue;
5812147Seric 			}
5822147Seric 			lp++;
5832147Seric 		}
5842147Seric 		else
5852147Seric 			*tp++ = *lp;
5862147Seric 	}
5872147Seric 
5882147Seric 	*tp = 0;
5892147Seric 	return(tline);
5902147Seric }
5912147Seric 
5922147Seric 
5932147Seric trans(tp,str)
5942147Seric register char *tp, *str;
5952147Seric {
5962147Seric 	Did_id = 1;
5972147Seric 	while(*tp++ = *str++)
5982147Seric 		;
5992147Seric 	return(tp-1);
6002147Seric }
6012147Seric 
6022147Seric 
6032147Seric clean_up(n)
6042147Seric {
6052147Seric 	if (gpkt.p_file[0])
6062147Seric 		unlockit(auxf(gpkt.p_file,'z'),getpid());
6072147Seric 	if (gpkt.p_iop)
6082147Seric 		fclose(gpkt.p_iop);
6092147Seric 	xfreeall();
6102147Seric }
6112147Seric 
6122147Seric 
6132147Seric wrtpfile(pkt,inc,exc)
6142147Seric register struct packet *pkt;
6152147Seric char *inc, *exc;
6162147Seric {
6172147Seric 	char line[64], str1[32], str2[32];
6182147Seric 	char *user;
6192147Seric 	FILE *in, *out;
6202147Seric 	struct pfile pf;
6212147Seric 	register char *p;
6222147Seric 	int fd;
6232147Seric 	int i;
6242147Seric 	extern long Timenow;
6252147Seric 
6262147Seric 	user = logname();
6272147Seric 	if (lockit(auxf(pkt->p_file,'z'),2,getpid()))
6282147Seric 		fatal("cannot create lock file (cm4)");
6292147Seric 	if (exists(p = auxf(pkt->p_file,'p'))) {
6302147Seric 		fd = xopen(p,2);
63119942Ssam 		in = fdopen(fd,"r");
6322147Seric 		while (fgets(line,sizeof(line),in) != NULL) {
6332147Seric 			p = line;
6342147Seric 			p[length(p) - 1] = 0;
6352147Seric 			pf_ab(p,&pf,0);
6362147Seric 			if ((pf.pf_gsid.s_rel == pkt->p_gotsid.s_rel &&
6372147Seric 				pf.pf_gsid.s_lev == pkt->p_gotsid.s_lev &&
6382147Seric 				pf.pf_gsid.s_br == pkt->p_gotsid.s_br &&
6392147Seric 				pf.pf_gsid.s_seq == pkt->p_gotsid.s_seq) ||
6402147Seric 				(pf.pf_nsid.s_rel == pkt->p_reqsid.s_rel &&
6412147Seric 				pf.pf_nsid.s_lev == pkt->p_reqsid.s_lev &&
6422147Seric 				pf.pf_nsid.s_br == pkt->p_reqsid.s_br &&
6432147Seric 				pf.pf_nsid.s_seq == pkt->p_reqsid.s_seq)) {
6442147Seric 				fclose(in);
645*33423Sbostic 				sprintf(Error,"being edited: `%s' (ge17)",line);
646*33423Sbostic 				fatal(Error);
6472147Seric 				}
6482147Seric 			if (!equal(pf.pf_user,user))
6492147Seric 				fprintf(stderr,"WARNING: being edited: `%s' (ge18)\n",line);
6502147Seric 		}
65119942Ssam 		out = fdopen(dup(fd),"w");
6522147Seric 		fclose(in);
6532147Seric 	}
6542147Seric 	else
6552539Seric 		out = xfcreat(p,0666);
6562147Seric 	fseek(out,0L,2);
6572147Seric 	sid_ba(&pkt->p_gotsid,str1);
6582147Seric 	sid_ba(&pkt->p_reqsid,str2);
6592147Seric 	date_ba(&Timenow,line);
6602147Seric 	fprintf(out,"%s %s %s %s",str1,str2,user,line);
6612147Seric 	if (inc)
6622147Seric 		fprintf(out," -i%s",inc);
6632147Seric 	if (exc)
6642147Seric 		fprintf(out," -x%s",exc);
6652147Seric 	fprintf(out,"\n");
6662147Seric 	fclose(out);
6672147Seric 	if (pkt->p_verbose)
6682147Seric 		fprintf(pkt->p_stdout,"new delta %s\n",str2);
6692147Seric 	unlockit(auxf(pkt->p_file,'z'),getpid());
6702147Seric }
6712147Seric 
6722147Seric 
6732147Seric getser(pkt)
6742147Seric register struct packet *pkt;
6752147Seric {
6762147Seric 	register struct idel *rdp;
6772147Seric 	int n, ser, def;
6782147Seric 	char *p;
6792147Seric 	extern char *Sflags[];
6802147Seric 
6812147Seric 	def = 0;
6822147Seric 	if (pkt->p_reqsid.s_rel == 0) {
6832147Seric 		if (p = Sflags[DEFTFLAG - 'a'])
6842147Seric 			chksid(sid_ab(p, &pkt->p_reqsid), &pkt->p_reqsid);
6852147Seric 		else {
6862147Seric 			pkt->p_reqsid.s_rel = MAX;
6872147Seric 			def = 1;
6882147Seric 		}
6892147Seric 	}
6902147Seric 	ser = 0;
6912147Seric 	if (pkt->p_reqsid.s_lev == 0) {
6922147Seric 		for (n = maxser(pkt); n; n--) {
6932147Seric 			rdp = &pkt->p_idel[n];
6942147Seric 			if ((rdp->i_sid.s_br == 0 || HADT) &&
6952147Seric 				pkt->p_reqsid.s_rel >= rdp->i_sid.s_rel &&
6962147Seric 				rdp->i_sid.s_rel > pkt->p_gotsid.s_rel) {
6972147Seric 					ser = n;
6982147Seric 					pkt->p_gotsid.s_rel = rdp->i_sid.s_rel;
6992147Seric 			}
7002147Seric 		}
7012147Seric 	}
7022147Seric 	else if (pkt->p_reqsid.s_br && pkt->p_reqsid.s_seq == 0) {
7032147Seric 		for (n = maxser(pkt); n; n--) {
7042147Seric 			rdp = &pkt->p_idel[n];
7052147Seric 			if (rdp->i_sid.s_rel == pkt->p_reqsid.s_rel &&
7062147Seric 				rdp->i_sid.s_lev == pkt->p_reqsid.s_lev &&
7072147Seric 				rdp->i_sid.s_br == pkt->p_reqsid.s_br)
7082147Seric 					break;
7092147Seric 		}
7102147Seric 		ser = n;
7112147Seric 	}
7122147Seric 	else {
7132147Seric 		ser = sidtoser(&pkt->p_reqsid,pkt);
7142147Seric 	}
7152147Seric 	if (ser == 0)
7162147Seric 		fatal("nonexistent sid (ge5)");
7172147Seric 	rdp = &pkt->p_idel[ser];
71819942Ssam 	bcopy(&rdp->i_sid, &pkt->p_gotsid, sizeof(pkt->p_gotsid));
7192147Seric 	if (def || (pkt->p_reqsid.s_lev == 0 && pkt->p_reqsid.s_rel == pkt->p_gotsid.s_rel))
72019942Ssam 		bcopy(&pkt->p_gotsid, &pkt->p_reqsid, sizeof(pkt->p_gotsid));
7212147Seric 	return(ser);
7222147Seric }
7232147Seric 
7242147Seric 
7252147Seric /* Null routine to satisfy external reference from dodelt() */
7262147Seric 
7272147Seric escdodelt()
7282147Seric {
7292147Seric }
730