xref: /csrg-svn/usr.bin/sccs/sccs.c (revision 6784)
1148Seric # include <stdio.h>
2*6784Smckusick # include <sys/param.h>
3148Seric # include <sys/stat.h>
4*6784Smckusick # 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*6784Smckusick static char SccsId[] = "@(#)sccs.c	1.61.1.2 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
108*6784Smckusick # 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 */
136200Seric 
137157Seric /* bits for sccsflags */
138200Seric # define NO_SDOT	0001	/* no s. on front of args */
139200Seric # define REALUSER	0002	/* protected (e.g., admin) */
140148Seric 
141819Seric /* modes for the "clean", "info", "check" ops */
142819Seric # define CLEANC		0	/* clean command */
143819Seric # define INFOC		1	/* info command */
144819Seric # define CHECKC		2	/* check command */
1451730Seric # define TELLC		3	/* give list of files being edited */
146819Seric 
1471432Seric /*
1481432Seric **  Description of commands known to this program.
1491432Seric **	First argument puts the command into a class.  Second arg is
1501432Seric **	info regarding treatment of this command.  Third arg is a
1511432Seric **	list of flags this command accepts from macros, etc.  Fourth
1521432Seric **	arg is the pathname of the implementing program, or the
1531432Seric **	macro definition, or the arg to a sub-algorithm.
1541432Seric */
155202Seric 
156148Seric struct sccsprog SccsProg[] =
157148Seric {
1581864Seric 	"admin",	PROG,	REALUSER,		PROGPATH(admin),
1591864Seric 	"chghist",	PROG,	0,			PROGPATH(rmdel),
1601864Seric 	"comb",		PROG,	0,			PROGPATH(comb),
1611864Seric 	"delta",	PROG,	0,			PROGPATH(delta),
1621864Seric 	"get",		PROG,	0,			PROGPATH(get),
1631864Seric 	"help",		PROG,	NO_SDOT,		PROGPATH(help),
1642136Seric 	"prs",		PROG,	0,			PROGPATH(prs),
1651864Seric 	"prt",		PROG,	0,			PROGPATH(prt),
1661864Seric 	"rmdel",	PROG,	REALUSER,		PROGPATH(rmdel),
1672136Seric 	"val",		PROG,	0,			PROGPATH(val),
1681864Seric 	"what",		PROG,	NO_SDOT,		PROGPATH(what),
1691864Seric 	"sccsdiff",	SHELL,	REALUSER,		PROGPATH(sccsdiff),
1701864Seric 	"edit",		CMACRO,	NO_SDOT,		"get -e",
1711864Seric 	"delget",	CMACRO,	NO_SDOT,		"delta:mysrp/get:ixbeskcl -t",
1722138Seric 	"deledit",	CMACRO,	NO_SDOT,		"delta:mysrp -n/get:ixbskcl -e -t -g",
1731864Seric 	"fix",		FIX,	NO_SDOT,		NULL,
1741864Seric 	"clean",	CLEAN,	REALUSER|NO_SDOT,	(char *) CLEANC,
1751864Seric 	"info",		CLEAN,	REALUSER|NO_SDOT,	(char *) INFOC,
1761864Seric 	"check",	CLEAN,	REALUSER|NO_SDOT,	(char *) CHECKC,
1771864Seric 	"tell",		CLEAN,	REALUSER|NO_SDOT,	(char *) TELLC,
1781864Seric 	"unedit",	UNEDIT,	NO_SDOT,		NULL,
1791864Seric 	"diffs",	DIFFS,	NO_SDOT|REALUSER,	NULL,
1801871Seric 	"-diff",	DODIFF,	NO_SDOT|REALUSER,	PROGPATH(bdiff),
1812137Seric 	"print",	CMACRO,	0,			"prt -e/get -p -m -s",
1822226Seric 	"branch",	CMACRO,	NO_SDOT,
1832226Seric 		"get:ixrc -e -b/delta: -s -n -ybranch-place-holder/get:pl -e -t -g",
1841864Seric 	NULL,		-1,	0,			NULL
185148Seric };
186148Seric 
1871432Seric /* one line from a p-file */
188396Seric struct pfile
189396Seric {
190396Seric 	char	*p_osid;	/* old SID */
191396Seric 	char	*p_nsid;	/* new SID */
192396Seric 	char	*p_user;	/* user who did edit */
193396Seric 	char	*p_date;	/* date of get */
194396Seric 	char	*p_time;	/* time of get */
1952161Seric 	char	*p_aux;		/* extra info at end */
196396Seric };
197396Seric 
1981270Seric char	*SccsPath = SCCSPATH;	/* pathname of SCCS files */
1991270Seric # ifdef SCCSDIR
2001270Seric char	*SccsDir = SCCSDIR;	/* directory to begin search from */
2011205Seric # else
2021270Seric char	*SccsDir = "";
2031205Seric # endif
2041437Seric char	MyName[] = MYNAME;	/* name used in messages */
2051433Seric int	OutFile = -1;		/* override output file for commands */
206157Seric bool	RealUser;		/* if set, running as real user */
207393Seric # ifdef DEBUG
208393Seric bool	Debug;			/* turn on tracing */
209393Seric # endif
2102139Seric # ifndef V6
2112139Seric extern char	*getenv();
2122139Seric # endif V6
2131432Seric 
214148Seric main(argc, argv)
215148Seric 	int argc;
216148Seric 	char **argv;
217148Seric {
218148Seric 	register char *p;
219262Seric 	extern struct sccsprog *lookup();
2201282Seric 	register int i;
2212139Seric # ifndef V6
2222139Seric # ifndef SCCSDIR
2232140Seric 	register struct passwd *pw;
2242140Seric 	extern struct passwd *getpwnam();
2252140Seric 	char buf[100];
2262140Seric 
2272139Seric 	/* pull "SccsDir" out of the environment (possibly) */
2282139Seric 	p = getenv("PROJECT");
2292140Seric 	if (p != NULL && p[0] != '\0')
2302140Seric 	{
2312140Seric 		if (p[0] == '/')
2322140Seric 			SccsDir = p;
2332140Seric 		else
2342140Seric 		{
2352140Seric 			pw = getpwnam(p);
2362140Seric 			if (pw == NULL)
2372140Seric 			{
2382140Seric 				usrerr("user %s does not exist", p);
2392140Seric 				exit(EX_USAGE);
2402140Seric 			}
2412140Seric 			strcpy(buf, pw->pw_dir);
2422140Seric 			strcat(buf, "/src");
2432140Seric 			if (access(buf, 0) < 0)
2442140Seric 			{
2452140Seric 				strcpy(buf, pw->pw_dir);
2462140Seric 				strcat(buf, "/source");
2472140Seric 				if (access(buf, 0) < 0)
2482140Seric 				{
2492140Seric 					usrerr("project %s has no source!", p);
2502140Seric 					exit(EX_USAGE);
2512140Seric 				}
2522140Seric 			}
2532140Seric 			SccsDir = buf;
2542140Seric 		}
2552140Seric 	}
2562139Seric # endif SCCSDIR
2572139Seric # endif V6
2582139Seric 
259148Seric 	/*
260148Seric 	**  Detect and decode flags intended for this program.
261148Seric 	*/
262148Seric 
263200Seric 	if (argc < 2)
264148Seric 	{
2651205Seric 		fprintf(stderr, "Usage: %s [flags] command [flags]\n", MyName);
266200Seric 		exit(EX_USAGE);
267200Seric 	}
268200Seric 	argv[argc] = NULL;
269200Seric 
270262Seric 	if (lookup(argv[0]) == NULL)
271200Seric 	{
272262Seric 		while ((p = *++argv) != NULL)
273148Seric 		{
274262Seric 			if (*p != '-')
275262Seric 				break;
276262Seric 			switch (*++p)
277262Seric 			{
278262Seric 			  case 'r':		/* run as real user */
279262Seric 				setuid(getuid());
280262Seric 				RealUser++;
281262Seric 				break;
282148Seric 
2831270Seric # ifndef SCCSDIR
284262Seric 			  case 'p':		/* path of sccs files */
285262Seric 				SccsPath = ++p;
2862348Seric 				if (SccsPath[0] == '\0' && argv[1] != NULL)
2872348Seric 					SccsPath = *++argv;
288262Seric 				break;
289148Seric 
290588Seric 			  case 'd':		/* directory to search from */
291588Seric 				SccsDir = ++p;
2922348Seric 				if (SccsDir[0] == '\0' && argv[1] != NULL)
2932348Seric 					SccsDir = *++argv;
294588Seric 				break;
2951205Seric # endif
296588Seric 
297393Seric # ifdef DEBUG
298393Seric 			  case 'T':		/* trace */
299393Seric 				Debug++;
300393Seric 				break;
301393Seric # endif
302393Seric 
303262Seric 			  default:
3041205Seric 				usrerr("unknown option -%s", p);
305262Seric 				break;
306262Seric 			}
307148Seric 		}
308262Seric 		if (SccsPath[0] == '\0')
309262Seric 			SccsPath = ".";
310148Seric 	}
311148Seric 
3121737Seric 	i = command(argv, FALSE, "");
3131282Seric 	exit(i);
314200Seric }
3151432Seric 
3161432Seric /*
3171282Seric **  COMMAND -- look up and perform a command
3181282Seric **
3191282Seric **	This routine is the guts of this program.  Given an
3201282Seric **	argument vector, it looks up the "command" (argv[0])
3211282Seric **	in the configuration table and does the necessary stuff.
3221282Seric **
3231282Seric **	Parameters:
3241282Seric **		argv -- an argument vector to process.
3251282Seric **		forkflag -- if set, fork before executing the command.
3261316Seric **		editflag -- if set, only include flags listed in the
3271316Seric **			sccsklets field of the command descriptor.
3281316Seric **		arg0 -- a space-seperated list of arguments to insert
3291316Seric **			before argv.
3301282Seric **
3311282Seric **	Returns:
3321282Seric **		zero -- command executed ok.
3331282Seric **		else -- error status.
3341282Seric **
3351282Seric **	Side Effects:
3361282Seric **		none.
3371282Seric */
338157Seric 
3391737Seric command(argv, forkflag, arg0)
340200Seric 	char **argv;
341201Seric 	bool forkflag;
3421316Seric 	char *arg0;
343200Seric {
344200Seric 	register struct sccsprog *cmd;
345200Seric 	register char *p;
3466783Smckusick 	char buf[40];
347262Seric 	extern struct sccsprog *lookup();
3481316Seric 	char *nav[1000];
3491316Seric 	char **np;
3501431Seric 	register char **ap;
351585Seric 	register int i;
3521431Seric 	register char *q;
353585Seric 	extern bool unedit();
3541282Seric 	int rval = 0;
3551316Seric 	extern char *index();
3561316Seric 	extern char *makefile();
3571737Seric 	char *editchs;
3581435Seric 	extern char *tail();
359200Seric 
360393Seric # ifdef DEBUG
361393Seric 	if (Debug)
362393Seric 	{
3631316Seric 		printf("command:\n\t\"%s\"\n", arg0);
3641316Seric 		for (np = argv; *np != NULL; np++)
3651316Seric 			printf("\t\"%s\"\n", *np);
366393Seric 	}
367393Seric # endif
368393Seric 
369157Seric 	/*
3701316Seric 	**  Copy arguments.
3711438Seric 	**	Copy from arg0 & if necessary at most one arg
3721438Seric 	**	from argv[0].
3731316Seric 	*/
3741316Seric 
3751431Seric 	np = ap = &nav[1];
3761737Seric 	editchs = NULL;
3771821Seric 	for (p = arg0, q = buf; *p != '\0' && *p != '/'; )
3781316Seric 	{
3791316Seric 		*np++ = q;
3801316Seric 		while (*p == ' ')
3811316Seric 			p++;
3821737Seric 		while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':')
3831316Seric 			*q++ = *p++;
3841316Seric 		*q++ = '\0';
3851737Seric 		if (*p == ':')
3861737Seric 		{
3871737Seric 			editchs = q;
3881821Seric 			while (*++p != '\0' && *p != '/' && *p != ' ')
3891737Seric 				*q++ = *p;
3901737Seric 			*q++ = '\0';
3911737Seric 		}
3921316Seric 	}
3931316Seric 	*np = NULL;
3941431Seric 	if (*ap == NULL)
3951316Seric 		*np++ = *argv++;
3961316Seric 
3971316Seric 	/*
398148Seric 	**  Look up command.
3991431Seric 	**	At this point, *ap is the command name.
400148Seric 	*/
401148Seric 
4021431Seric 	cmd = lookup(*ap);
403262Seric 	if (cmd == NULL)
404148Seric 	{
4051431Seric 		usrerr("Unknown command \"%s\"", *ap);
4061282Seric 		return (EX_USAGE);
407148Seric 	}
408148Seric 
409148Seric 	/*
4101316Seric 	**  Copy remaining arguments doing editing as appropriate.
4111316Seric 	*/
4121316Seric 
4131316Seric 	for (; *argv != NULL; argv++)
4141316Seric 	{
4151316Seric 		p = *argv;
4161316Seric 		if (*p == '-')
4171316Seric 		{
4181737Seric 			if (p[1] == '\0' || editchs == NULL || index(editchs, p[1]) != NULL)
4191316Seric 				*np++ = p;
4201316Seric 		}
4211316Seric 		else
4221316Seric 		{
4231316Seric 			if (!bitset(NO_SDOT, cmd->sccsflags))
4241316Seric 				p = makefile(p);
4251316Seric 			if (p != NULL)
4261316Seric 				*np++ = p;
4271316Seric 		}
4281316Seric 	}
4291316Seric 	*np = NULL;
4301316Seric 
4311316Seric 	/*
432200Seric 	**  Interpret operation associated with this command.
433157Seric 	*/
434157Seric 
435200Seric 	switch (cmd->sccsoper)
436200Seric 	{
4371431Seric 	  case SHELL:		/* call a shell file */
4381431Seric 		*ap = cmd->sccspath;
4391431Seric 		*--ap = "sh";
4401431Seric 		rval = callprog("/bin/sh", cmd->sccsflags, ap, forkflag);
4411431Seric 		break;
4421431Seric 
443200Seric 	  case PROG:		/* call an sccs prog */
4441431Seric 		rval = callprog(cmd->sccspath, cmd->sccsflags, ap, forkflag);
445201Seric 		break;
446201Seric 
447201Seric 	  case CMACRO:		/* command macro */
4481438Seric 		/* step through & execute each part of the macro */
449201Seric 		for (p = cmd->sccspath; *p != '\0'; p++)
450201Seric 		{
4511316Seric 			q = p;
4521316Seric 			while (*p != '\0' && *p != '/')
4531316Seric 				p++;
4541737Seric 			rval = command(&ap[1], *p != '\0', q);
4551282Seric 			if (rval != 0)
4561282Seric 				break;
457201Seric 		}
4581282Seric 		break;
459157Seric 
460226Seric 	  case FIX:		/* fix a delta */
4611431Seric 		if (strncmp(ap[1], "-r", 2) != 0)
462226Seric 		{
4631205Seric 			usrerr("-r flag needed for fix command");
4641282Seric 			rval = EX_USAGE;
465226Seric 			break;
466226Seric 		}
4671438Seric 
4681438Seric 		/* get the version with all changes */
4691737Seric 		rval = command(&ap[1], TRUE, "get -k");
4701438Seric 
4711438Seric 		/* now remove that version from the s-file */
4721282Seric 		if (rval == 0)
4731737Seric 			rval = command(&ap[1], TRUE, "rmdel:r");
4741438Seric 
4751438Seric 		/* and edit the old version (but don't clobber new vers) */
4761282Seric 		if (rval == 0)
4771737Seric 			rval = command(&ap[2], FALSE, "get -e -g");
4781282Seric 		break;
479226Seric 
480261Seric 	  case CLEAN:
4811822Seric 		rval = clean((int) cmd->sccspath, ap);
482261Seric 		break;
483261Seric 
484396Seric 	  case UNEDIT:
4851431Seric 		for (argv = np = &ap[1]; *argv != NULL; argv++)
486585Seric 		{
4871316Seric 			if (unedit(*argv))
4881316Seric 				*np++ = *argv;
489585Seric 		}
4901316Seric 		*np = NULL;
4911438Seric 
4921438Seric 		/* get all the files that we unedited successfully */
4931738Seric 		if (np > &ap[1])
4941737Seric 			rval = command(&ap[1], FALSE, "get");
495396Seric 		break;
496396Seric 
4971433Seric 	  case DIFFS:		/* diff between s-file & edit file */
4981433Seric 		/* find the end of the flag arguments */
4991433Seric 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
5001433Seric 			continue;
5011433Seric 		argv = np;
5021433Seric 
5031433Seric 		/* for each file, do the diff */
5041502Seric 		p = argv[1];
5051433Seric 		while (*np != NULL)
5061433Seric 		{
5071438Seric 			/* messy, but we need a null terminated argv */
5081433Seric 			*argv = *np++;
5091502Seric 			argv[1] = NULL;
5101435Seric 			i = dodiff(ap, tail(*argv));
5111433Seric 			if (rval == 0)
5121433Seric 				rval = i;
5131502Seric 			argv[1] = p;
5141433Seric 		}
5151433Seric 		break;
5161433Seric 
5171871Seric 	  case DODIFF:		/* internal diff call */
5181871Seric 		setuid(getuid());
5191871Seric 		for (np = ap; *np != NULL; np++)
5201871Seric 		{
5211871Seric 			if ((*np)[0] == '-' && (*np)[1] == 'C')
5221871Seric 				(*np)[1] = 'c';
5231871Seric 		}
5241871Seric 
5251871Seric 		/* insert "-" argument */
5261871Seric 		np[1] = NULL;
5271871Seric 		np[0] = np[-1];
5281871Seric 		np[-1] = "-";
5291871Seric 
5301871Seric 		/* execute the diff program of choice */
5311871Seric # ifndef V6
5321871Seric 		execvp("diff", ap);
5331871Seric # endif
5341871Seric 		execv(cmd->sccspath, argv);
5351871Seric 		syserr("cannot exec %s", cmd->sccspath);
5361871Seric 		exit(EX_OSERR);
5371871Seric 
538200Seric 	  default:
5391205Seric 		syserr("oper %d", cmd->sccsoper);
540200Seric 		exit(EX_SOFTWARE);
541200Seric 	}
5421282Seric # ifdef DEBUG
5431282Seric 	if (Debug)
5441282Seric 		printf("command: rval=%d\n", rval);
5451282Seric # endif
5461282Seric 	return (rval);
547200Seric }
5481432Seric 
5491432Seric /*
550262Seric **  LOOKUP -- look up an SCCS command name.
551262Seric **
552262Seric **	Parameters:
553262Seric **		name -- the name of the command to look up.
554262Seric **
555262Seric **	Returns:
556262Seric **		ptr to command descriptor for this command.
557262Seric **		NULL if no such entry.
558262Seric **
559262Seric **	Side Effects:
560262Seric **		none.
561262Seric */
562200Seric 
563262Seric struct sccsprog *
564262Seric lookup(name)
565262Seric 	char *name;
566262Seric {
567262Seric 	register struct sccsprog *cmd;
568226Seric 
569262Seric 	for (cmd = SccsProg; cmd->sccsname != NULL; cmd++)
570262Seric 	{
571262Seric 		if (strcmp(cmd->sccsname, name) == 0)
572262Seric 			return (cmd);
573262Seric 	}
574262Seric 	return (NULL);
575262Seric }
5761432Seric 
5771432Seric /*
5781282Seric **  CALLPROG -- call a program
5791282Seric **
5801316Seric **	Used to call the SCCS programs.
5811282Seric **
5821282Seric **	Parameters:
5831282Seric **		progpath -- pathname of the program to call.
5841282Seric **		flags -- status flags from the command descriptors.
5851282Seric **		argv -- an argument vector to pass to the program.
5861282Seric **		forkflag -- if true, fork before calling, else just
5871282Seric **			exec.
5881282Seric **
5891282Seric **	Returns:
5901282Seric **		The exit status of the program.
5911282Seric **		Nothing if forkflag == FALSE.
5921282Seric **
5931282Seric **	Side Effects:
5941282Seric **		Can exit if forkflag == FALSE.
5951282Seric */
596226Seric 
597200Seric callprog(progpath, flags, argv, forkflag)
598200Seric 	char *progpath;
599200Seric 	short flags;
600200Seric 	char **argv;
601200Seric 	bool forkflag;
602200Seric {
603200Seric 	register int i;
604201Seric 	auto int st;
605200Seric 
6061316Seric # ifdef DEBUG
6071316Seric 	if (Debug)
6081316Seric 	{
6091316Seric 		printf("callprog:\n");
6101316Seric 		for (i = 0; argv[i] != NULL; i++)
6111316Seric 			printf("\t\"%s\"\n", argv[i]);
6121316Seric 	}
6131316Seric # endif
6141316Seric 
615200Seric 	if (*argv == NULL)
616200Seric 		return (-1);
617200Seric 
618157Seric 	/*
619226Seric 	**  Fork if appropriate.
620148Seric 	*/
621148Seric 
622200Seric 	if (forkflag)
623200Seric 	{
624393Seric # ifdef DEBUG
625393Seric 		if (Debug)
626393Seric 			printf("Forking\n");
627393Seric # endif
628200Seric 		i = fork();
629200Seric 		if (i < 0)
630200Seric 		{
6311205Seric 			syserr("cannot fork");
632200Seric 			exit(EX_OSERR);
633200Seric 		}
634200Seric 		else if (i > 0)
635201Seric 		{
636201Seric 			wait(&st);
6371282Seric 			if ((st & 0377) == 0)
6381282Seric 				st = (st >> 8) & 0377;
6391433Seric 			if (OutFile >= 0)
6401433Seric 			{
6411433Seric 				close(OutFile);
6421433Seric 				OutFile = -1;
6431433Seric 			}
644201Seric 			return (st);
645201Seric 		}
646200Seric 	}
6471433Seric 	else if (OutFile >= 0)
6481433Seric 	{
6491433Seric 		syserr("callprog: setting stdout w/o forking");
6501433Seric 		exit(EX_SOFTWARE);
6511433Seric 	}
652200Seric 
6531433Seric 	/* set protection as appropriate */
654200Seric 	if (bitset(REALUSER, flags))
655200Seric 		setuid(getuid());
6561433Seric 
6571433Seric 	/* change standard input & output if needed */
6581433Seric 	if (OutFile >= 0)
6591433Seric 	{
6601433Seric 		close(1);
6611433Seric 		dup(OutFile);
6621433Seric 		close(OutFile);
6631433Seric 	}
664226Seric 
6651433Seric 	/* call real SCCS program */
666226Seric 	execv(progpath, argv);
6671205Seric 	syserr("cannot execute %s", progpath);
668148Seric 	exit(EX_UNAVAILABLE);
6691738Seric 	/*NOTREACHED*/
670148Seric }
6711432Seric 
6721432Seric /*
673586Seric **  MAKEFILE -- make filename of SCCS file
674586Seric **
675586Seric **	If the name passed is already the name of an SCCS file,
676586Seric **	just return it.  Otherwise, munge the name into the name
677586Seric **	of the actual SCCS file.
678586Seric **
679586Seric **	There are cases when it is not clear what you want to
680586Seric **	do.  For example, if SccsPath is an absolute pathname
681586Seric **	and the name given is also an absolute pathname, we go
682586Seric **	for SccsPath (& only use the last component of the name
683586Seric **	passed) -- this is important for security reasons (if
684586Seric **	sccs is being used as a setuid front end), but not
685586Seric **	particularly intuitive.
686586Seric **
687586Seric **	Parameters:
688586Seric **		name -- the file name to be munged.
689586Seric **
690586Seric **	Returns:
691586Seric **		The pathname of the sccs file.
692586Seric **		NULL on error.
693586Seric **
694586Seric **	Side Effects:
695586Seric **		none.
696586Seric */
697148Seric 
698148Seric char *
699148Seric makefile(name)
700148Seric 	char *name;
701148Seric {
702148Seric 	register char *p;
703148Seric 	char buf[512];
704148Seric 	extern char *malloc();
705586Seric 	extern char *rindex();
706588Seric 	extern bool safepath();
707587Seric 	extern bool isdir();
708587Seric 	register char *q;
709148Seric 
710586Seric 	p = rindex(name, '/');
711586Seric 	if (p == NULL)
712586Seric 		p = name;
713586Seric 	else
714586Seric 		p++;
715586Seric 
716148Seric 	/*
717588Seric 	**  Check to see that the path is "safe", i.e., that we
718588Seric 	**  are not letting some nasty person use the setuid part
719588Seric 	**  of this program to look at or munge some presumably
720588Seric 	**  hidden files.
721148Seric 	*/
722148Seric 
723588Seric 	if (SccsDir[0] == '/' && !safepath(name))
724588Seric 		return (NULL);
725586Seric 
726586Seric 	/*
727588Seric 	**  Create the base pathname.
728586Seric 	*/
729586Seric 
7301438Seric 	/* first the directory part */
731588Seric 	if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0)
732148Seric 	{
733588Seric 		strcpy(buf, SccsDir);
734586Seric 		strcat(buf, "/");
735586Seric 	}
736586Seric 	else
737586Seric 		strcpy(buf, "");
7381438Seric 
7391438Seric 	/* then the head of the pathname */
740587Seric 	strncat(buf, name, p - name);
741587Seric 	q = &buf[strlen(buf)];
7421438Seric 
7431438Seric 	/* now copy the final part of the name, in case useful */
744587Seric 	strcpy(q, p);
7451438Seric 
7461438Seric 	/* so is it useful? */
747587Seric 	if (strncmp(p, "s.", 2) != 0 && !isdir(buf))
748586Seric 	{
7491438Seric 		/* sorry, no; copy the SCCS pathname & the "s." */
750588Seric 		strcpy(q, SccsPath);
751588Seric 		strcat(buf, "/s.");
7521438Seric 
7531438Seric 		/* and now the end of the name */
754586Seric 		strcat(buf, p);
755586Seric 	}
756148Seric 
7571438Seric 	/* if i haven't changed it, why did I do all this? */
758588Seric 	if (strcmp(buf, name) == 0)
759588Seric 		p = name;
760588Seric 	else
761148Seric 	{
7621438Seric 		/* but if I have, squirrel it away */
763588Seric 		p = malloc(strlen(buf) + 1);
764588Seric 		if (p == NULL)
765588Seric 		{
766588Seric 			perror("Sccs: no mem");
767588Seric 			exit(EX_OSERR);
768588Seric 		}
769588Seric 		strcpy(p, buf);
770148Seric 	}
7711438Seric 
772148Seric 	return (p);
773148Seric }
7741432Seric 
7751432Seric /*
776587Seric **  ISDIR -- return true if the argument is a directory.
777587Seric **
778587Seric **	Parameters:
779587Seric **		name -- the pathname of the file to check.
780587Seric **
781587Seric **	Returns:
782587Seric **		TRUE if 'name' is a directory, FALSE otherwise.
783587Seric **
784587Seric **	Side Effects:
785587Seric **		none.
786587Seric */
787587Seric 
788587Seric bool
789587Seric isdir(name)
790587Seric 	char *name;
791587Seric {
792587Seric 	struct stat stbuf;
793587Seric 
794587Seric 	return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR);
795587Seric }
7961432Seric 
7971432Seric /*
798586Seric **  SAFEPATH -- determine whether a pathname is "safe"
799586Seric **
800586Seric **	"Safe" pathnames only allow you to get deeper into the
801586Seric **	directory structure, i.e., full pathnames and ".." are
802586Seric **	not allowed.
803586Seric **
804586Seric **	Parameters:
805586Seric **		p -- the name to check.
806586Seric **
807586Seric **	Returns:
808586Seric **		TRUE -- if the path is safe.
809586Seric **		FALSE -- if the path is not safe.
810586Seric **
811586Seric **	Side Effects:
812586Seric **		Prints a message if the path is not safe.
813586Seric */
814586Seric 
815586Seric bool
816586Seric safepath(p)
817586Seric 	register char *p;
818586Seric {
819586Seric 	extern char *index();
820586Seric 
821586Seric 	if (*p != '/')
822586Seric 	{
823586Seric 		while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0)
824586Seric 		{
825586Seric 			p = index(p, '/');
826586Seric 			if (p == NULL)
827586Seric 				return (TRUE);
828586Seric 			p++;
829586Seric 		}
830586Seric 	}
831586Seric 
832586Seric 	printf("You may not use full pathnames or \"..\"\n");
833586Seric 	return (FALSE);
834586Seric }
8351432Seric 
8361432Seric /*
837261Seric **  CLEAN -- clean out recreatable files
838261Seric **
839261Seric **	Any file for which an "s." file exists but no "p." file
840261Seric **	exists in the current directory is purged.
841261Seric **
842261Seric **	Parameters:
8431822Seric **		mode -- tells whether this came from a "clean", "info", or
8441822Seric **			"check" command.
8451822Seric **		argv -- the rest of the argument vector.
846261Seric **
847261Seric **	Returns:
848261Seric **		none.
849261Seric **
850261Seric **	Side Effects:
851819Seric **		Removes files in the current directory.
852819Seric **		Prints information regarding files being edited.
853819Seric **		Exits if a "check" command.
854261Seric */
855261Seric 
8561822Seric clean(mode, argv)
857819Seric 	int mode;
8581822Seric 	char **argv;
859261Seric {
860*6784Smckusick 	struct direct *dir;
861261Seric 	char buf[100];
8622140Seric 	char *bufend;
863*6784Smckusick 	register DIR *dirfd;
864346Seric 	register char *basefile;
865351Seric 	bool gotedit;
8661822Seric 	bool gotpfent;
867394Seric 	FILE *pfp;
8681822Seric 	bool nobranch = FALSE;
8691822Seric 	extern struct pfile *getpfent();
8701822Seric 	register struct pfile *pf;
8711822Seric 	register char **ap;
8721864Seric 	extern char *username();
8731864Seric 	char *usernm = NULL;
8742140Seric 	char *subdir = NULL;
8752140Seric 	char *cmdname;
876261Seric 
8771438Seric 	/*
8781822Seric 	**  Process the argv
8791822Seric 	*/
8801822Seric 
8812140Seric 	cmdname = *argv;
8822140Seric 	for (ap = argv; *++ap != NULL; )
8831822Seric 	{
8841864Seric 		if (**ap == '-')
8851864Seric 		{
8861864Seric 			/* we have a flag */
8871864Seric 			switch ((*ap)[1])
8881864Seric 			{
8891864Seric 			  case 'b':
8901864Seric 				nobranch = TRUE;
8911864Seric 				break;
8921864Seric 
8931864Seric 			  case 'u':
8941864Seric 				if ((*ap)[2] != '\0')
8951864Seric 					usernm = &(*ap)[2];
8961864Seric 				else if (ap[1] != NULL && ap[1][0] != '-')
8971864Seric 					usernm = *++ap;
8981864Seric 				else
8991864Seric 					usernm = username();
9001864Seric 				break;
9011864Seric 			}
9021864Seric 		}
9032140Seric 		else
9042140Seric 		{
9052140Seric 			if (subdir != NULL)
9062140Seric 				usrerr("too many args");
9072140Seric 			else
9082140Seric 				subdir = *ap;
9092140Seric 		}
9101822Seric 	}
9111822Seric 
9121822Seric 	/*
9131438Seric 	**  Find and open the SCCS directory.
9141438Seric 	*/
9151438Seric 
9161207Seric 	strcpy(buf, SccsDir);
9171207Seric 	if (buf[0] != '\0')
9181207Seric 		strcat(buf, "/");
9192140Seric 	if (subdir != NULL)
9202140Seric 	{
9212140Seric 		strcat(buf, subdir);
9222140Seric 		strcat(buf, "/");
9232140Seric 	}
9241207Seric 	strcat(buf, SccsPath);
9252140Seric 	bufend = &buf[strlen(buf)];
9261438Seric 
927*6784Smckusick 	dirfd = opendir(buf);
928261Seric 	if (dirfd == NULL)
929261Seric 	{
9301207Seric 		usrerr("cannot open %s", buf);
9311282Seric 		return (EX_NOINPUT);
932261Seric 	}
933261Seric 
934261Seric 	/*
935261Seric 	**  Scan the SCCS directory looking for s. files.
9361438Seric 	**	gotedit tells whether we have tried to clean any
9371438Seric 	**		files that are being edited.
938261Seric 	*/
939261Seric 
940351Seric 	gotedit = FALSE;
941*6784Smckusick 	while (dir = readdir(dirfd)) {
942*6784Smckusick 		if (strncmp(dir->d_name, "s.", 2) != 0)
943261Seric 			continue;
944261Seric 
945261Seric 		/* got an s. file -- see if the p. file exists */
9462140Seric 		strcpy(bufend, "/p.");
9472140Seric 		basefile = bufend + 3;
948*6784Smckusick 		strcpy(basefile, &dir->d_name[2]);
9491822Seric 
9501822Seric 		/*
9511822Seric 		**  open and scan the p-file.
9521822Seric 		**	'gotpfent' tells if we have found a valid p-file
9531822Seric 		**		entry.
9541822Seric 		*/
9551822Seric 
956394Seric 		pfp = fopen(buf, "r");
9571822Seric 		gotpfent = FALSE;
958394Seric 		if (pfp != NULL)
959346Seric 		{
9601438Seric 			/* the file exists -- report it's contents */
9611822Seric 			while ((pf = getpfent(pfp)) != NULL)
9621730Seric 			{
9631822Seric 				if (nobranch && isbranch(pf->p_nsid))
9641822Seric 					continue;
9651864Seric 				if (usernm != NULL && strcmp(usernm, pf->p_user) != 0 && mode != CLEANC)
9661864Seric 					continue;
9671822Seric 				gotedit = TRUE;
9681822Seric 				gotpfent = TRUE;
9691822Seric 				if (mode == TELLC)
9701822Seric 				{
9711822Seric 					printf("%s\n", basefile);
9721822Seric 					break;
9731822Seric 				}
9742161Seric 				printf("%12s: being edited: ", basefile);
9752161Seric 				putpfent(pf, stdout);
9761730Seric 			}
977394Seric 			fclose(pfp);
9781822Seric 		}
979261Seric 
980261Seric 		/* the s. file exists and no p. file exists -- unlink the g-file */
9811870Seric 		if (mode == CLEANC && !gotpfent)
982346Seric 		{
983*6784Smckusick 			strcpy(buf, &dir->d_name[2]);
984346Seric 			unlink(buf);
985346Seric 		}
986261Seric 	}
987261Seric 
9881438Seric 	/* cleanup & report results */
989*6784Smckusick 	closedir(dirfd);
990819Seric 	if (!gotedit && mode == INFOC)
9911864Seric 	{
9921864Seric 		printf("Nothing being edited");
9931864Seric 		if (nobranch)
9941864Seric 			printf(" (on trunk)");
9951864Seric 		if (usernm == NULL)
9961864Seric 			printf("\n");
9971864Seric 		else
9981864Seric 			printf(" by %s\n", usernm);
9991864Seric 	}
1000819Seric 	if (mode == CHECKC)
1001819Seric 		exit(gotedit);
10021282Seric 	return (EX_OK);
1003261Seric }
10041432Seric 
10051432Seric /*
10061822Seric **  ISBRANCH -- is the SID a branch?
10071822Seric **
10081822Seric **	Parameters:
10091822Seric **		sid -- the sid to check.
10101822Seric **
10111822Seric **	Returns:
10121822Seric **		TRUE if the sid represents a branch.
10131822Seric **		FALSE otherwise.
10141822Seric **
10151822Seric **	Side Effects:
10161822Seric **		none.
10171822Seric */
10181822Seric 
10191822Seric isbranch(sid)
10201822Seric 	char *sid;
10211822Seric {
10221822Seric 	register char *p;
10231822Seric 	int dots;
10241822Seric 
10251822Seric 	dots = 0;
10261822Seric 	for (p = sid; *p != '\0'; p++)
10271822Seric 	{
10281822Seric 		if (*p == '.')
10291822Seric 			dots++;
10301822Seric 		if (dots > 1)
10311822Seric 			return (TRUE);
10321822Seric 	}
10331822Seric 	return (FALSE);
10341822Seric }
10351822Seric 
10361822Seric /*
1037396Seric **  UNEDIT -- unedit a file
1038396Seric **
1039396Seric **	Checks to see that the current user is actually editting
1040396Seric **	the file and arranges that s/he is not editting it.
1041396Seric **
1042396Seric **	Parameters:
1043416Seric **		fn -- the name of the file to be unedited.
1044396Seric **
1045396Seric **	Returns:
1046585Seric **		TRUE -- if the file was successfully unedited.
1047585Seric **		FALSE -- if the file was not unedited for some
1048585Seric **			reason.
1049396Seric **
1050396Seric **	Side Effects:
1051396Seric **		fn is removed
1052396Seric **		entries are removed from pfile.
1053396Seric */
1054396Seric 
1055585Seric bool
1056396Seric unedit(fn)
1057396Seric 	char *fn;
1058396Seric {
1059396Seric 	register FILE *pfp;
1060396Seric 	char *pfn;
1061396Seric 	static char tfn[] = "/tmp/sccsXXXXX";
1062396Seric 	FILE *tfp;
1063396Seric 	register char *q;
1064396Seric 	bool delete = FALSE;
1065396Seric 	bool others = FALSE;
1066396Seric 	char *myname;
10671864Seric 	extern char *username();
1068396Seric 	struct pfile *pent;
10691822Seric 	extern struct pfile *getpfent();
1070396Seric 	char buf[120];
10711316Seric 	extern char *makefile();
1072396Seric 
1073396Seric 	/* make "s." filename & find the trailing component */
1074396Seric 	pfn = makefile(fn);
1075586Seric 	if (pfn == NULL)
1076586Seric 		return (FALSE);
1077586Seric 	q = rindex(pfn, '/');
1078586Seric 	if (q == NULL)
1079586Seric 		q = &pfn[-1];
1080586Seric 	if (q[1] != 's' || q[2] != '.')
1081396Seric 	{
10821205Seric 		usrerr("bad file name \"%s\"", fn);
1083585Seric 		return (FALSE);
1084396Seric 	}
1085396Seric 
10861438Seric 	/* turn "s." into "p." & try to open it */
1087396Seric 	*++q = 'p';
1088396Seric 
1089396Seric 	pfp = fopen(pfn, "r");
1090396Seric 	if (pfp == NULL)
1091396Seric 	{
1092416Seric 		printf("%12s: not being edited\n", fn);
1093585Seric 		return (FALSE);
1094396Seric 	}
1095396Seric 
10961438Seric 	/* create temp file for editing p-file */
1097396Seric 	mktemp(tfn);
1098396Seric 	tfp = fopen(tfn, "w");
1099396Seric 	if (tfp == NULL)
1100396Seric 	{
11011205Seric 		usrerr("cannot create \"%s\"", tfn);
1102396Seric 		exit(EX_OSERR);
1103396Seric 	}
1104396Seric 
11051438Seric 	/* figure out who I am */
11061864Seric 	myname = username();
11071438Seric 
11081438Seric 	/*
11091438Seric 	**  Copy p-file to temp file, doing deletions as needed.
11101438Seric 	*/
11111438Seric 
11121822Seric 	while ((pent = getpfent(pfp)) != NULL)
1113396Seric 	{
1114396Seric 		if (strcmp(pent->p_user, myname) == 0)
1115396Seric 		{
1116396Seric 			/* a match */
1117396Seric 			delete++;
1118396Seric 		}
1119396Seric 		else
1120396Seric 		{
11211438Seric 			/* output it again */
11222161Seric 			putpfent(pent, tfp);
1123396Seric 			others++;
1124396Seric 		}
1125396Seric 	}
1126396Seric 
1127396Seric 	/* do final cleanup */
1128396Seric 	if (others)
1129396Seric 	{
11301438Seric 		/* copy it back (perhaps it should be linked?) */
1131396Seric 		if (freopen(tfn, "r", tfp) == NULL)
1132396Seric 		{
11331205Seric 			syserr("cannot reopen \"%s\"", tfn);
1134396Seric 			exit(EX_OSERR);
1135396Seric 		}
1136396Seric 		if (freopen(pfn, "w", pfp) == NULL)
1137396Seric 		{
11381205Seric 			usrerr("cannot create \"%s\"", pfn);
1139585Seric 			return (FALSE);
1140396Seric 		}
1141396Seric 		while (fgets(buf, sizeof buf, tfp) != NULL)
1142396Seric 			fputs(buf, pfp);
1143396Seric 	}
1144396Seric 	else
1145396Seric 	{
11461438Seric 		/* it's empty -- remove it */
1147396Seric 		unlink(pfn);
1148396Seric 	}
1149396Seric 	fclose(tfp);
1150396Seric 	fclose(pfp);
1151396Seric 	unlink(tfn);
1152396Seric 
11531438Seric 	/* actually remove the g-file */
1154396Seric 	if (delete)
1155396Seric 	{
11561435Seric 		unlink(tail(fn));
11571435Seric 		printf("%12s: removed\n", tail(fn));
1158585Seric 		return (TRUE);
1159396Seric 	}
1160396Seric 	else
1161396Seric 	{
1162416Seric 		printf("%12s: not being edited by you\n", fn);
1163585Seric 		return (FALSE);
1164396Seric 	}
1165396Seric }
11661432Seric 
11671432Seric /*
11681433Seric **  DODIFF -- diff an s-file against a g-file
11691433Seric **
11701433Seric **	Parameters:
11711433Seric **		getv -- argv for the 'get' command.
11721433Seric **		gfile -- name of the g-file to diff against.
11731433Seric **
11741433Seric **	Returns:
11751433Seric **		Result of get.
11761433Seric **
11771433Seric **	Side Effects:
11781433Seric **		none.
11791433Seric */
11801433Seric 
11811433Seric dodiff(getv, gfile)
11821433Seric 	char **getv;
11831433Seric 	char *gfile;
11841433Seric {
11851433Seric 	int pipev[2];
11861433Seric 	int rval;
11871433Seric 	register int i;
11881433Seric 	register int pid;
11891433Seric 	auto int st;
11901433Seric 	extern int errno;
11911433Seric 	int (*osig)();
11921433Seric 
11931905Seric 	printf("\n------- %s -------\n", gfile);
11941871Seric 	fflush(stdout);
11951501Seric 
11961438Seric 	/* create context for diff to run in */
11971433Seric 	if (pipe(pipev) < 0)
11981433Seric 	{
11991433Seric 		syserr("dodiff: pipe failed");
12001433Seric 		exit(EX_OSERR);
12011433Seric 	}
12021433Seric 	if ((pid = fork()) < 0)
12031433Seric 	{
12041433Seric 		syserr("dodiff: fork failed");
12051433Seric 		exit(EX_OSERR);
12061433Seric 	}
12071433Seric 	else if (pid > 0)
12081433Seric 	{
12091433Seric 		/* in parent; run get */
12101433Seric 		OutFile = pipev[1];
12111433Seric 		close(pipev[0]);
12121871Seric 		rval = command(&getv[1], TRUE, "get:rcixt -s -k -p");
12131433Seric 		osig = signal(SIGINT, SIG_IGN);
12141433Seric 		while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR)
12151433Seric 			errno = 0;
12161433Seric 		signal(SIGINT, osig);
12171433Seric 		/* ignore result of diff */
12181433Seric 	}
12191433Seric 	else
12201433Seric 	{
12211433Seric 		/* in child, run diff */
12221433Seric 		if (close(pipev[1]) < 0 || close(0) < 0 ||
12231433Seric 		    dup(pipev[0]) != 0 || close(pipev[0]) < 0)
12241433Seric 		{
12251433Seric 			syserr("dodiff: magic failed");
12261433Seric 			exit(EX_OSERR);
12271433Seric 		}
12281871Seric 		command(&getv[1], FALSE, "-diff:elsfhbC");
12291433Seric 	}
12301433Seric 	return (rval);
12311433Seric }
12321433Seric 
12331433Seric /*
12341435Seric **  TAIL -- return tail of filename.
12351435Seric **
12361435Seric **	Parameters:
12371435Seric **		fn -- the filename.
12381435Seric **
12391435Seric **	Returns:
12401435Seric **		a pointer to the tail of the filename; e.g., given
12411435Seric **		"cmd/ls.c", "ls.c" is returned.
12421435Seric **
12431435Seric **	Side Effects:
12441435Seric **		none.
12451435Seric */
12461435Seric 
12471435Seric char *
12481435Seric tail(fn)
12491435Seric 	register char *fn;
12501435Seric {
12511435Seric 	register char *p;
12521435Seric 
12531435Seric 	for (p = fn; *p != 0; p++)
12541435Seric 		if (*p == '/' && p[1] != '\0' && p[1] != '/')
12551435Seric 			fn = &p[1];
12561435Seric 	return (fn);
12571435Seric }
12581435Seric 
12591435Seric /*
12601822Seric **  GETPFENT -- get an entry from the p-file
1261396Seric **
1262396Seric **	Parameters:
1263396Seric **		pfp -- p-file file pointer
1264396Seric **
1265396Seric **	Returns:
1266396Seric **		pointer to p-file struct for next entry
1267396Seric **		NULL on EOF or error
1268396Seric **
1269396Seric **	Side Effects:
1270396Seric **		Each call wipes out results of previous call.
1271396Seric */
1272396Seric 
1273396Seric struct pfile *
12741822Seric getpfent(pfp)
1275396Seric 	FILE *pfp;
1276396Seric {
1277396Seric 	static struct pfile ent;
1278396Seric 	static char buf[120];
1279396Seric 	register char *p;
1280396Seric 	extern char *nextfield();
1281396Seric 
1282396Seric 	if (fgets(buf, sizeof buf, pfp) == NULL)
1283396Seric 		return (NULL);
1284396Seric 
1285396Seric 	ent.p_osid = p = buf;
1286396Seric 	ent.p_nsid = p = nextfield(p);
1287396Seric 	ent.p_user = p = nextfield(p);
1288396Seric 	ent.p_date = p = nextfield(p);
1289396Seric 	ent.p_time = p = nextfield(p);
12902161Seric 	ent.p_aux = p = nextfield(p);
1291396Seric 
1292396Seric 	return (&ent);
1293396Seric }
1294396Seric 
1295396Seric 
1296396Seric char *
1297396Seric nextfield(p)
1298396Seric 	register char *p;
1299396Seric {
1300396Seric 	if (p == NULL || *p == '\0')
1301396Seric 		return (NULL);
1302396Seric 	while (*p != ' ' && *p != '\n' && *p != '\0')
1303396Seric 		p++;
1304396Seric 	if (*p == '\n' || *p == '\0')
1305396Seric 	{
1306396Seric 		*p = '\0';
1307396Seric 		return (NULL);
1308396Seric 	}
1309396Seric 	*p++ = '\0';
1310396Seric 	return (p);
1311396Seric }
13122161Seric /*
13132161Seric **  PUTPFENT -- output a p-file entry to a file
13142161Seric **
13152161Seric **	Parameters:
13162161Seric **		pf -- the p-file entry
13172161Seric **		f -- the file to put it on.
13182161Seric **
13192161Seric **	Returns:
13202161Seric **		none.
13212161Seric **
13222161Seric **	Side Effects:
13232161Seric **		pf is written onto file f.
13242161Seric */
13252161Seric 
13262161Seric putpfent(pf, f)
13272161Seric 	register struct pfile *pf;
13282161Seric 	register FILE *f;
13292161Seric {
13302161Seric 	fprintf(f, "%s %s %s %s %s", pf->p_osid, pf->p_nsid,
13312161Seric 		pf->p_user, pf->p_date, pf->p_time);
13322161Seric 	if (pf->p_aux != NULL)
13332161Seric 		fprintf(f, " %s", pf->p_aux);
13342161Seric 	else
13352161Seric 		fprintf(f, "\n");
13362161Seric }
13371432Seric 
13381432Seric /*
13391205Seric **  USRERR -- issue user-level error
13401205Seric **
13411205Seric **	Parameters:
13421205Seric **		f -- format string.
13431205Seric **		p1-p3 -- parameters to a printf.
13441205Seric **
13451205Seric **	Returns:
13461205Seric **		-1
13471205Seric **
13481205Seric **	Side Effects:
13491205Seric **		none.
13501205Seric */
13511205Seric 
13521738Seric /*VARARGS1*/
13531205Seric usrerr(f, p1, p2, p3)
13541205Seric 	char *f;
13551205Seric {
13561205Seric 	fprintf(stderr, "\n%s: ", MyName);
13571205Seric 	fprintf(stderr, f, p1, p2, p3);
13581205Seric 	fprintf(stderr, "\n");
13591205Seric 
13601205Seric 	return (-1);
13611205Seric }
13621432Seric 
13631432Seric /*
13641205Seric **  SYSERR -- print system-generated error.
13651205Seric **
13661205Seric **	Parameters:
13671205Seric **		f -- format string to a printf.
13681205Seric **		p1, p2, p3 -- parameters to f.
13691205Seric **
13701205Seric **	Returns:
13711205Seric **		never.
13721205Seric **
13731205Seric **	Side Effects:
13741205Seric **		none.
13751205Seric */
13761205Seric 
13771738Seric /*VARARGS1*/
13781205Seric syserr(f, p1, p2, p3)
13791205Seric 	char *f;
13801205Seric {
13811205Seric 	extern int errno;
13821205Seric 
13831205Seric 	fprintf(stderr, "\n%s SYSERR: ", MyName);
13841205Seric 	fprintf(stderr, f, p1, p2, p3);
13851205Seric 	fprintf(stderr, "\n");
13861205Seric 	if (errno == 0)
13871205Seric 		exit(EX_SOFTWARE);
13881205Seric 	else
13891205Seric 	{
13901738Seric 		perror(NULL);
13911205Seric 		exit(EX_OSERR);
13921205Seric 	}
13931205Seric }
13941864Seric /*
13951864Seric **  USERNAME -- return name of the current user
13961864Seric **
13971864Seric **	Parameters:
13981864Seric **		none
13991864Seric **
14001864Seric **	Returns:
14011864Seric **		name of current user
14021864Seric **
14031864Seric **	Side Effects:
14041864Seric **		none
14051864Seric */
14061864Seric 
14071864Seric char *
14081864Seric username()
14091864Seric {
14101864Seric # ifdef UIDUSER
14111864Seric 	extern struct passwd *getpwuid();
14121864Seric 	register struct passwd *pw;
14131864Seric 
14141864Seric 	pw = getpwuid(getuid());
14151864Seric 	if (pw == NULL)
14161864Seric 	{
14171864Seric 		syserr("who are you? (uid=%d)", getuid());
14181864Seric 		exit(EX_OSERR);
14191864Seric 	}
14201864Seric 	return (pw->pw_name);
14211864Seric # else
14221905Seric 	extern char *getlogin();
14231905Seric 
14246783Smckusick 	return (getlogin());
14251864Seric # endif UIDUSER
14261864Seric }
1427