xref: /csrg-svn/usr.bin/sccs/sccs.c (revision 6785)
1148Seric # include <stdio.h>
26784Smckusick # include <sys/param.h>
3148Seric # include <sys/stat.h>
46784Smckusick # include <dir.h>
51433Seric # include <errno.h>
61433Seric # include <signal.h>
7148Seric # include <sysexits.h>
82140Seric # include <pwd.h>
9148Seric 
10828Seric /*
11828Seric **  SCCS.C -- human-oriented front end to the SCCS system.
12828Seric **
13828Seric **	Without trying to add any functionality to speak of, this
14828Seric **	program tries to make SCCS a little more accessible to human
15828Seric **	types.  The main thing it does is automatically put the
16828Seric **	string "SCCS/s." on the front of names.  Also, it has a
17828Seric **	couple of things that are designed to shorten frequent
18828Seric **	combinations, e.g., "delget" which expands to a "delta"
19828Seric **	and a "get".
20828Seric **
21828Seric **	This program can also function as a setuid front end.
22828Seric **	To do this, you should copy the source, renaming it to
23828Seric **	whatever you want, e.g., "syssccs".  Change any defaults
24828Seric **	in the program (e.g., syssccs might default -d to
25828Seric **	"/usr/src/sys").  Then recompile and put the result
26828Seric **	as setuid to whomever you want.  In this mode, sccs
27828Seric **	knows to not run setuid for certain programs in order
28828Seric **	to preserve security, and so forth.
29828Seric **
30828Seric **	Usage:
31828Seric **		sccs [flags] command [args]
32828Seric **
33828Seric **	Flags:
34828Seric **		-d<dir>		<dir> represents a directory to search
35828Seric **				out of.  It should be a full pathname
36828Seric **				for general usage.  E.g., if <dir> is
37828Seric **				"/usr/src/sys", then a reference to the
38828Seric **				file "dev/bio.c" becomes a reference to
39828Seric **				"/usr/src/sys/dev/bio.c".
40828Seric **		-p<path>	prepends <path> to the final component
41828Seric **				of the pathname.  By default, this is
42828Seric **				"SCCS".  For example, in the -d example
43828Seric **				above, the path then gets modified to
44828Seric **				"/usr/src/sys/dev/SCCS/s.bio.c".  In
45828Seric **				more common usage (without the -d flag),
46828Seric **				"prog.c" would get modified to
47828Seric **				"SCCS/s.prog.c".  In both cases, the
48828Seric **				"s." gets automatically prepended.
49828Seric **		-r		run as the real user.
50828Seric **
51828Seric **	Commands:
52828Seric **		admin,
53828Seric **		get,
54828Seric **		delta,
55828Seric **		rmdel,
56828Seric **		chghist,
57828Seric **		etc.		Straight out of SCCS; only difference
58828Seric **				is that pathnames get modified as
59828Seric **				described above.
60828Seric **		edit		Macro for "get -e".
61828Seric **		unedit		Removes a file being edited, knowing
62828Seric **				about p-files, etc.
63828Seric **		delget		Macro for "delta" followed by "get".
64828Seric **		deledit		Macro for "delta" followed by "get -e".
65828Seric **		info		Tell what files being edited.
66828Seric **		clean		Remove all files that can be
67828Seric **				regenerated from SCCS files.
681205Seric **		check		Like info, but return exit status, for
69828Seric **				use in makefiles.
70828Seric **		fix		Remove a top delta & reedit, but save
71828Seric **				the previous changes in that delta.
72828Seric **
73828Seric **	Compilation Flags:
74828Seric **		UIDUSER -- determine who the user is by looking at the
75828Seric **			uid rather than the login name -- for machines
76828Seric **			where SCCS gets the user in this way.
771270Seric **		SCCSDIR -- if defined, forces the -d flag to take on
781205Seric **			this value.  This is so that the setuid
791205Seric **			aspects of this program cannot be abused.
801270Seric **			This flag also disables the -p flag.
811270Seric **		SCCSPATH -- the default for the -p flag.
821437Seric **		MYNAME -- the title this program should print when it
831437Seric **			gives error messages.
84828Seric **
85828Seric **	Compilation Instructions:
86828Seric **		cc -O -n -s sccs.c
871437Seric **		The flags listed above can be -D defined to simplify
881437Seric **			recompilation for variant versions.
89828Seric **
90828Seric **	Author:
91828Seric **		Eric Allman, UCB/INGRES
921270Seric **		Copyright 1980 Regents of the University of California
93828Seric */
94155Seric 
95*6785Smckusick static char SccsId[] = "@(#)sccs.c	1.65 05/11/82";
961432Seric 
971270Seric /*******************  Configuration Information  ********************/
981270Seric 
991437Seric # ifndef SCCSPATH
1001432Seric # define SCCSPATH	"SCCS"	/* pathname in which to find s-files */
1011437Seric # endif NOT SCCSPATH
102828Seric 
1031437Seric # ifndef MYNAME
1041437Seric # define MYNAME		"sccs"	/* name used for printing errors */
1051437Seric # endif NOT MYNAME
1061270Seric 
1071432Seric # ifndef PROGPATH
1086784Smckusick # define PROGPATH(name)	"/usr/local/name"	/* place to find binaries */
1091432Seric # endif PROGPATH
1101432Seric 
1111270Seric /****************  End of Configuration Information  ****************/
1121432Seric 
113157Seric typedef char	bool;
114200Seric # define TRUE	1
115200Seric # define FALSE	0
116157Seric 
1171438Seric # define bitset(bit, word)	((bool) ((bit) & (word)))
1181438Seric 
119148Seric struct sccsprog
120148Seric {
121148Seric 	char	*sccsname;	/* name of SCCS routine */
122200Seric 	short	sccsoper;	/* opcode, see below */
123200Seric 	short	sccsflags;	/* flags, see below */
124148Seric 	char	*sccspath;	/* pathname of binary implementing */
125148Seric };
126148Seric 
127200Seric /* values for sccsoper */
128200Seric # define PROG		0	/* call a program */
129201Seric # define CMACRO		1	/* command substitution macro */
130226Seric # define FIX		2	/* fix a delta */
131261Seric # define CLEAN		3	/* clean out recreatable files */
132396Seric # define UNEDIT		4	/* unedit a file */
1331431Seric # define SHELL		5	/* call a shell file (like PROG) */
1341433Seric # define DIFFS		6	/* diff between sccs & file out */
1351871Seric # define DODIFF		7	/* internal call to diff program */
136*6785Smckusick # define CREATE		8	/* create new files */
137200Seric 
138157Seric /* bits for sccsflags */
139200Seric # define NO_SDOT	0001	/* no s. on front of args */
140200Seric # define REALUSER	0002	/* protected (e.g., admin) */
141148Seric 
142819Seric /* modes for the "clean", "info", "check" ops */
143819Seric # define CLEANC		0	/* clean command */
144819Seric # define INFOC		1	/* info command */
145819Seric # define CHECKC		2	/* check command */
1461730Seric # define TELLC		3	/* give list of files being edited */
147819Seric 
1481432Seric /*
1491432Seric **  Description of commands known to this program.
1501432Seric **	First argument puts the command into a class.  Second arg is
1511432Seric **	info regarding treatment of this command.  Third arg is a
1521432Seric **	list of flags this command accepts from macros, etc.  Fourth
1531432Seric **	arg is the pathname of the implementing program, or the
1541432Seric **	macro definition, or the arg to a sub-algorithm.
1551432Seric */
156202Seric 
157148Seric struct sccsprog SccsProg[] =
158148Seric {
1591864Seric 	"admin",	PROG,	REALUSER,		PROGPATH(admin),
1601864Seric 	"chghist",	PROG,	0,			PROGPATH(rmdel),
1611864Seric 	"comb",		PROG,	0,			PROGPATH(comb),
1621864Seric 	"delta",	PROG,	0,			PROGPATH(delta),
1631864Seric 	"get",		PROG,	0,			PROGPATH(get),
1641864Seric 	"help",		PROG,	NO_SDOT,		PROGPATH(help),
1652136Seric 	"prs",		PROG,	0,			PROGPATH(prs),
1661864Seric 	"prt",		PROG,	0,			PROGPATH(prt),
1671864Seric 	"rmdel",	PROG,	REALUSER,		PROGPATH(rmdel),
1682136Seric 	"val",		PROG,	0,			PROGPATH(val),
1691864Seric 	"what",		PROG,	NO_SDOT,		PROGPATH(what),
1701864Seric 	"sccsdiff",	SHELL,	REALUSER,		PROGPATH(sccsdiff),
1711864Seric 	"edit",		CMACRO,	NO_SDOT,		"get -e",
1721864Seric 	"delget",	CMACRO,	NO_SDOT,		"delta:mysrp/get:ixbeskcl -t",
1732138Seric 	"deledit",	CMACRO,	NO_SDOT,		"delta:mysrp -n/get:ixbskcl -e -t -g",
1741864Seric 	"fix",		FIX,	NO_SDOT,		NULL,
1751864Seric 	"clean",	CLEAN,	REALUSER|NO_SDOT,	(char *) CLEANC,
1761864Seric 	"info",		CLEAN,	REALUSER|NO_SDOT,	(char *) INFOC,
1771864Seric 	"check",	CLEAN,	REALUSER|NO_SDOT,	(char *) CHECKC,
1781864Seric 	"tell",		CLEAN,	REALUSER|NO_SDOT,	(char *) TELLC,
1791864Seric 	"unedit",	UNEDIT,	NO_SDOT,		NULL,
1801864Seric 	"diffs",	DIFFS,	NO_SDOT|REALUSER,	NULL,
1811871Seric 	"-diff",	DODIFF,	NO_SDOT|REALUSER,	PROGPATH(bdiff),
1822137Seric 	"print",	CMACRO,	0,			"prt -e/get -p -m -s",
1832226Seric 	"branch",	CMACRO,	NO_SDOT,
1842226Seric 		"get:ixrc -e -b/delta: -s -n -ybranch-place-holder/get:pl -e -t -g",
185*6785Smckusick 	"create",	CREATE,	NO_SDOT,		NULL,
1861864Seric 	NULL,		-1,	0,			NULL
187148Seric };
188148Seric 
1891432Seric /* one line from a p-file */
190396Seric struct pfile
191396Seric {
192396Seric 	char	*p_osid;	/* old SID */
193396Seric 	char	*p_nsid;	/* new SID */
194396Seric 	char	*p_user;	/* user who did edit */
195396Seric 	char	*p_date;	/* date of get */
196396Seric 	char	*p_time;	/* time of get */
1972161Seric 	char	*p_aux;		/* extra info at end */
198396Seric };
199396Seric 
2001270Seric char	*SccsPath = SCCSPATH;	/* pathname of SCCS files */
2011270Seric # ifdef SCCSDIR
2021270Seric char	*SccsDir = SCCSDIR;	/* directory to begin search from */
2031205Seric # else
2041270Seric char	*SccsDir = "";
2051205Seric # endif
2061437Seric char	MyName[] = MYNAME;	/* name used in messages */
2071433Seric int	OutFile = -1;		/* override output file for commands */
208157Seric bool	RealUser;		/* if set, running as real user */
209393Seric # ifdef DEBUG
210393Seric bool	Debug;			/* turn on tracing */
211393Seric # endif
2122139Seric # ifndef V6
2132139Seric extern char	*getenv();
2142139Seric # endif V6
2151432Seric 
216148Seric main(argc, argv)
217148Seric 	int argc;
218148Seric 	char **argv;
219148Seric {
220148Seric 	register char *p;
221262Seric 	extern struct sccsprog *lookup();
2221282Seric 	register int i;
2232139Seric # ifndef V6
2242139Seric # ifndef SCCSDIR
2252140Seric 	register struct passwd *pw;
2262140Seric 	extern struct passwd *getpwnam();
2272140Seric 	char buf[100];
2282140Seric 
2292139Seric 	/* pull "SccsDir" out of the environment (possibly) */
2302139Seric 	p = getenv("PROJECT");
2312140Seric 	if (p != NULL && p[0] != '\0')
2322140Seric 	{
2332140Seric 		if (p[0] == '/')
2342140Seric 			SccsDir = p;
2352140Seric 		else
2362140Seric 		{
2372140Seric 			pw = getpwnam(p);
2382140Seric 			if (pw == NULL)
2392140Seric 			{
2402140Seric 				usrerr("user %s does not exist", p);
2412140Seric 				exit(EX_USAGE);
2422140Seric 			}
2432140Seric 			strcpy(buf, pw->pw_dir);
2442140Seric 			strcat(buf, "/src");
2452140Seric 			if (access(buf, 0) < 0)
2462140Seric 			{
2472140Seric 				strcpy(buf, pw->pw_dir);
2482140Seric 				strcat(buf, "/source");
2492140Seric 				if (access(buf, 0) < 0)
2502140Seric 				{
2512140Seric 					usrerr("project %s has no source!", p);
2522140Seric 					exit(EX_USAGE);
2532140Seric 				}
2542140Seric 			}
2552140Seric 			SccsDir = buf;
2562140Seric 		}
2572140Seric 	}
2582139Seric # endif SCCSDIR
2592139Seric # endif V6
2602139Seric 
261148Seric 	/*
262148Seric 	**  Detect and decode flags intended for this program.
263148Seric 	*/
264148Seric 
265200Seric 	if (argc < 2)
266148Seric 	{
2671205Seric 		fprintf(stderr, "Usage: %s [flags] command [flags]\n", MyName);
268200Seric 		exit(EX_USAGE);
269200Seric 	}
270200Seric 	argv[argc] = NULL;
271200Seric 
272262Seric 	if (lookup(argv[0]) == NULL)
273200Seric 	{
274262Seric 		while ((p = *++argv) != NULL)
275148Seric 		{
276262Seric 			if (*p != '-')
277262Seric 				break;
278262Seric 			switch (*++p)
279262Seric 			{
280262Seric 			  case 'r':		/* run as real user */
281262Seric 				setuid(getuid());
282262Seric 				RealUser++;
283262Seric 				break;
284148Seric 
2851270Seric # ifndef SCCSDIR
286262Seric 			  case 'p':		/* path of sccs files */
287262Seric 				SccsPath = ++p;
2882348Seric 				if (SccsPath[0] == '\0' && argv[1] != NULL)
2892348Seric 					SccsPath = *++argv;
290262Seric 				break;
291148Seric 
292588Seric 			  case 'd':		/* directory to search from */
293588Seric 				SccsDir = ++p;
2942348Seric 				if (SccsDir[0] == '\0' && argv[1] != NULL)
2952348Seric 					SccsDir = *++argv;
296588Seric 				break;
2971205Seric # endif
298588Seric 
299393Seric # ifdef DEBUG
300393Seric 			  case 'T':		/* trace */
301393Seric 				Debug++;
302393Seric 				break;
303393Seric # endif
304393Seric 
305262Seric 			  default:
3061205Seric 				usrerr("unknown option -%s", p);
307262Seric 				break;
308262Seric 			}
309148Seric 		}
310262Seric 		if (SccsPath[0] == '\0')
311262Seric 			SccsPath = ".";
312148Seric 	}
313148Seric 
3141737Seric 	i = command(argv, FALSE, "");
3151282Seric 	exit(i);
316200Seric }
3171432Seric 
3181432Seric /*
3191282Seric **  COMMAND -- look up and perform a command
3201282Seric **
3211282Seric **	This routine is the guts of this program.  Given an
3221282Seric **	argument vector, it looks up the "command" (argv[0])
3231282Seric **	in the configuration table and does the necessary stuff.
3241282Seric **
3251282Seric **	Parameters:
3261282Seric **		argv -- an argument vector to process.
3271282Seric **		forkflag -- if set, fork before executing the command.
3281316Seric **		editflag -- if set, only include flags listed in the
3291316Seric **			sccsklets field of the command descriptor.
3301316Seric **		arg0 -- a space-seperated list of arguments to insert
3311316Seric **			before argv.
3321282Seric **
3331282Seric **	Returns:
3341282Seric **		zero -- command executed ok.
3351282Seric **		else -- error status.
3361282Seric **
3371282Seric **	Side Effects:
3381282Seric **		none.
3391282Seric */
340157Seric 
3411737Seric command(argv, forkflag, arg0)
342200Seric 	char **argv;
343201Seric 	bool forkflag;
3441316Seric 	char *arg0;
345200Seric {
346200Seric 	register struct sccsprog *cmd;
347200Seric 	register char *p;
348*6785Smckusick 	char buf[100];
349262Seric 	extern struct sccsprog *lookup();
3501316Seric 	char *nav[1000];
3511316Seric 	char **np;
3521431Seric 	register char **ap;
353585Seric 	register int i;
3541431Seric 	register char *q;
355585Seric 	extern bool unedit();
3561282Seric 	int rval = 0;
3571316Seric 	extern char *index();
3581316Seric 	extern char *makefile();
3591737Seric 	char *editchs;
3601435Seric 	extern char *tail();
361200Seric 
362393Seric # ifdef DEBUG
363393Seric 	if (Debug)
364393Seric 	{
3651316Seric 		printf("command:\n\t\"%s\"\n", arg0);
3661316Seric 		for (np = argv; *np != NULL; np++)
3671316Seric 			printf("\t\"%s\"\n", *np);
368393Seric 	}
369393Seric # endif
370393Seric 
371157Seric 	/*
3721316Seric 	**  Copy arguments.
3731438Seric 	**	Copy from arg0 & if necessary at most one arg
3741438Seric 	**	from argv[0].
3751316Seric 	*/
3761316Seric 
3771431Seric 	np = ap = &nav[1];
3781737Seric 	editchs = NULL;
3791821Seric 	for (p = arg0, q = buf; *p != '\0' && *p != '/'; )
3801316Seric 	{
3811316Seric 		*np++ = q;
3821316Seric 		while (*p == ' ')
3831316Seric 			p++;
3841737Seric 		while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':')
3851316Seric 			*q++ = *p++;
3861316Seric 		*q++ = '\0';
3871737Seric 		if (*p == ':')
3881737Seric 		{
3891737Seric 			editchs = q;
3901821Seric 			while (*++p != '\0' && *p != '/' && *p != ' ')
3911737Seric 				*q++ = *p;
3921737Seric 			*q++ = '\0';
3931737Seric 		}
3941316Seric 	}
3951316Seric 	*np = NULL;
3961431Seric 	if (*ap == NULL)
3971316Seric 		*np++ = *argv++;
3981316Seric 
3991316Seric 	/*
400148Seric 	**  Look up command.
4011431Seric 	**	At this point, *ap is the command name.
402148Seric 	*/
403148Seric 
4041431Seric 	cmd = lookup(*ap);
405262Seric 	if (cmd == NULL)
406148Seric 	{
4071431Seric 		usrerr("Unknown command \"%s\"", *ap);
4081282Seric 		return (EX_USAGE);
409148Seric 	}
410148Seric 
411148Seric 	/*
4121316Seric 	**  Copy remaining arguments doing editing as appropriate.
4131316Seric 	*/
4141316Seric 
4151316Seric 	for (; *argv != NULL; argv++)
4161316Seric 	{
4171316Seric 		p = *argv;
4181316Seric 		if (*p == '-')
4191316Seric 		{
4201737Seric 			if (p[1] == '\0' || editchs == NULL || index(editchs, p[1]) != NULL)
4211316Seric 				*np++ = p;
4221316Seric 		}
4231316Seric 		else
4241316Seric 		{
4251316Seric 			if (!bitset(NO_SDOT, cmd->sccsflags))
4261316Seric 				p = makefile(p);
4271316Seric 			if (p != NULL)
4281316Seric 				*np++ = p;
4291316Seric 		}
4301316Seric 	}
4311316Seric 	*np = NULL;
4321316Seric 
4331316Seric 	/*
434200Seric 	**  Interpret operation associated with this command.
435157Seric 	*/
436157Seric 
437200Seric 	switch (cmd->sccsoper)
438200Seric 	{
4391431Seric 	  case SHELL:		/* call a shell file */
4401431Seric 		*ap = cmd->sccspath;
4411431Seric 		*--ap = "sh";
4421431Seric 		rval = callprog("/bin/sh", cmd->sccsflags, ap, forkflag);
4431431Seric 		break;
4441431Seric 
445200Seric 	  case PROG:		/* call an sccs prog */
4461431Seric 		rval = callprog(cmd->sccspath, cmd->sccsflags, ap, forkflag);
447201Seric 		break;
448201Seric 
449201Seric 	  case CMACRO:		/* command macro */
4501438Seric 		/* step through & execute each part of the macro */
451201Seric 		for (p = cmd->sccspath; *p != '\0'; p++)
452201Seric 		{
4531316Seric 			q = p;
4541316Seric 			while (*p != '\0' && *p != '/')
4551316Seric 				p++;
4561737Seric 			rval = command(&ap[1], *p != '\0', q);
4571282Seric 			if (rval != 0)
4581282Seric 				break;
459201Seric 		}
4601282Seric 		break;
461157Seric 
462226Seric 	  case FIX:		/* fix a delta */
4631431Seric 		if (strncmp(ap[1], "-r", 2) != 0)
464226Seric 		{
4651205Seric 			usrerr("-r flag needed for fix command");
4661282Seric 			rval = EX_USAGE;
467226Seric 			break;
468226Seric 		}
4691438Seric 
4701438Seric 		/* get the version with all changes */
4711737Seric 		rval = command(&ap[1], TRUE, "get -k");
4721438Seric 
4731438Seric 		/* now remove that version from the s-file */
4741282Seric 		if (rval == 0)
4751737Seric 			rval = command(&ap[1], TRUE, "rmdel:r");
4761438Seric 
4771438Seric 		/* and edit the old version (but don't clobber new vers) */
4781282Seric 		if (rval == 0)
4791737Seric 			rval = command(&ap[2], FALSE, "get -e -g");
4801282Seric 		break;
481226Seric 
482261Seric 	  case CLEAN:
4831822Seric 		rval = clean((int) cmd->sccspath, ap);
484261Seric 		break;
485261Seric 
486396Seric 	  case UNEDIT:
4871431Seric 		for (argv = np = &ap[1]; *argv != NULL; argv++)
488585Seric 		{
4891316Seric 			if (unedit(*argv))
4901316Seric 				*np++ = *argv;
491585Seric 		}
4921316Seric 		*np = NULL;
4931438Seric 
4941438Seric 		/* get all the files that we unedited successfully */
4951738Seric 		if (np > &ap[1])
4961737Seric 			rval = command(&ap[1], FALSE, "get");
497396Seric 		break;
498396Seric 
4991433Seric 	  case DIFFS:		/* diff between s-file & edit file */
5001433Seric 		/* find the end of the flag arguments */
5011433Seric 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
5021433Seric 			continue;
5031433Seric 		argv = np;
5041433Seric 
5051433Seric 		/* for each file, do the diff */
5061502Seric 		p = argv[1];
5071433Seric 		while (*np != NULL)
5081433Seric 		{
5091438Seric 			/* messy, but we need a null terminated argv */
5101433Seric 			*argv = *np++;
5111502Seric 			argv[1] = NULL;
5121435Seric 			i = dodiff(ap, tail(*argv));
5131433Seric 			if (rval == 0)
5141433Seric 				rval = i;
5151502Seric 			argv[1] = p;
5161433Seric 		}
5171433Seric 		break;
5181433Seric 
5191871Seric 	  case DODIFF:		/* internal diff call */
5201871Seric 		setuid(getuid());
5211871Seric 		for (np = ap; *np != NULL; np++)
5221871Seric 		{
5231871Seric 			if ((*np)[0] == '-' && (*np)[1] == 'C')
5241871Seric 				(*np)[1] = 'c';
5251871Seric 		}
5261871Seric 
5271871Seric 		/* insert "-" argument */
5281871Seric 		np[1] = NULL;
5291871Seric 		np[0] = np[-1];
5301871Seric 		np[-1] = "-";
5311871Seric 
5321871Seric 		/* execute the diff program of choice */
5331871Seric # ifndef V6
5341871Seric 		execvp("diff", ap);
5351871Seric # endif
5361871Seric 		execv(cmd->sccspath, argv);
5371871Seric 		syserr("cannot exec %s", cmd->sccspath);
5381871Seric 		exit(EX_OSERR);
5391871Seric 
540*6785Smckusick 	  case CREATE:		/* create new sccs files */
541*6785Smckusick 		/* skip over flag arguments */
542*6785Smckusick 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
543*6785Smckusick 			continue;
544*6785Smckusick 		argv = np;
545*6785Smckusick 
546*6785Smckusick 		/* do an admin for each file */
547*6785Smckusick 		p = argv[1];
548*6785Smckusick 		while (*np != NULL)
549*6785Smckusick 		{
550*6785Smckusick 			printf("\n%s:\n", *np);
551*6785Smckusick 			sprintf(buf, "-i%s", *np);
552*6785Smckusick 			ap[0] = buf;
553*6785Smckusick 			argv[0] = tail(*np);
554*6785Smckusick 			argv[1] = NULL;
555*6785Smckusick 			rval = command(ap, TRUE, "admin");
556*6785Smckusick 			argv[1] = p;
557*6785Smckusick 			if (rval == 0)
558*6785Smckusick 			{
559*6785Smckusick 				sprintf(buf, ",%s", tail(*np));
560*6785Smckusick 				if (link(*np, buf) >= 0)
561*6785Smckusick 					unlink(*np);
562*6785Smckusick 			}
563*6785Smckusick 			np++;
564*6785Smckusick 		}
565*6785Smckusick 		break;
566*6785Smckusick 
567200Seric 	  default:
5681205Seric 		syserr("oper %d", cmd->sccsoper);
569200Seric 		exit(EX_SOFTWARE);
570200Seric 	}
5711282Seric # ifdef DEBUG
5721282Seric 	if (Debug)
5731282Seric 		printf("command: rval=%d\n", rval);
5741282Seric # endif
5751282Seric 	return (rval);
576200Seric }
5771432Seric 
5781432Seric /*
579262Seric **  LOOKUP -- look up an SCCS command name.
580262Seric **
581262Seric **	Parameters:
582262Seric **		name -- the name of the command to look up.
583262Seric **
584262Seric **	Returns:
585262Seric **		ptr to command descriptor for this command.
586262Seric **		NULL if no such entry.
587262Seric **
588262Seric **	Side Effects:
589262Seric **		none.
590262Seric */
591200Seric 
592262Seric struct sccsprog *
593262Seric lookup(name)
594262Seric 	char *name;
595262Seric {
596262Seric 	register struct sccsprog *cmd;
597226Seric 
598262Seric 	for (cmd = SccsProg; cmd->sccsname != NULL; cmd++)
599262Seric 	{
600262Seric 		if (strcmp(cmd->sccsname, name) == 0)
601262Seric 			return (cmd);
602262Seric 	}
603262Seric 	return (NULL);
604262Seric }
6051432Seric 
6061432Seric /*
6071282Seric **  CALLPROG -- call a program
6081282Seric **
6091316Seric **	Used to call the SCCS programs.
6101282Seric **
6111282Seric **	Parameters:
6121282Seric **		progpath -- pathname of the program to call.
6131282Seric **		flags -- status flags from the command descriptors.
6141282Seric **		argv -- an argument vector to pass to the program.
6151282Seric **		forkflag -- if true, fork before calling, else just
6161282Seric **			exec.
6171282Seric **
6181282Seric **	Returns:
6191282Seric **		The exit status of the program.
6201282Seric **		Nothing if forkflag == FALSE.
6211282Seric **
6221282Seric **	Side Effects:
6231282Seric **		Can exit if forkflag == FALSE.
6241282Seric */
625226Seric 
626200Seric callprog(progpath, flags, argv, forkflag)
627200Seric 	char *progpath;
628200Seric 	short flags;
629200Seric 	char **argv;
630200Seric 	bool forkflag;
631200Seric {
632200Seric 	register int i;
633201Seric 	auto int st;
634200Seric 
6351316Seric # ifdef DEBUG
6361316Seric 	if (Debug)
6371316Seric 	{
6381316Seric 		printf("callprog:\n");
6391316Seric 		for (i = 0; argv[i] != NULL; i++)
6401316Seric 			printf("\t\"%s\"\n", argv[i]);
6411316Seric 	}
6421316Seric # endif
6431316Seric 
644200Seric 	if (*argv == NULL)
645200Seric 		return (-1);
646200Seric 
647157Seric 	/*
648226Seric 	**  Fork if appropriate.
649148Seric 	*/
650148Seric 
651200Seric 	if (forkflag)
652200Seric 	{
653393Seric # ifdef DEBUG
654393Seric 		if (Debug)
655393Seric 			printf("Forking\n");
656393Seric # endif
657200Seric 		i = fork();
658200Seric 		if (i < 0)
659200Seric 		{
6601205Seric 			syserr("cannot fork");
661200Seric 			exit(EX_OSERR);
662200Seric 		}
663200Seric 		else if (i > 0)
664201Seric 		{
665201Seric 			wait(&st);
6661282Seric 			if ((st & 0377) == 0)
6671282Seric 				st = (st >> 8) & 0377;
6681433Seric 			if (OutFile >= 0)
6691433Seric 			{
6701433Seric 				close(OutFile);
6711433Seric 				OutFile = -1;
6721433Seric 			}
673201Seric 			return (st);
674201Seric 		}
675200Seric 	}
6761433Seric 	else if (OutFile >= 0)
6771433Seric 	{
6781433Seric 		syserr("callprog: setting stdout w/o forking");
6791433Seric 		exit(EX_SOFTWARE);
6801433Seric 	}
681200Seric 
6821433Seric 	/* set protection as appropriate */
683200Seric 	if (bitset(REALUSER, flags))
684200Seric 		setuid(getuid());
6851433Seric 
6861433Seric 	/* change standard input & output if needed */
6871433Seric 	if (OutFile >= 0)
6881433Seric 	{
6891433Seric 		close(1);
6901433Seric 		dup(OutFile);
6911433Seric 		close(OutFile);
6921433Seric 	}
693226Seric 
6941433Seric 	/* call real SCCS program */
695226Seric 	execv(progpath, argv);
6961205Seric 	syserr("cannot execute %s", progpath);
697148Seric 	exit(EX_UNAVAILABLE);
6981738Seric 	/*NOTREACHED*/
699148Seric }
7001432Seric 
7011432Seric /*
702586Seric **  MAKEFILE -- make filename of SCCS file
703586Seric **
704586Seric **	If the name passed is already the name of an SCCS file,
705586Seric **	just return it.  Otherwise, munge the name into the name
706586Seric **	of the actual SCCS file.
707586Seric **
708586Seric **	There are cases when it is not clear what you want to
709586Seric **	do.  For example, if SccsPath is an absolute pathname
710586Seric **	and the name given is also an absolute pathname, we go
711586Seric **	for SccsPath (& only use the last component of the name
712586Seric **	passed) -- this is important for security reasons (if
713586Seric **	sccs is being used as a setuid front end), but not
714586Seric **	particularly intuitive.
715586Seric **
716586Seric **	Parameters:
717586Seric **		name -- the file name to be munged.
718586Seric **
719586Seric **	Returns:
720586Seric **		The pathname of the sccs file.
721586Seric **		NULL on error.
722586Seric **
723586Seric **	Side Effects:
724586Seric **		none.
725586Seric */
726148Seric 
727148Seric char *
728148Seric makefile(name)
729148Seric 	char *name;
730148Seric {
731148Seric 	register char *p;
732148Seric 	char buf[512];
733148Seric 	extern char *malloc();
734586Seric 	extern char *rindex();
735588Seric 	extern bool safepath();
736587Seric 	extern bool isdir();
737587Seric 	register char *q;
738148Seric 
739586Seric 	p = rindex(name, '/');
740586Seric 	if (p == NULL)
741586Seric 		p = name;
742586Seric 	else
743586Seric 		p++;
744586Seric 
745148Seric 	/*
746588Seric 	**  Check to see that the path is "safe", i.e., that we
747588Seric 	**  are not letting some nasty person use the setuid part
748588Seric 	**  of this program to look at or munge some presumably
749588Seric 	**  hidden files.
750148Seric 	*/
751148Seric 
752588Seric 	if (SccsDir[0] == '/' && !safepath(name))
753588Seric 		return (NULL);
754586Seric 
755586Seric 	/*
756588Seric 	**  Create the base pathname.
757586Seric 	*/
758586Seric 
7591438Seric 	/* first the directory part */
760588Seric 	if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0)
761148Seric 	{
762588Seric 		strcpy(buf, SccsDir);
763586Seric 		strcat(buf, "/");
764586Seric 	}
765586Seric 	else
766586Seric 		strcpy(buf, "");
7671438Seric 
7681438Seric 	/* then the head of the pathname */
769587Seric 	strncat(buf, name, p - name);
770587Seric 	q = &buf[strlen(buf)];
7711438Seric 
7721438Seric 	/* now copy the final part of the name, in case useful */
773587Seric 	strcpy(q, p);
7741438Seric 
7751438Seric 	/* so is it useful? */
776587Seric 	if (strncmp(p, "s.", 2) != 0 && !isdir(buf))
777586Seric 	{
7781438Seric 		/* sorry, no; copy the SCCS pathname & the "s." */
779588Seric 		strcpy(q, SccsPath);
780588Seric 		strcat(buf, "/s.");
7811438Seric 
7821438Seric 		/* and now the end of the name */
783586Seric 		strcat(buf, p);
784586Seric 	}
785148Seric 
7861438Seric 	/* if i haven't changed it, why did I do all this? */
787588Seric 	if (strcmp(buf, name) == 0)
788588Seric 		p = name;
789588Seric 	else
790148Seric 	{
7911438Seric 		/* but if I have, squirrel it away */
792588Seric 		p = malloc(strlen(buf) + 1);
793588Seric 		if (p == NULL)
794588Seric 		{
795588Seric 			perror("Sccs: no mem");
796588Seric 			exit(EX_OSERR);
797588Seric 		}
798588Seric 		strcpy(p, buf);
799148Seric 	}
8001438Seric 
801148Seric 	return (p);
802148Seric }
8031432Seric 
8041432Seric /*
805587Seric **  ISDIR -- return true if the argument is a directory.
806587Seric **
807587Seric **	Parameters:
808587Seric **		name -- the pathname of the file to check.
809587Seric **
810587Seric **	Returns:
811587Seric **		TRUE if 'name' is a directory, FALSE otherwise.
812587Seric **
813587Seric **	Side Effects:
814587Seric **		none.
815587Seric */
816587Seric 
817587Seric bool
818587Seric isdir(name)
819587Seric 	char *name;
820587Seric {
821587Seric 	struct stat stbuf;
822587Seric 
823587Seric 	return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR);
824587Seric }
8251432Seric 
8261432Seric /*
827586Seric **  SAFEPATH -- determine whether a pathname is "safe"
828586Seric **
829586Seric **	"Safe" pathnames only allow you to get deeper into the
830586Seric **	directory structure, i.e., full pathnames and ".." are
831586Seric **	not allowed.
832586Seric **
833586Seric **	Parameters:
834586Seric **		p -- the name to check.
835586Seric **
836586Seric **	Returns:
837586Seric **		TRUE -- if the path is safe.
838586Seric **		FALSE -- if the path is not safe.
839586Seric **
840586Seric **	Side Effects:
841586Seric **		Prints a message if the path is not safe.
842586Seric */
843586Seric 
844586Seric bool
845586Seric safepath(p)
846586Seric 	register char *p;
847586Seric {
848586Seric 	extern char *index();
849586Seric 
850586Seric 	if (*p != '/')
851586Seric 	{
852586Seric 		while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0)
853586Seric 		{
854586Seric 			p = index(p, '/');
855586Seric 			if (p == NULL)
856586Seric 				return (TRUE);
857586Seric 			p++;
858586Seric 		}
859586Seric 	}
860586Seric 
861586Seric 	printf("You may not use full pathnames or \"..\"\n");
862586Seric 	return (FALSE);
863586Seric }
8641432Seric 
8651432Seric /*
866261Seric **  CLEAN -- clean out recreatable files
867261Seric **
868261Seric **	Any file for which an "s." file exists but no "p." file
869261Seric **	exists in the current directory is purged.
870261Seric **
871261Seric **	Parameters:
8721822Seric **		mode -- tells whether this came from a "clean", "info", or
8731822Seric **			"check" command.
8741822Seric **		argv -- the rest of the argument vector.
875261Seric **
876261Seric **	Returns:
877261Seric **		none.
878261Seric **
879261Seric **	Side Effects:
880819Seric **		Removes files in the current directory.
881819Seric **		Prints information regarding files being edited.
882819Seric **		Exits if a "check" command.
883261Seric */
884261Seric 
8851822Seric clean(mode, argv)
886819Seric 	int mode;
8871822Seric 	char **argv;
888261Seric {
8896784Smckusick 	struct direct *dir;
890261Seric 	char buf[100];
8912140Seric 	char *bufend;
8926784Smckusick 	register DIR *dirfd;
893346Seric 	register char *basefile;
894351Seric 	bool gotedit;
8951822Seric 	bool gotpfent;
896394Seric 	FILE *pfp;
8971822Seric 	bool nobranch = FALSE;
8981822Seric 	extern struct pfile *getpfent();
8991822Seric 	register struct pfile *pf;
9001822Seric 	register char **ap;
9011864Seric 	extern char *username();
9021864Seric 	char *usernm = NULL;
9032140Seric 	char *subdir = NULL;
9042140Seric 	char *cmdname;
905261Seric 
9061438Seric 	/*
9071822Seric 	**  Process the argv
9081822Seric 	*/
9091822Seric 
9102140Seric 	cmdname = *argv;
9112140Seric 	for (ap = argv; *++ap != NULL; )
9121822Seric 	{
9131864Seric 		if (**ap == '-')
9141864Seric 		{
9151864Seric 			/* we have a flag */
9161864Seric 			switch ((*ap)[1])
9171864Seric 			{
9181864Seric 			  case 'b':
9191864Seric 				nobranch = TRUE;
9201864Seric 				break;
9211864Seric 
9221864Seric 			  case 'u':
9231864Seric 				if ((*ap)[2] != '\0')
9241864Seric 					usernm = &(*ap)[2];
9251864Seric 				else if (ap[1] != NULL && ap[1][0] != '-')
9261864Seric 					usernm = *++ap;
9271864Seric 				else
9281864Seric 					usernm = username();
9291864Seric 				break;
9301864Seric 			}
9311864Seric 		}
9322140Seric 		else
9332140Seric 		{
9342140Seric 			if (subdir != NULL)
9352140Seric 				usrerr("too many args");
9362140Seric 			else
9372140Seric 				subdir = *ap;
9382140Seric 		}
9391822Seric 	}
9401822Seric 
9411822Seric 	/*
9421438Seric 	**  Find and open the SCCS directory.
9431438Seric 	*/
9441438Seric 
9451207Seric 	strcpy(buf, SccsDir);
9461207Seric 	if (buf[0] != '\0')
9471207Seric 		strcat(buf, "/");
9482140Seric 	if (subdir != NULL)
9492140Seric 	{
9502140Seric 		strcat(buf, subdir);
9512140Seric 		strcat(buf, "/");
9522140Seric 	}
9531207Seric 	strcat(buf, SccsPath);
9542140Seric 	bufend = &buf[strlen(buf)];
9551438Seric 
9566784Smckusick 	dirfd = opendir(buf);
957261Seric 	if (dirfd == NULL)
958261Seric 	{
9591207Seric 		usrerr("cannot open %s", buf);
9601282Seric 		return (EX_NOINPUT);
961261Seric 	}
962261Seric 
963261Seric 	/*
964261Seric 	**  Scan the SCCS directory looking for s. files.
9651438Seric 	**	gotedit tells whether we have tried to clean any
9661438Seric 	**		files that are being edited.
967261Seric 	*/
968261Seric 
969351Seric 	gotedit = FALSE;
9706784Smckusick 	while (dir = readdir(dirfd)) {
9716784Smckusick 		if (strncmp(dir->d_name, "s.", 2) != 0)
972261Seric 			continue;
973261Seric 
974261Seric 		/* got an s. file -- see if the p. file exists */
9752140Seric 		strcpy(bufend, "/p.");
9762140Seric 		basefile = bufend + 3;
9776784Smckusick 		strcpy(basefile, &dir->d_name[2]);
9781822Seric 
9791822Seric 		/*
9801822Seric 		**  open and scan the p-file.
9811822Seric 		**	'gotpfent' tells if we have found a valid p-file
9821822Seric 		**		entry.
9831822Seric 		*/
9841822Seric 
985394Seric 		pfp = fopen(buf, "r");
9861822Seric 		gotpfent = FALSE;
987394Seric 		if (pfp != NULL)
988346Seric 		{
9891438Seric 			/* the file exists -- report it's contents */
9901822Seric 			while ((pf = getpfent(pfp)) != NULL)
9911730Seric 			{
9921822Seric 				if (nobranch && isbranch(pf->p_nsid))
9931822Seric 					continue;
9941864Seric 				if (usernm != NULL && strcmp(usernm, pf->p_user) != 0 && mode != CLEANC)
9951864Seric 					continue;
9961822Seric 				gotedit = TRUE;
9971822Seric 				gotpfent = TRUE;
9981822Seric 				if (mode == TELLC)
9991822Seric 				{
10001822Seric 					printf("%s\n", basefile);
10011822Seric 					break;
10021822Seric 				}
10032161Seric 				printf("%12s: being edited: ", basefile);
10042161Seric 				putpfent(pf, stdout);
10051730Seric 			}
1006394Seric 			fclose(pfp);
10071822Seric 		}
1008261Seric 
1009261Seric 		/* the s. file exists and no p. file exists -- unlink the g-file */
10101870Seric 		if (mode == CLEANC && !gotpfent)
1011346Seric 		{
10126784Smckusick 			strcpy(buf, &dir->d_name[2]);
1013346Seric 			unlink(buf);
1014346Seric 		}
1015261Seric 	}
1016261Seric 
10171438Seric 	/* cleanup & report results */
10186784Smckusick 	closedir(dirfd);
1019819Seric 	if (!gotedit && mode == INFOC)
10201864Seric 	{
10211864Seric 		printf("Nothing being edited");
10221864Seric 		if (nobranch)
10231864Seric 			printf(" (on trunk)");
10241864Seric 		if (usernm == NULL)
10251864Seric 			printf("\n");
10261864Seric 		else
10271864Seric 			printf(" by %s\n", usernm);
10281864Seric 	}
1029819Seric 	if (mode == CHECKC)
1030819Seric 		exit(gotedit);
10311282Seric 	return (EX_OK);
1032261Seric }
10331432Seric 
10341432Seric /*
10351822Seric **  ISBRANCH -- is the SID a branch?
10361822Seric **
10371822Seric **	Parameters:
10381822Seric **		sid -- the sid to check.
10391822Seric **
10401822Seric **	Returns:
10411822Seric **		TRUE if the sid represents a branch.
10421822Seric **		FALSE otherwise.
10431822Seric **
10441822Seric **	Side Effects:
10451822Seric **		none.
10461822Seric */
10471822Seric 
10481822Seric isbranch(sid)
10491822Seric 	char *sid;
10501822Seric {
10511822Seric 	register char *p;
10521822Seric 	int dots;
10531822Seric 
10541822Seric 	dots = 0;
10551822Seric 	for (p = sid; *p != '\0'; p++)
10561822Seric 	{
10571822Seric 		if (*p == '.')
10581822Seric 			dots++;
10591822Seric 		if (dots > 1)
10601822Seric 			return (TRUE);
10611822Seric 	}
10621822Seric 	return (FALSE);
10631822Seric }
10641822Seric 
10651822Seric /*
1066396Seric **  UNEDIT -- unedit a file
1067396Seric **
1068396Seric **	Checks to see that the current user is actually editting
1069396Seric **	the file and arranges that s/he is not editting it.
1070396Seric **
1071396Seric **	Parameters:
1072416Seric **		fn -- the name of the file to be unedited.
1073396Seric **
1074396Seric **	Returns:
1075585Seric **		TRUE -- if the file was successfully unedited.
1076585Seric **		FALSE -- if the file was not unedited for some
1077585Seric **			reason.
1078396Seric **
1079396Seric **	Side Effects:
1080396Seric **		fn is removed
1081396Seric **		entries are removed from pfile.
1082396Seric */
1083396Seric 
1084585Seric bool
1085396Seric unedit(fn)
1086396Seric 	char *fn;
1087396Seric {
1088396Seric 	register FILE *pfp;
1089396Seric 	char *pfn;
1090396Seric 	static char tfn[] = "/tmp/sccsXXXXX";
1091396Seric 	FILE *tfp;
1092396Seric 	register char *q;
1093396Seric 	bool delete = FALSE;
1094396Seric 	bool others = FALSE;
1095396Seric 	char *myname;
10961864Seric 	extern char *username();
1097396Seric 	struct pfile *pent;
10981822Seric 	extern struct pfile *getpfent();
1099396Seric 	char buf[120];
11001316Seric 	extern char *makefile();
1101396Seric 
1102396Seric 	/* make "s." filename & find the trailing component */
1103396Seric 	pfn = makefile(fn);
1104586Seric 	if (pfn == NULL)
1105586Seric 		return (FALSE);
1106586Seric 	q = rindex(pfn, '/');
1107586Seric 	if (q == NULL)
1108586Seric 		q = &pfn[-1];
1109586Seric 	if (q[1] != 's' || q[2] != '.')
1110396Seric 	{
11111205Seric 		usrerr("bad file name \"%s\"", fn);
1112585Seric 		return (FALSE);
1113396Seric 	}
1114396Seric 
11151438Seric 	/* turn "s." into "p." & try to open it */
1116396Seric 	*++q = 'p';
1117396Seric 
1118396Seric 	pfp = fopen(pfn, "r");
1119396Seric 	if (pfp == NULL)
1120396Seric 	{
1121416Seric 		printf("%12s: not being edited\n", fn);
1122585Seric 		return (FALSE);
1123396Seric 	}
1124396Seric 
11251438Seric 	/* create temp file for editing p-file */
1126396Seric 	mktemp(tfn);
1127396Seric 	tfp = fopen(tfn, "w");
1128396Seric 	if (tfp == NULL)
1129396Seric 	{
11301205Seric 		usrerr("cannot create \"%s\"", tfn);
1131396Seric 		exit(EX_OSERR);
1132396Seric 	}
1133396Seric 
11341438Seric 	/* figure out who I am */
11351864Seric 	myname = username();
11361438Seric 
11371438Seric 	/*
11381438Seric 	**  Copy p-file to temp file, doing deletions as needed.
11391438Seric 	*/
11401438Seric 
11411822Seric 	while ((pent = getpfent(pfp)) != NULL)
1142396Seric 	{
1143396Seric 		if (strcmp(pent->p_user, myname) == 0)
1144396Seric 		{
1145396Seric 			/* a match */
1146396Seric 			delete++;
1147396Seric 		}
1148396Seric 		else
1149396Seric 		{
11501438Seric 			/* output it again */
11512161Seric 			putpfent(pent, tfp);
1152396Seric 			others++;
1153396Seric 		}
1154396Seric 	}
1155396Seric 
1156396Seric 	/* do final cleanup */
1157396Seric 	if (others)
1158396Seric 	{
11591438Seric 		/* copy it back (perhaps it should be linked?) */
1160396Seric 		if (freopen(tfn, "r", tfp) == NULL)
1161396Seric 		{
11621205Seric 			syserr("cannot reopen \"%s\"", tfn);
1163396Seric 			exit(EX_OSERR);
1164396Seric 		}
1165396Seric 		if (freopen(pfn, "w", pfp) == NULL)
1166396Seric 		{
11671205Seric 			usrerr("cannot create \"%s\"", pfn);
1168585Seric 			return (FALSE);
1169396Seric 		}
1170396Seric 		while (fgets(buf, sizeof buf, tfp) != NULL)
1171396Seric 			fputs(buf, pfp);
1172396Seric 	}
1173396Seric 	else
1174396Seric 	{
11751438Seric 		/* it's empty -- remove it */
1176396Seric 		unlink(pfn);
1177396Seric 	}
1178396Seric 	fclose(tfp);
1179396Seric 	fclose(pfp);
1180396Seric 	unlink(tfn);
1181396Seric 
11821438Seric 	/* actually remove the g-file */
1183396Seric 	if (delete)
1184396Seric 	{
11851435Seric 		unlink(tail(fn));
11861435Seric 		printf("%12s: removed\n", tail(fn));
1187585Seric 		return (TRUE);
1188396Seric 	}
1189396Seric 	else
1190396Seric 	{
1191416Seric 		printf("%12s: not being edited by you\n", fn);
1192585Seric 		return (FALSE);
1193396Seric 	}
1194396Seric }
11951432Seric 
11961432Seric /*
11971433Seric **  DODIFF -- diff an s-file against a g-file
11981433Seric **
11991433Seric **	Parameters:
12001433Seric **		getv -- argv for the 'get' command.
12011433Seric **		gfile -- name of the g-file to diff against.
12021433Seric **
12031433Seric **	Returns:
12041433Seric **		Result of get.
12051433Seric **
12061433Seric **	Side Effects:
12071433Seric **		none.
12081433Seric */
12091433Seric 
12101433Seric dodiff(getv, gfile)
12111433Seric 	char **getv;
12121433Seric 	char *gfile;
12131433Seric {
12141433Seric 	int pipev[2];
12151433Seric 	int rval;
12161433Seric 	register int i;
12171433Seric 	register int pid;
12181433Seric 	auto int st;
12191433Seric 	extern int errno;
12201433Seric 	int (*osig)();
12211433Seric 
12221905Seric 	printf("\n------- %s -------\n", gfile);
12231871Seric 	fflush(stdout);
12241501Seric 
12251438Seric 	/* create context for diff to run in */
12261433Seric 	if (pipe(pipev) < 0)
12271433Seric 	{
12281433Seric 		syserr("dodiff: pipe failed");
12291433Seric 		exit(EX_OSERR);
12301433Seric 	}
12311433Seric 	if ((pid = fork()) < 0)
12321433Seric 	{
12331433Seric 		syserr("dodiff: fork failed");
12341433Seric 		exit(EX_OSERR);
12351433Seric 	}
12361433Seric 	else if (pid > 0)
12371433Seric 	{
12381433Seric 		/* in parent; run get */
12391433Seric 		OutFile = pipev[1];
12401433Seric 		close(pipev[0]);
12411871Seric 		rval = command(&getv[1], TRUE, "get:rcixt -s -k -p");
12421433Seric 		osig = signal(SIGINT, SIG_IGN);
12431433Seric 		while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR)
12441433Seric 			errno = 0;
12451433Seric 		signal(SIGINT, osig);
12461433Seric 		/* ignore result of diff */
12471433Seric 	}
12481433Seric 	else
12491433Seric 	{
12501433Seric 		/* in child, run diff */
12511433Seric 		if (close(pipev[1]) < 0 || close(0) < 0 ||
12521433Seric 		    dup(pipev[0]) != 0 || close(pipev[0]) < 0)
12531433Seric 		{
12541433Seric 			syserr("dodiff: magic failed");
12551433Seric 			exit(EX_OSERR);
12561433Seric 		}
12571871Seric 		command(&getv[1], FALSE, "-diff:elsfhbC");
12581433Seric 	}
12591433Seric 	return (rval);
12601433Seric }
12611433Seric 
12621433Seric /*
12631435Seric **  TAIL -- return tail of filename.
12641435Seric **
12651435Seric **	Parameters:
12661435Seric **		fn -- the filename.
12671435Seric **
12681435Seric **	Returns:
12691435Seric **		a pointer to the tail of the filename; e.g., given
12701435Seric **		"cmd/ls.c", "ls.c" is returned.
12711435Seric **
12721435Seric **	Side Effects:
12731435Seric **		none.
12741435Seric */
12751435Seric 
12761435Seric char *
12771435Seric tail(fn)
12781435Seric 	register char *fn;
12791435Seric {
12801435Seric 	register char *p;
12811435Seric 
12821435Seric 	for (p = fn; *p != 0; p++)
12831435Seric 		if (*p == '/' && p[1] != '\0' && p[1] != '/')
12841435Seric 			fn = &p[1];
12851435Seric 	return (fn);
12861435Seric }
12871435Seric 
12881435Seric /*
12891822Seric **  GETPFENT -- get an entry from the p-file
1290396Seric **
1291396Seric **	Parameters:
1292396Seric **		pfp -- p-file file pointer
1293396Seric **
1294396Seric **	Returns:
1295396Seric **		pointer to p-file struct for next entry
1296396Seric **		NULL on EOF or error
1297396Seric **
1298396Seric **	Side Effects:
1299396Seric **		Each call wipes out results of previous call.
1300396Seric */
1301396Seric 
1302396Seric struct pfile *
13031822Seric getpfent(pfp)
1304396Seric 	FILE *pfp;
1305396Seric {
1306396Seric 	static struct pfile ent;
1307396Seric 	static char buf[120];
1308396Seric 	register char *p;
1309396Seric 	extern char *nextfield();
1310396Seric 
1311396Seric 	if (fgets(buf, sizeof buf, pfp) == NULL)
1312396Seric 		return (NULL);
1313396Seric 
1314396Seric 	ent.p_osid = p = buf;
1315396Seric 	ent.p_nsid = p = nextfield(p);
1316396Seric 	ent.p_user = p = nextfield(p);
1317396Seric 	ent.p_date = p = nextfield(p);
1318396Seric 	ent.p_time = p = nextfield(p);
13192161Seric 	ent.p_aux = p = nextfield(p);
1320396Seric 
1321396Seric 	return (&ent);
1322396Seric }
1323396Seric 
1324396Seric 
1325396Seric char *
1326396Seric nextfield(p)
1327396Seric 	register char *p;
1328396Seric {
1329396Seric 	if (p == NULL || *p == '\0')
1330396Seric 		return (NULL);
1331396Seric 	while (*p != ' ' && *p != '\n' && *p != '\0')
1332396Seric 		p++;
1333396Seric 	if (*p == '\n' || *p == '\0')
1334396Seric 	{
1335396Seric 		*p = '\0';
1336396Seric 		return (NULL);
1337396Seric 	}
1338396Seric 	*p++ = '\0';
1339396Seric 	return (p);
1340396Seric }
13412161Seric /*
13422161Seric **  PUTPFENT -- output a p-file entry to a file
13432161Seric **
13442161Seric **	Parameters:
13452161Seric **		pf -- the p-file entry
13462161Seric **		f -- the file to put it on.
13472161Seric **
13482161Seric **	Returns:
13492161Seric **		none.
13502161Seric **
13512161Seric **	Side Effects:
13522161Seric **		pf is written onto file f.
13532161Seric */
13542161Seric 
13552161Seric putpfent(pf, f)
13562161Seric 	register struct pfile *pf;
13572161Seric 	register FILE *f;
13582161Seric {
13592161Seric 	fprintf(f, "%s %s %s %s %s", pf->p_osid, pf->p_nsid,
13602161Seric 		pf->p_user, pf->p_date, pf->p_time);
13612161Seric 	if (pf->p_aux != NULL)
13622161Seric 		fprintf(f, " %s", pf->p_aux);
13632161Seric 	else
13642161Seric 		fprintf(f, "\n");
13652161Seric }
13661432Seric 
13671432Seric /*
13681205Seric **  USRERR -- issue user-level error
13691205Seric **
13701205Seric **	Parameters:
13711205Seric **		f -- format string.
13721205Seric **		p1-p3 -- parameters to a printf.
13731205Seric **
13741205Seric **	Returns:
13751205Seric **		-1
13761205Seric **
13771205Seric **	Side Effects:
13781205Seric **		none.
13791205Seric */
13801205Seric 
13811738Seric /*VARARGS1*/
13821205Seric usrerr(f, p1, p2, p3)
13831205Seric 	char *f;
13841205Seric {
13851205Seric 	fprintf(stderr, "\n%s: ", MyName);
13861205Seric 	fprintf(stderr, f, p1, p2, p3);
13871205Seric 	fprintf(stderr, "\n");
13881205Seric 
13891205Seric 	return (-1);
13901205Seric }
13911432Seric 
13921432Seric /*
13931205Seric **  SYSERR -- print system-generated error.
13941205Seric **
13951205Seric **	Parameters:
13961205Seric **		f -- format string to a printf.
13971205Seric **		p1, p2, p3 -- parameters to f.
13981205Seric **
13991205Seric **	Returns:
14001205Seric **		never.
14011205Seric **
14021205Seric **	Side Effects:
14031205Seric **		none.
14041205Seric */
14051205Seric 
14061738Seric /*VARARGS1*/
14071205Seric syserr(f, p1, p2, p3)
14081205Seric 	char *f;
14091205Seric {
14101205Seric 	extern int errno;
14111205Seric 
14121205Seric 	fprintf(stderr, "\n%s SYSERR: ", MyName);
14131205Seric 	fprintf(stderr, f, p1, p2, p3);
14141205Seric 	fprintf(stderr, "\n");
14151205Seric 	if (errno == 0)
14161205Seric 		exit(EX_SOFTWARE);
14171205Seric 	else
14181205Seric 	{
14191738Seric 		perror(NULL);
14201205Seric 		exit(EX_OSERR);
14211205Seric 	}
14221205Seric }
14231864Seric /*
14241864Seric **  USERNAME -- return name of the current user
14251864Seric **
14261864Seric **	Parameters:
14271864Seric **		none
14281864Seric **
14291864Seric **	Returns:
14301864Seric **		name of current user
14311864Seric **
14321864Seric **	Side Effects:
14331864Seric **		none
14341864Seric */
14351864Seric 
14361864Seric char *
14371864Seric username()
14381864Seric {
14391864Seric # ifdef UIDUSER
14401864Seric 	extern struct passwd *getpwuid();
14411864Seric 	register struct passwd *pw;
14421864Seric 
14431864Seric 	pw = getpwuid(getuid());
14441864Seric 	if (pw == NULL)
14451864Seric 	{
14461864Seric 		syserr("who are you? (uid=%d)", getuid());
14471864Seric 		exit(EX_OSERR);
14481864Seric 	}
14491864Seric 	return (pw->pw_name);
14501864Seric # else
14511905Seric 	extern char *getlogin();
1452*6785Smckusick 	extern char *getenv();
1453*6785Smckusick 	register char *p;
14541905Seric 
1455*6785Smckusick 	p = getenv("USER");
1456*6785Smckusick 	if (p == NULL || p[0] == '\0')
1457*6785Smckusick 		p = getlogin();
1458*6785Smckusick 	return (p);
14591864Seric # endif UIDUSER
14601864Seric }
1461